Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
2import { ref } from 'vue-demi'
3import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
4import { QueryClient } from '../queryClient'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6β
7vi.mock('@tanstack/query-core')
8β
9const queryKeyRef = ['foo', ref('bar')]
10const queryKeyUnref = ['foo', 'bar']
11β
12const fn = () => 'mock'
13β
14describe('QueryCache', () => {
15 beforeEach(() => {
16 vi.useFakeTimers()
17 })
18β
19 afterEach(() => {
20 vi.useRealTimers()
21 })
22β
23 describe('isFetching', () => {
24 test('should properly unwrap 1 parameter', () => {
25 const queryClient = new QueryClient()
26β
27 queryClient.isFetching({
28 queryKey: queryKeyRef,
29 })
30β
31 expect(QueryClientOrigin.prototype.isFetching).toBeCalledWith({
32 queryKey: queryKeyUnref,
33 })
34 })
35 })
36β
37 describe('isMutating', () => {
38 test('should properly unwrap 1 parameter', () => {
39 const queryClient = new QueryClient()
40β
41 queryClient.isMutating({
42 mutationKey: queryKeyRef,
43 })
44β
45 expect(QueryClientOrigin.prototype.isMutating).toBeCalledWith({
46 mutationKey: queryKeyUnref,
47 })
48 })
49 })
50β
51 describe('getQueryData', () => {
52 test('should properly unwrap 1 parameter', () => {
53 const queryClient = new QueryClient()
54β
55 queryClient.getQueryData(queryKeyRef)
56β
57 expect(QueryClientOrigin.prototype.getQueryData).toBeCalledWith(
58 queryKeyUnref,
59 )
60 })
61 })
62β
63 describe('getQueriesData', () => {
64 test('should properly unwrap queryKey param', () => {
65 const queryClient = new QueryClient()
66β
67 queryClient.getQueriesData({ queryKey: queryKeyRef })
68β
69 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
70 queryKey: queryKeyUnref,
71 })
72 })
73β
74 test('should properly unwrap filters param', () => {
75 const queryClient = new QueryClient()
76β
77 queryClient.getQueriesData({ queryKey: queryKeyRef })
78β
79 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
80 queryKey: queryKeyUnref,
81 })
82 })
83 })
84β
85 describe('setQueryData', () => {
86 test('should properly unwrap 3 parameter', () => {
87 const queryClient = new QueryClient()
88β
89 queryClient.setQueryData(queryKeyRef, fn, {
90 updatedAt: ref(3),
91 })
92β
93 expect(QueryClientOrigin.prototype.setQueryData).toBeCalledWith(
94 queryKeyUnref,
95 fn,
96 { updatedAt: 3 },
97 )
98 })
99 })
100β
101 describe('setQueriesData', () => {
102 test('should properly unwrap params with queryKey', () => {
103 const queryClient = new QueryClient()
104β
105 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
106 updatedAt: ref(3),
107 })
108β
109 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
110 { queryKey: queryKeyUnref },
111 fn,
112 { updatedAt: 3 },
113 )
114 })
115β
116 test('should properly unwrap params with filters', () => {
117 const queryClient = new QueryClient()
118β
119 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
120 updatedAt: ref(3),
121 })
122β
123 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
124 { queryKey: queryKeyUnref },
125 fn,
126 { updatedAt: 3 },
127 )
128 })
129 })
130β
131 describe('getQueryState', () => {
132 test('should properly unwrap 1 parameter', () => {
133 const queryClient = new QueryClient()
134β
135 queryClient.getQueryState(queryKeyRef)
136β
137 expect(QueryClientOrigin.prototype.getQueryState).toBeCalledWith(
138 queryKeyUnref,
139 )
140 })
141 })
142β
143 describe('removeQueries', () => {
144 test('should properly unwrap 1 parameter', () => {
145 const queryClient = new QueryClient()
146β
147 queryClient.removeQueries({
148 queryKey: queryKeyRef,
149 })
150β
151 expect(QueryClientOrigin.prototype.removeQueries).toBeCalledWith({
152 queryKey: queryKeyUnref,
153 })
154 })
155 })
156β
157 describe('resetQueries', () => {
158 test('should properly unwrap 2 parameter', () => {
159 const queryClient = new QueryClient()
160β
161 queryClient.resetQueries(
162 {
163 queryKey: queryKeyRef,
164 },
165 { cancelRefetch: ref(false) },
166 )
167β
168 expect(QueryClientOrigin.prototype.resetQueries).toBeCalledWith(
169 {
170 queryKey: queryKeyUnref,
171 },
172 { cancelRefetch: false },
173 )
174 })
175 })
176β
177 describe('cancelQueries', () => {
178 test('should properly unwrap 2 parameter', () => {
179 const queryClient = new QueryClient()
180β
181 queryClient.cancelQueries(
182 {
183 queryKey: queryKeyRef,
184 },
185 { revert: ref(false) },
186 )
187β
188 expect(QueryClientOrigin.prototype.cancelQueries).toBeCalledWith(
189 {
190 queryKey: queryKeyUnref,
191 },
192 { revert: false },
193 )
194 })
195 })
196β
197 describe('invalidateQueries', () => {
198 test('should properly unwrap 2 parameter', () => {
199 const queryClient = new QueryClient()
200β
201 queryClient.invalidateQueries(
202 {
203 queryKey: queryKeyRef,
204 },
205 { cancelRefetch: ref(false) },
206 )
207β
208 expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
209 {
210 queryKey: queryKeyUnref,
211 refetchType: 'none',
212 },
213 { cancelRefetch: false },
214 )
215 })
216β
217 // #7694
218 test('should call invalidateQueries immediately and refetchQueries after sleep', async () => {
219 const invalidateQueries = vi.spyOn(
220 QueryClientOrigin.prototype,
221 'invalidateQueries',
222 )
223 const refetchQueries = vi.spyOn(
224 QueryClientOrigin.prototype,
225 'refetchQueries',
226 )
227β
228 const queryClient = new QueryClient()
229β
230 queryClient.invalidateQueries({
231 queryKey: queryKeyRef,
232 })
233β
234 expect(invalidateQueries).toBeCalled()
235 expect(refetchQueries).not.toBeCalled()
236β
237 await vi.advanceTimersByTimeAsync(0)
238β
239 expect(refetchQueries).toBeCalled()
240 })
241β
242 test('should call invalidateQueries immediately and not call refetchQueries', async () => {
243 const invalidateQueries = vi.spyOn(
244 QueryClientOrigin.prototype,
245 'invalidateQueries',
246 )
247 const refetchQueries = vi.spyOn(
248 QueryClientOrigin.prototype,
249 'refetchQueries',
250 )
251β
252 const queryClient = new QueryClient()
253β
254 queryClient.invalidateQueries({
255 queryKey: queryKeyRef,
256 refetchType: 'none',
257 })
258β
259 expect(invalidateQueries).toBeCalled()
260 expect(refetchQueries).not.toBeCalled()
261β
262 await vi.advanceTimersByTimeAsync(0)
263β
264 expect(refetchQueries).not.toBeCalled()
265 })
266 })
267β
268 describe('refetchQueries', () => {
269 test('should properly unwrap 2 parameter', () => {
270 const queryClient = new QueryClient()
271β
272 queryClient.refetchQueries(
273 {
274 queryKey: queryKeyRef,
275 },
276 { cancelRefetch: ref(false) },
277 )
278β
279 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
280 {
281 queryKey: queryKeyUnref,
282 },
283 { cancelRefetch: false },
284 )
285 })
286 })
287β
288 describe('fetchQuery', () => {
289 test('should properly unwrap parameter', () => {
290 const queryClient = new QueryClient()
291β
292 queryClient.fetchQuery({
293 queryKey: queryKeyRef,
294 })
295β
296 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
297 queryKey: queryKeyUnref,
298 })
299 })
300 })
301β
302 describe('prefetchQuery', () => {
303 test('should properly unwrap parameters', () => {
304 const queryClient = new QueryClient()
305β
306 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
307β
308 expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith({
309 queryKey: queryKeyUnref,
310 queryFn: fn,
311 })
312 })
313 })
314β
315 describe('fetchInfiniteQuery', () => {
316 test('should properly unwrap parameter', () => {
317 const queryClient = new QueryClient()
318β
319 queryClient.fetchInfiniteQuery({
320 queryKey: queryKeyRef,
321 initialPageParam: 0,
322 })
323β
324 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
325 initialPageParam: 0,
326 queryKey: queryKeyUnref,
327 })
328 })
329 test('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
330 const queryClient = new QueryClient()
331β
332 const options = infiniteQueryOptions({
333 queryKey: queryKeyUnref,
334 initialPageParam: 0,
335 getNextPageParam: () => 12,
336 })
337β
338 queryClient.fetchInfiniteQuery(options)
339β
340 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
341 initialPageParam: 0,
342 queryKey: queryKeyUnref,
343 })
344 })
345 })
346β
347 describe('prefetchInfiniteQuery', () => {
348 test('should properly unwrap parameters', () => {
349 const queryClient = new QueryClient()
350β
351 queryClient.prefetchInfiniteQuery({
352 queryKey: queryKeyRef,
353 queryFn: fn,
354 initialPageParam: 0,
355 })
356β
357 expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({
358 initialPageParam: 0,
359 queryKey: queryKeyUnref,
360 queryFn: fn,
361 })
362 })
363 })
364β
365 describe('setDefaultOptions', () => {
366 test('should properly unwrap parameters', () => {
367 const queryClient = new QueryClient()
368β
369 queryClient.setDefaultOptions({
370 queries: {
371 enabled: ref(false),
372 },
373 })
374β
375 expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
376 queries: {
377 enabled: false,
378 },
379 })
380 })
381 })
382β
383 describe('setQueryDefaults', () => {
384 test('should properly unwrap parameters', () => {
385 const queryClient = new QueryClient()
386β
387 queryClient.setQueryDefaults(queryKeyRef, {
388 enabled: ref(false),
389 })
390β
391 expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
392 queryKeyUnref,
393 {
394 enabled: false,
395 },
396 )
397 })
398 })
399β
400 describe('getQueryDefaults', () => {
401 test('should properly unwrap parameters', () => {
402 const queryClient = new QueryClient()
403β
404 queryClient.getQueryDefaults(queryKeyRef)
405β
406 expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
407 queryKeyUnref,
408 )
409 })
410 })
411β
412 describe('setMutationDefaults', () => {
413 test('should properly unwrap parameters', () => {
414 const queryClient = new QueryClient()
415β
416 queryClient.setMutationDefaults(queryKeyRef, {
417 mutationKey: queryKeyRef,
418 })
419β
420 expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
421 queryKeyUnref,
422 {
423 mutationKey: queryKeyUnref,
424 },
425 )
426 })
427 })
428β
429 describe('getMutationDefaults', () => {
430 test('should properly unwrap parameters', () => {
431 const queryClient = new QueryClient()
432β
433 queryClient.getMutationDefaults(queryKeyRef)
434β
435 expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
436 queryKeyUnref,
437 )
438 })
439 })
440})