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