Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

usePrefetchQuery.test.ts

127 lines3.4 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { nextTick, ref } from 'vue-demi'
3import { queryKey } from '@tanstack/query-test-utils'
4import { QueryClient } from '../queryClient'
5import { usePrefetchQuery } from '../usePrefetchQuery'
6​
7describe('usePrefetchQuery', () => {
8 beforeEach(() => {
9 vi.useFakeTimers()
10 })
11​
12 afterEach(() => {
13 vi.useRealTimers()
14 })
15​
16 it('should prefetch query if query state does not exist', () => {
17 const queryClient = new QueryClient()
18 const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
19 const queryFn = vi.fn(() => Promise.resolve('prefetched'))
20 const key = queryKey()
21​
22 usePrefetchQuery(
23 {
24 queryKey: key,
25 queryFn,
26 },
27 queryClient,
28 )
29​
30 expect(prefetchQuerySpy).toHaveBeenCalledTimes(1)
31 expect(prefetchQuerySpy).toHaveBeenCalledWith({
32 queryKey: key,
33 queryFn,
34 })
35 })
36​
37 it('should not prefetch query if query state exists', () => {
38 const queryClient = new QueryClient()
39 const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
40 const queryFn = vi.fn(() => Promise.resolve('prefetched'))
41 const key = queryKey()
42 queryClient.setQueryData(key, 'existing')
43​
44 usePrefetchQuery(
45 {
46 queryKey: key,
47 queryFn,
48 },
49 queryClient,
50 )
51​
52 expect(prefetchQuerySpy).not.toHaveBeenCalled()
53 })
54​
55 it('should unwrap refs in query options', () => {
56 const queryClient = new QueryClient()
57 const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
58 const nestedRef = ref('value')
59 const key = queryKey()
60​
61 usePrefetchQuery(
62 {
63 queryKey: [...key, nestedRef],
64 queryFn: () => Promise.resolve('prefetched'),
65 },
66 queryClient,
67 )
68​
69 expect(prefetchQuerySpy).toHaveBeenCalledWith(
70 expect.objectContaining({
71 queryKey: [...key, 'value'],
72 }),
73 )
74 })
75​
76 it('should prefetch again when query key changes reactively', async () => {
77 const queryClient = new QueryClient()
78 const prefetchQuerySpy = vi.spyOn(queryClient, 'prefetchQuery')
79 const keyRef = ref('first')
80 const key = queryKey()
81​
82 usePrefetchQuery(
83 () => ({
84 queryKey: [...key, keyRef.value],
85 queryFn: () => Promise.resolve(keyRef.value),
86 }),
87 queryClient,
88 )
89​
90 expect(prefetchQuerySpy).toHaveBeenCalledTimes(1)
91 expect(prefetchQuerySpy).toHaveBeenLastCalledWith({
92 queryKey: [...key, 'first'],
93 queryFn: expect.any(Function),
94 })
95​
96 keyRef.value = 'second'
97 await nextTick()
98​
99 expect(prefetchQuerySpy).toHaveBeenCalledTimes(2)
100 expect(prefetchQuerySpy).toHaveBeenLastCalledWith({
101 queryKey: [...key, 'second'],
102 queryFn: expect.any(Function),
103 })
104 })
105​
106 it('should warn when used outside of setup function in development mode', () => {
107 vi.stubEnv('NODE_ENV', 'development')
108 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
109​
110 try {
111 usePrefetchQuery(
112 {
113 queryKey: queryKey(),
114 queryFn: () => Promise.resolve('prefetched'),
115 },
116 new QueryClient(),
117 )
118​
119 expect(warnSpy).toHaveBeenCalledWith(
120 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
121 )
122 } finally {
123 warnSpy.mockRestore()
124 vi.unstubAllEnvs()
125 }
126 })
127})