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