Open History to see the last commit
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 querySpy = vi.spyOn(queryClient, 'query')
19 const queryFn = () => Promise.resolve('prefetched')
20 const key = queryKey()
21β
22 usePrefetchQuery(
23 {
24 queryKey: key,
25 queryFn,
26 },
27 queryClient,
28 )
29β
30 expect(querySpy).toHaveBeenCalledTimes(1)
31 expect(querySpy).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 querySpy = vi.spyOn(queryClient, 'query')
40 const queryFn = () => 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(querySpy).not.toHaveBeenCalled()
53 })
54β
55 it('should unwrap refs in query options', () => {
56 const queryClient = new QueryClient()
57 const querySpy = vi.spyOn(queryClient, 'query')
58 const nestedRef = ref('value')
59 const key = queryKey()
60 const queryFn = () => Promise.resolve('prefetched')
61β
62 usePrefetchQuery(
63 {
64 queryKey: [...key, nestedRef],
65 queryFn,
66 },
67 queryClient,
68 )
69β
70 expect(querySpy).toHaveBeenCalledWith({
71 queryKey: [...key, 'value'],
72 queryFn,
73 })
74 })
75β
76 it('should prefetch again when query key changes reactively', async () => {
77 const queryClient = new QueryClient()
78 const querySpy = vi.spyOn(queryClient, 'query')
79 const keyRef = ref('first')
80 const key = queryKey()
81 const queryFn = () => Promise.resolve(keyRef.value)
82β
83 usePrefetchQuery(
84 () => ({
85 queryKey: [...key, keyRef.value],
86 queryFn,
87 }),
88 queryClient,
89 )
90β
91 expect(querySpy).toHaveBeenCalledTimes(1)
92 expect(querySpy).toHaveBeenNthCalledWith(1, {
93 queryKey: [...key, 'first'],
94 queryFn,
95 })
96β
97 keyRef.value = 'second'
98 await nextTick()
99β
100 expect(querySpy).toHaveBeenCalledTimes(2)
101 expect(querySpy).toHaveBeenNthCalledWith(2, {
102 queryKey: [...key, 'second'],
103 queryFn,
104 })
105 })
106β
107 it('should warn when used outside of setup function in development mode', () => {
108 vi.stubEnv('NODE_ENV', 'development')
109 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
110β
111 try {
112 usePrefetchQuery(
113 {
114 queryKey: queryKey(),
115 queryFn: () => Promise.resolve('prefetched'),
116 },
117 new QueryClient(),
118 )
119β
120 expect(warnSpy).toHaveBeenCalledWith(
121 '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.',
122 )
123 } finally {
124 warnSpy.mockRestore()
125 vi.unstubAllEnvs()
126 }
127 })
128})