Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { ref, unref } from 'vue-demi'
3import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
4import { QueryClient } from '../queryClient'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import { queryOptions } from '../queryOptions'
7β
8vi.mock('@tanstack/query-core', async () => {
9 const actual = await vi.importActual<{
10 QueryClient: typeof QueryClientOrigin
11 }>('@tanstack/query-core')
12β
13 // Get the prototype methods dynamically
14 const prototypeMethods = Object.getOwnPropertyNames(
15 actual.QueryClient.prototype,
16 ).filter((prop): prop is keyof typeof actual.QueryClient.prototype => {
17 const descriptor = Object.getOwnPropertyDescriptor(
18 actual.QueryClient.prototype,
19 prop,
20 )
21 return typeof descriptor?.value === 'function' && prop !== 'constructor'
22 })
23β
24 // Spy on all methods in the prototype
25 prototypeMethods.forEach((method) => {
26 vi.spyOn(actual.QueryClient.prototype, method)
27 })
28β
29 return actual
30})
31β
32const queryKeyRef = ['foo', ref('bar')]
33const queryKeyUnref = ['foo', 'bar']
34β
35const fn = () => 'mock'
36β
37describe('QueryCache', () => {
38 beforeEach(() => {
39 vi.useFakeTimers()
40 vi.clearAllMocks()
41 })
42β
43 afterEach(() => {
44 vi.useRealTimers()
45 })
46β
47 describe('isFetching', () => {
48 it('should properly unwrap 1 parameter', () => {
49 const queryClient = new QueryClient()
50β
51 queryClient.isFetching({
52 queryKey: queryKeyRef,
53 })
54β
55 expect(QueryClientOrigin.prototype.isFetching).toHaveBeenCalledWith({
56 queryKey: queryKeyUnref,
57 })
58 })
59 })
60β
61 describe('isMutating', () => {
62 it('should properly unwrap 1 parameter', () => {
63 const queryClient = new QueryClient()
64β
65 queryClient.isMutating({
66 mutationKey: queryKeyRef,
67 })
68β
69 expect(QueryClientOrigin.prototype.isMutating).toHaveBeenCalledWith({
70 mutationKey: queryKeyUnref,
71 })
72 })
73 })
74β
75 describe('getQueryData', () => {
76 it('should properly unwrap 1 parameter', () => {
77 const queryClient = new QueryClient()
78β
79 queryClient.getQueryData(queryKeyRef)
80β
81 expect(QueryClientOrigin.prototype.getQueryData).toHaveBeenCalledWith(
82 queryKeyUnref,
83 )
84 })
85 })
86β
87 describe('ensureQueryData', () => {
88 it('should properly unwrap parameter', () => {
89 const queryClient = new QueryClient()
90β
91 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
92 queryClient.ensureQueryData({
93 queryKey: queryKeyRef,
94 queryFn: fn,
95 })
96β
97 expect(QueryClientOrigin.prototype.ensureQueryData).toHaveBeenCalledWith({
98 queryKey: queryKeyUnref,
99 queryFn: fn,
100 })
101 })
102 })
103β
104 describe('getQueriesData', () => {
105 it('should properly unwrap queryKey param', () => {
106 const queryClient = new QueryClient()
107β
108 queryClient.getQueriesData({ queryKey: queryKeyRef })
109β
110 expect(QueryClientOrigin.prototype.getQueriesData).toHaveBeenCalledWith({
111 queryKey: queryKeyUnref,
112 })
113 })
114β
115 it('should properly unwrap filters param', () => {
116 const queryClient = new QueryClient()
117β
118 queryClient.getQueriesData({ queryKey: queryKeyRef })
119β
120 expect(QueryClientOrigin.prototype.getQueriesData).toHaveBeenCalledWith({
121 queryKey: queryKeyUnref,
122 })
123 })
124 })
125β
126 describe('setQueryData', () => {
127 it('should properly unwrap 3 parameter', () => {
128 const queryClient = new QueryClient()
129β
130 queryClient.setQueryData(queryKeyRef, fn, {
131 updatedAt: ref(3),
132 })
133β
134 expect(QueryClientOrigin.prototype.setQueryData).toHaveBeenCalledWith(
135 queryKeyUnref,
136 fn,
137 { updatedAt: 3 },
138 )
139 })
140 })
141β
142 describe('setQueriesData', () => {
143 it('should properly unwrap params with queryKey', () => {
144 const queryClient = new QueryClient()
145β
146 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
147 updatedAt: ref(3),
148 })
149β
150 expect(QueryClientOrigin.prototype.setQueriesData).toHaveBeenCalledWith(
151 { queryKey: queryKeyUnref },
152 fn,
153 { updatedAt: 3 },
154 )
155 })
156β
157 it('should properly unwrap params with filters', () => {
158 const queryClient = new QueryClient()
159β
160 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
161 updatedAt: ref(3),
162 })
163β
164 expect(QueryClientOrigin.prototype.setQueriesData).toHaveBeenCalledWith(
165 { queryKey: queryKeyUnref },
166 fn,
167 { updatedAt: 3 },
168 )
169 })
170 })
171β
172 describe('getQueryState', () => {
173 it('should properly unwrap 1 parameter', () => {
174 const queryClient = new QueryClient()
175β
176 queryClient.getQueryState(queryKeyRef)
177β
178 expect(QueryClientOrigin.prototype.getQueryState).toHaveBeenCalledWith(
179 queryKeyUnref,
180 )
181 })
182 })
183β
184 describe('removeQueries', () => {
185 it('should properly unwrap 1 parameter', () => {
186 const queryClient = new QueryClient()
187β
188 queryClient.removeQueries({
189 queryKey: queryKeyRef,
190 })
191β
192 expect(QueryClientOrigin.prototype.removeQueries).toHaveBeenCalledWith({
193 queryKey: queryKeyUnref,
194 })
195 })
196 })
197β
198 describe('resetQueries', () => {
199 it('should properly unwrap 2 parameter', () => {
200 const queryClient = new QueryClient()
201β
202 queryClient.resetQueries(
203 {
204 queryKey: queryKeyRef,
205 },
206 { cancelRefetch: ref(false) },
207 )
208β
209 expect(QueryClientOrigin.prototype.resetQueries).toHaveBeenCalledWith(
210 {
211 queryKey: queryKeyUnref,
212 },
213 { cancelRefetch: false },
214 )
215 })
216 })
217β
218 describe('cancelQueries', () => {
219 it('should properly unwrap 2 parameter', () => {
220 const queryClient = new QueryClient()
221β
222 queryClient.cancelQueries(
223 {
224 queryKey: queryKeyRef,
225 },
226 { revert: ref(false) },
227 )
228β
229 expect(QueryClientOrigin.prototype.cancelQueries).toHaveBeenCalledWith(
230 {
231 queryKey: queryKeyUnref,
232 },
233 { revert: false },
234 )
235 })
236 })
237β
238 describe('invalidateQueries', () => {
239 it('should properly unwrap 2 parameter', () => {
240 const queryClient = new QueryClient()
241β
242 queryClient.invalidateQueries(
243 {
244 queryKey: queryKeyRef,
245 },
246 { cancelRefetch: ref(false) },
247 )
248β
249 expect(
250 QueryClientOrigin.prototype.invalidateQueries,
251 ).toHaveBeenCalledWith(
252 {
253 queryKey: queryKeyUnref,
254 refetchType: 'none',
255 },
256 { cancelRefetch: false },
257 )
258 })
259β
260 // #7694
261 it('should call invalidateQueries immediately and refetchQueries after sleep', async () => {
262 const invalidateQueries = vi.spyOn(
263 QueryClientOrigin.prototype,
264 'invalidateQueries',
265 )
266 const refetchQueries = vi.spyOn(
267 QueryClientOrigin.prototype,
268 'refetchQueries',
269 )
270β
271 const queryClient = new QueryClient()
272β
273 queryClient.invalidateQueries({
274 queryKey: queryKeyRef,
275 })
276β
277 expect(invalidateQueries).toHaveBeenCalled()
278 expect(refetchQueries).not.toHaveBeenCalled()
279β
280 await vi.advanceTimersByTimeAsync(0)
281β
282 expect(refetchQueries).toHaveBeenCalled()
283 })
284β
285 it('should call invalidateQueries immediately and not call refetchQueries', async () => {
286 const invalidateQueries = vi.spyOn(
287 QueryClientOrigin.prototype,
288 'invalidateQueries',
289 )
290 const refetchQueries = vi.spyOn(
291 QueryClientOrigin.prototype,
292 'refetchQueries',
293 )
294β
295 const queryClient = new QueryClient()
296β
297 queryClient.invalidateQueries({
298 queryKey: queryKeyRef,
299 refetchType: 'none',
300 })
301β
302 expect(invalidateQueries).toHaveBeenCalled()
303 expect(refetchQueries).not.toHaveBeenCalled()
304β
305 await vi.advanceTimersByTimeAsync(0)
306β
307 expect(refetchQueries).not.toHaveBeenCalled()
308 })
309 })
310β
311 describe('refetchQueries', () => {
312 it('should properly unwrap 2 parameter', () => {
313 const queryClient = new QueryClient()
314β
315 queryClient.refetchQueries(
316 {
317 queryKey: queryKeyRef,
318 },
319 { cancelRefetch: ref(false) },
320 )
321β
322 expect(QueryClientOrigin.prototype.refetchQueries).toHaveBeenCalledWith(
323 {
324 queryKey: queryKeyUnref,
325 },
326 { cancelRefetch: false },
327 )
328 })
329 })
330β
331 describe('fetchQuery', () => {
332 it('should properly unwrap parameter', () => {
333 const queryClient = new QueryClient()
334β
335 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
336 queryClient.fetchQuery({
337 queryKey: queryKeyRef,
338 })
339β
340 expect(QueryClientOrigin.prototype.fetchQuery).toHaveBeenCalledWith({
341 queryKey: queryKeyUnref,
342 })
343 })
344 })
345β
346 describe('query', () => {
347 it('should properly unwrap queryKey', () => {
348 const queryClient = new QueryClient()
349β
350 queryClient.query({
351 queryKey: queryKeyRef,
352 })
353β
354 expect(QueryClientOrigin.prototype.query).toHaveBeenCalledWith({
355 queryKey: queryKeyUnref,
356 })
357 })
358β
359 it('should properly unwrap staleTime, and select', () => {
360 const queryClient = new QueryClient()
361 const staleTime = () => 1000
362 const select = (data: string) => data.length
363β
364 queryClient.query({
365 queryKey: queryKeyRef,
366 staleTime: ref(staleTime),
367 select: ref(select),
368 })
369β
370 expect(QueryClientOrigin.prototype.query).toHaveBeenCalledWith({
371 queryKey: queryKeyUnref,
372 staleTime,
373 select,
374 })
375 })
376β
377 it('should accept explicitly resolved getter options and unwrap queryKey', () => {
378 const queryClient = new QueryClient()
379 const options = queryOptions(() => ({
380 queryKey: queryKeyRef,
381 }))
382β
383 queryClient.query(options())
384β
385 expect(QueryClientOrigin.prototype.query).toHaveBeenCalledWith({
386 queryKey: queryKeyUnref,
387 })
388 })
389 })
390β
391 describe('prefetchQuery', () => {
392 it('should properly unwrap parameters', () => {
393 const queryClient = new QueryClient()
394β
395 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
396 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
397β
398 expect(QueryClientOrigin.prototype.prefetchQuery).toHaveBeenCalledWith({
399 queryKey: queryKeyUnref,
400 queryFn: fn,
401 })
402 })
403 })
404β
405 describe('fetchInfiniteQuery', () => {
406 it('should properly unwrap parameter', () => {
407 const queryClient = new QueryClient()
408β
409 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
410 queryClient.fetchInfiniteQuery({
411 queryKey: queryKeyRef,
412 initialPageParam: 0,
413 })
414β
415 expect(
416 QueryClientOrigin.prototype.fetchInfiniteQuery,
417 ).toHaveBeenCalledWith(
418 expect.objectContaining({
419 initialPageParam: 0,
420 queryKey: queryKeyUnref,
421 }),
422 )
423 })
424 it('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
425 const queryClient = new QueryClient()
426β
427 const options = infiniteQueryOptions({
428 queryKey: queryKeyUnref,
429 initialPageParam: 0,
430 getNextPageParam: () => 12,
431 })
432β
433 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
434 queryClient.fetchInfiniteQuery(options)
435β
436 expect(
437 QueryClientOrigin.prototype.fetchInfiniteQuery,
438 ).toHaveBeenCalledWith(
439 expect.objectContaining({
440 initialPageParam: 0,
441 queryKey: queryKeyUnref,
442 }),
443 )
444 })
445 })
446β
447 describe('infiniteQuery', () => {
448 it('should properly unwrap queryKey, initialPageParam, pages, and select', () => {
449 const queryClient = new QueryClient()
450 const getNextPageParam = () => 1
451 const select = (data: { pages: Array<string> }) => data.pages.length
452β
453 queryClient.infiniteQuery({
454 queryKey: queryKeyRef,
455 initialPageParam: ref(0),
456 pages: ref(2),
457 getNextPageParam: ref(getNextPageParam),
458 select: ref(select),
459 })
460β
461 expect(QueryClientOrigin.prototype.infiniteQuery).toBeCalledWith(
462 expect.objectContaining({
463 queryKey: queryKeyUnref,
464 initialPageParam: 0,
465 pages: 2,
466 getNextPageParam,
467 select,
468 }),
469 )
470 })
471β
472 it('should properly unwrap getNextPageParam when using infiniteQueryOptions', () => {
473 const queryClient = new QueryClient()
474 const getNextPageParam = () => 12
475β
476 const options = infiniteQueryOptions({
477 queryKey: queryKeyRef,
478 initialPageParam: ref(0),
479 getNextPageParam: ref(getNextPageParam),
480 })
481β
482 queryClient.infiniteQuery({
483 ...unref(options),
484 staleTime: 0,
485 pages: 1,
486 })
487β
488 expect(QueryClientOrigin.prototype.infiniteQuery).toHaveBeenCalledWith(
489 expect.objectContaining({
490 queryKey: queryKeyUnref,
491 initialPageParam: 0,
492 pages: 1,
493 getNextPageParam,
494 }),
495 )
496 })
497 })
498β
499 describe('prefetchInfiniteQuery', () => {
500 it('should properly unwrap parameters', () => {
501 const queryClient = new QueryClient()
502β
503 // eslint-disable-next-line no-restricted-syntax -- grandfathered direct test
504 queryClient.prefetchInfiniteQuery({
505 queryKey: queryKeyRef,
506 queryFn: fn,
507 initialPageParam: 0,
508 })
509β
510 expect(
511 QueryClientOrigin.prototype.prefetchInfiniteQuery,
512 ).toHaveBeenCalledWith({
513 initialPageParam: 0,
514 queryKey: queryKeyUnref,
515 queryFn: fn,
516 })
517 })
518 })
519β
520 describe('setDefaultOptions', () => {
521 it('should properly unwrap parameters', () => {
522 const queryClient = new QueryClient()
523β
524 queryClient.setDefaultOptions({
525 queries: {
526 enabled: ref(false),
527 },
528 })
529β
530 expect(
531 QueryClientOrigin.prototype.setDefaultOptions,
532 ).toHaveBeenCalledWith({
533 queries: {
534 enabled: false,
535 },
536 })
537 })
538 })
539β
540 describe('setQueryDefaults', () => {
541 it('should properly unwrap parameters', () => {
542 const queryClient = new QueryClient()
543β
544 queryClient.setQueryDefaults(queryKeyRef, {
545 enabled: ref(false),
546 })
547β
548 expect(QueryClientOrigin.prototype.setQueryDefaults).toHaveBeenCalledWith(
549 queryKeyUnref,
550 {
551 enabled: false,
552 },
553 )
554 })
555 })
556β
557 describe('getQueryDefaults', () => {
558 it('should properly unwrap parameters', () => {
559 const queryClient = new QueryClient()
560β
561 queryClient.getQueryDefaults(queryKeyRef)
562β
563 expect(QueryClientOrigin.prototype.getQueryDefaults).toHaveBeenCalledWith(
564 queryKeyUnref,
565 )
566 })
567 })
568β
569 describe('setMutationDefaults', () => {
570 it('should properly unwrap parameters', () => {
571 const queryClient = new QueryClient()
572β
573 queryClient.setMutationDefaults(queryKeyRef, {
574 mutationKey: queryKeyRef,
575 })
576β
577 expect(
578 QueryClientOrigin.prototype.setMutationDefaults,
579 ).toHaveBeenCalledWith(queryKeyUnref, {
580 mutationKey: queryKeyUnref,
581 })
582 })
583 })
584β
585 describe('getMutationDefaults', () => {
586 it('should properly unwrap parameters', () => {
587 const queryClient = new QueryClient()
588β
589 queryClient.getMutationDefaults(queryKeyRef)
590β
591 expect(
592 QueryClientOrigin.prototype.getMutationDefaults,
593 ).toHaveBeenCalledWith(queryKeyUnref)
594 })
595 })
596})