Open History to see the last commit
1import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3import { ref, unref } from 'vue-demi'
4import { infiniteQueryOptions } from '../infiniteQueryOptions'
5import { MutationCache } from '../mutationCache'
6import { QueryCache } from '../queryCache'
7import { QueryClient } from '../queryClient'
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 getNextPageParam: () => undefined,
441 initialPageParam: 0,
442 })
443β
444 expect(
445 QueryClientOrigin.prototype.fetchInfiniteQuery,
446 ).toHaveBeenCalledWith(
447 expect.objectContaining({
448 initialPageParam: 0,
449 queryKey: queryKeyUnref,
450 }),
451 )
452 })
453β
454 it('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
455 const queryClient = new QueryClient()
456β
457 const options = infiniteQueryOptions({
458 queryKey: queryKeyUnref,
459 initialPageParam: 0,
460 getNextPageParam: () => 12,
461 })
462β
463 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
464 queryClient.fetchInfiniteQuery(options)
465β
466 expect(
467 QueryClientOrigin.prototype.fetchInfiniteQuery,
468 ).toHaveBeenCalledWith(
469 expect.objectContaining({
470 initialPageParam: 0,
471 queryKey: queryKeyUnref,
472 }),
473 )
474 })
475 })
476β
477 describe('infiniteQuery', () => {
478 it('should properly unwrap queryKey, initialPageParam, pages, and select', () => {
479 const queryClient = new QueryClient()
480 const getNextPageParam = () => 1
481 const select = (data: { pages: Array<string> }) => data.pages.length
482β
483 queryClient.infiniteQuery({
484 queryKey: queryKeyRef,
485 initialPageParam: ref(0),
486 pages: ref(2),
487 getNextPageParam: ref(getNextPageParam),
488 select: ref(select),
489 })
490β
491 expect(QueryClientOrigin.prototype.infiniteQuery).toHaveBeenCalledWith(
492 expect.objectContaining({
493 queryKey: queryKeyUnref,
494 initialPageParam: 0,
495 pages: 2,
496 getNextPageParam,
497 select,
498 }),
499 )
500 })
501β
502 it('should properly unwrap getNextPageParam when using infiniteQueryOptions', () => {
503 const queryClient = new QueryClient()
504 const getNextPageParam = () => 12
505β
506 const options = infiniteQueryOptions({
507 queryKey: queryKeyRef,
508 initialPageParam: ref(0),
509 getNextPageParam: ref(getNextPageParam),
510 })
511β
512 queryClient.infiniteQuery({
513 ...unref(options),
514 staleTime: 0,
515 pages: 1,
516 })
517β
518 expect(QueryClientOrigin.prototype.infiniteQuery).toHaveBeenCalledWith(
519 expect.objectContaining({
520 queryKey: queryKeyUnref,
521 initialPageParam: 0,
522 pages: 1,
523 getNextPageParam,
524 }),
525 )
526 })
527 })
528β
529 describe('prefetchInfiniteQuery', () => {
530 it('should properly unwrap parameters', () => {
531 const queryClient = new QueryClient()
532 const getNextPageParam = () => undefined
533β
534 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
535 queryClient.prefetchInfiniteQuery({
536 queryKey: queryKeyRef,
537 queryFn: fn,
538 getNextPageParam,
539 initialPageParam: 0,
540 })
541β
542 expect(
543 QueryClientOrigin.prototype.prefetchInfiniteQuery,
544 ).toHaveBeenCalledWith({
545 initialPageParam: 0,
546 queryKey: queryKeyUnref,
547 queryFn: fn,
548 getNextPageParam,
549 })
550 })
551 })
552β
553 describe('setDefaultOptions', () => {
554 it('should properly unwrap parameters', () => {
555 const queryClient = new QueryClient()
556β
557 queryClient.setDefaultOptions({
558 queries: {
559 enabled: ref(false),
560 },
561 })
562β
563 expect(
564 QueryClientOrigin.prototype.setDefaultOptions,
565 ).toHaveBeenCalledWith({
566 queries: {
567 enabled: false,
568 },
569 })
570 })
571 })
572β
573 describe('setQueryDefaults', () => {
574 it('should properly unwrap parameters', () => {
575 const queryClient = new QueryClient()
576β
577 queryClient.setQueryDefaults(queryKeyRef, {
578 enabled: ref(false),
579 })
580β
581 expect(QueryClientOrigin.prototype.setQueryDefaults).toHaveBeenCalledWith(
582 queryKeyUnref,
583 {
584 enabled: false,
585 },
586 )
587 })
588 })
589β
590 describe('getQueryDefaults', () => {
591 it('should properly unwrap parameters', () => {
592 const queryClient = new QueryClient()
593β
594 queryClient.getQueryDefaults(queryKeyRef)
595β
596 expect(QueryClientOrigin.prototype.getQueryDefaults).toHaveBeenCalledWith(
597 queryKeyUnref,
598 )
599 })
600 })
601β
602 describe('setMutationDefaults', () => {
603 it('should properly unwrap parameters', () => {
604 const queryClient = new QueryClient()
605β
606 queryClient.setMutationDefaults(queryKeyRef, {
607 mutationKey: queryKeyRef,
608 })
609β
610 expect(
611 QueryClientOrigin.prototype.setMutationDefaults,
612 ).toHaveBeenCalledWith(queryKeyUnref, {
613 mutationKey: queryKeyUnref,
614 })
615 })
616 })
617β
618 describe('getMutationDefaults', () => {
619 it('should properly unwrap parameters', () => {
620 const queryClient = new QueryClient()
621β
622 queryClient.getMutationDefaults(queryKeyRef)
623β
624 expect(
625 QueryClientOrigin.prototype.getMutationDefaults,
626 ).toHaveBeenCalledWith(queryKeyUnref)
627 })
628 })
629})