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

usePrefetchQuery.test.ts

122 lines3.4 KB

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