Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryClient.test.ts

629 lines16.6 KB

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})