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

queryClient.test.ts

590 lines14.7 KB

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 queryClient.ensureQueryData({
92 queryKey: queryKeyRef,
93 queryFn: fn,
94 })
95​
96 expect(QueryClientOrigin.prototype.ensureQueryData).toHaveBeenCalledWith({
97 queryKey: queryKeyUnref,
98 queryFn: fn,
99 })
100 })
101 })
102​
103 describe('getQueriesData', () => {
104 it('should properly unwrap queryKey param', () => {
105 const queryClient = new QueryClient()
106​
107 queryClient.getQueriesData({ queryKey: queryKeyRef })
108​
109 expect(QueryClientOrigin.prototype.getQueriesData).toHaveBeenCalledWith({
110 queryKey: queryKeyUnref,
111 })
112 })
113​
114 it('should properly unwrap filters param', () => {
115 const queryClient = new QueryClient()
116​
117 queryClient.getQueriesData({ queryKey: queryKeyRef })
118​
119 expect(QueryClientOrigin.prototype.getQueriesData).toHaveBeenCalledWith({
120 queryKey: queryKeyUnref,
121 })
122 })
123 })
124​
125 describe('setQueryData', () => {
126 it('should properly unwrap 3 parameter', () => {
127 const queryClient = new QueryClient()
128​
129 queryClient.setQueryData(queryKeyRef, fn, {
130 updatedAt: ref(3),
131 })
132​
133 expect(QueryClientOrigin.prototype.setQueryData).toHaveBeenCalledWith(
134 queryKeyUnref,
135 fn,
136 { updatedAt: 3 },
137 )
138 })
139 })
140​
141 describe('setQueriesData', () => {
142 it('should properly unwrap params with queryKey', () => {
143 const queryClient = new QueryClient()
144​
145 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
146 updatedAt: ref(3),
147 })
148​
149 expect(QueryClientOrigin.prototype.setQueriesData).toHaveBeenCalledWith(
150 { queryKey: queryKeyUnref },
151 fn,
152 { updatedAt: 3 },
153 )
154 })
155​
156 it('should properly unwrap params with filters', () => {
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​
171 describe('getQueryState', () => {
172 it('should properly unwrap 1 parameter', () => {
173 const queryClient = new QueryClient()
174​
175 queryClient.getQueryState(queryKeyRef)
176​
177 expect(QueryClientOrigin.prototype.getQueryState).toHaveBeenCalledWith(
178 queryKeyUnref,
179 )
180 })
181 })
182​
183 describe('removeQueries', () => {
184 it('should properly unwrap 1 parameter', () => {
185 const queryClient = new QueryClient()
186​
187 queryClient.removeQueries({
188 queryKey: queryKeyRef,
189 })
190​
191 expect(QueryClientOrigin.prototype.removeQueries).toHaveBeenCalledWith({
192 queryKey: queryKeyUnref,
193 })
194 })
195 })
196​
197 describe('resetQueries', () => {
198 it('should properly unwrap 2 parameter', () => {
199 const queryClient = new QueryClient()
200​
201 queryClient.resetQueries(
202 {
203 queryKey: queryKeyRef,
204 },
205 { cancelRefetch: ref(false) },
206 )
207​
208 expect(QueryClientOrigin.prototype.resetQueries).toHaveBeenCalledWith(
209 {
210 queryKey: queryKeyUnref,
211 },
212 { cancelRefetch: false },
213 )
214 })
215 })
216​
217 describe('cancelQueries', () => {
218 it('should properly unwrap 2 parameter', () => {
219 const queryClient = new QueryClient()
220​
221 queryClient.cancelQueries(
222 {
223 queryKey: queryKeyRef,
224 },
225 { revert: ref(false) },
226 )
227​
228 expect(QueryClientOrigin.prototype.cancelQueries).toHaveBeenCalledWith(
229 {
230 queryKey: queryKeyUnref,
231 },
232 { revert: false },
233 )
234 })
235 })
236​
237 describe('invalidateQueries', () => {
238 it('should properly unwrap 2 parameter', () => {
239 const queryClient = new QueryClient()
240​
241 queryClient.invalidateQueries(
242 {
243 queryKey: queryKeyRef,
244 },
245 { cancelRefetch: ref(false) },
246 )
247​
248 expect(
249 QueryClientOrigin.prototype.invalidateQueries,
250 ).toHaveBeenCalledWith(
251 {
252 queryKey: queryKeyUnref,
253 refetchType: 'none',
254 },
255 { cancelRefetch: false },
256 )
257 })
258​
259 // #7694
260 it('should call invalidateQueries immediately and refetchQueries after sleep', async () => {
261 const invalidateQueries = vi.spyOn(
262 QueryClientOrigin.prototype,
263 'invalidateQueries',
264 )
265 const refetchQueries = vi.spyOn(
266 QueryClientOrigin.prototype,
267 'refetchQueries',
268 )
269​
270 const queryClient = new QueryClient()
271​
272 queryClient.invalidateQueries({
273 queryKey: queryKeyRef,
274 })
275​
276 expect(invalidateQueries).toHaveBeenCalled()
277 expect(refetchQueries).not.toHaveBeenCalled()
278​
279 await vi.advanceTimersByTimeAsync(0)
280​
281 expect(refetchQueries).toHaveBeenCalled()
282 })
283​
284 it('should call invalidateQueries immediately and not call refetchQueries', async () => {
285 const invalidateQueries = vi.spyOn(
286 QueryClientOrigin.prototype,
287 'invalidateQueries',
288 )
289 const refetchQueries = vi.spyOn(
290 QueryClientOrigin.prototype,
291 'refetchQueries',
292 )
293​
294 const queryClient = new QueryClient()
295​
296 queryClient.invalidateQueries({
297 queryKey: queryKeyRef,
298 refetchType: 'none',
299 })
300​
301 expect(invalidateQueries).toHaveBeenCalled()
302 expect(refetchQueries).not.toHaveBeenCalled()
303​
304 await vi.advanceTimersByTimeAsync(0)
305​
306 expect(refetchQueries).not.toHaveBeenCalled()
307 })
308 })
309​
310 describe('refetchQueries', () => {
311 it('should properly unwrap 2 parameter', () => {
312 const queryClient = new QueryClient()
313​
314 queryClient.refetchQueries(
315 {
316 queryKey: queryKeyRef,
317 },
318 { cancelRefetch: ref(false) },
319 )
320​
321 expect(QueryClientOrigin.prototype.refetchQueries).toHaveBeenCalledWith(
322 {
323 queryKey: queryKeyUnref,
324 },
325 { cancelRefetch: false },
326 )
327 })
328 })
329​
330 describe('fetchQuery', () => {
331 it('should properly unwrap parameter', () => {
332 const queryClient = new QueryClient()
333​
334 queryClient.fetchQuery({
335 queryKey: queryKeyRef,
336 })
337​
338 expect(QueryClientOrigin.prototype.fetchQuery).toHaveBeenCalledWith({
339 queryKey: queryKeyUnref,
340 })
341 })
342 })
343​
344 describe('query', () => {
345 it('should properly unwrap queryKey', () => {
346 const queryClient = new QueryClient()
347​
348 queryClient.query({
349 queryKey: queryKeyRef,
350 })
351​
352 expect(QueryClientOrigin.prototype.query).toHaveBeenCalledWith({
353 queryKey: queryKeyUnref,
354 })
355 })
356​
357 it('should properly unwrap staleTime, and select', () => {
358 const queryClient = new QueryClient()
359 const staleTime = () => 1000
360 const select = (data: string) => data.length
361​
362 queryClient.query({
363 queryKey: queryKeyRef,
364 staleTime: ref(staleTime),
365 select: ref(select),
366 })
367​
368 expect(QueryClientOrigin.prototype.query).toHaveBeenCalledWith({
369 queryKey: queryKeyUnref,
370 staleTime,
371 select,
372 })
373 })
374​
375 it('should accept explicitly resolved getter options and unwrap queryKey', () => {
376 const queryClient = new QueryClient()
377 const options = queryOptions(() => ({
378 queryKey: queryKeyRef,
379 }))
380​
381 queryClient.query(options())
382​
383 expect(QueryClientOrigin.prototype.query).toHaveBeenCalledWith({
384 queryKey: queryKeyUnref,
385 })
386 })
387 })
388​
389 describe('prefetchQuery', () => {
390 it('should properly unwrap parameters', () => {
391 const queryClient = new QueryClient()
392​
393 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
394​
395 expect(QueryClientOrigin.prototype.prefetchQuery).toHaveBeenCalledWith({
396 queryKey: queryKeyUnref,
397 queryFn: fn,
398 })
399 })
400 })
401​
402 describe('fetchInfiniteQuery', () => {
403 it('should properly unwrap parameter', () => {
404 const queryClient = new QueryClient()
405​
406 queryClient.fetchInfiniteQuery({
407 queryKey: queryKeyRef,
408 initialPageParam: 0,
409 })
410​
411 expect(
412 QueryClientOrigin.prototype.fetchInfiniteQuery,
413 ).toHaveBeenCalledWith(
414 expect.objectContaining({
415 initialPageParam: 0,
416 queryKey: queryKeyUnref,
417 }),
418 )
419 })
420 it('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
421 const queryClient = new QueryClient()
422​
423 const options = infiniteQueryOptions({
424 queryKey: queryKeyUnref,
425 initialPageParam: 0,
426 getNextPageParam: () => 12,
427 })
428​
429 queryClient.fetchInfiniteQuery(options)
430​
431 expect(
432 QueryClientOrigin.prototype.fetchInfiniteQuery,
433 ).toHaveBeenCalledWith(
434 expect.objectContaining({
435 initialPageParam: 0,
436 queryKey: queryKeyUnref,
437 }),
438 )
439 })
440 })
441​
442 describe('infiniteQuery', () => {
443 it('should properly unwrap queryKey, initialPageParam, pages, and select', () => {
444 const queryClient = new QueryClient()
445 const getNextPageParam = () => 1
446 const select = (data: { pages: Array<string> }) => data.pages.length
447​
448 queryClient.infiniteQuery({
449 queryKey: queryKeyRef,
450 initialPageParam: ref(0),
451 pages: ref(2),
452 getNextPageParam: ref(getNextPageParam),
453 select: ref(select),
454 })
455​
456 expect(QueryClientOrigin.prototype.infiniteQuery).toBeCalledWith(
457 expect.objectContaining({
458 queryKey: queryKeyUnref,
459 initialPageParam: 0,
460 pages: 2,
461 getNextPageParam,
462 select,
463 }),
464 )
465 })
466​
467 it('should properly unwrap getNextPageParam when using infiniteQueryOptions', () => {
468 const queryClient = new QueryClient()
469 const getNextPageParam = () => 12
470​
471 const options = infiniteQueryOptions({
472 queryKey: queryKeyRef,
473 initialPageParam: ref(0),
474 getNextPageParam: ref(getNextPageParam),
475 })
476​
477 queryClient.infiniteQuery({
478 ...unref(options),
479 staleTime: 0,
480 pages: 1,
481 })
482​
483 expect(QueryClientOrigin.prototype.infiniteQuery).toHaveBeenCalledWith(
484 expect.objectContaining({
485 queryKey: queryKeyUnref,
486 initialPageParam: 0,
487 pages: 1,
488 getNextPageParam,
489 }),
490 )
491 })
492 })
493​
494 describe('prefetchInfiniteQuery', () => {
495 it('should properly unwrap parameters', () => {
496 const queryClient = new QueryClient()
497​
498 queryClient.prefetchInfiniteQuery({
499 queryKey: queryKeyRef,
500 queryFn: fn,
501 initialPageParam: 0,
502 })
503​
504 expect(
505 QueryClientOrigin.prototype.prefetchInfiniteQuery,
506 ).toHaveBeenCalledWith({
507 initialPageParam: 0,
508 queryKey: queryKeyUnref,
509 queryFn: fn,
510 })
511 })
512 })
513​
514 describe('setDefaultOptions', () => {
515 it('should properly unwrap parameters', () => {
516 const queryClient = new QueryClient()
517​
518 queryClient.setDefaultOptions({
519 queries: {
520 enabled: ref(false),
521 },
522 })
523​
524 expect(
525 QueryClientOrigin.prototype.setDefaultOptions,
526 ).toHaveBeenCalledWith({
527 queries: {
528 enabled: false,
529 },
530 })
531 })
532 })
533​
534 describe('setQueryDefaults', () => {
535 it('should properly unwrap parameters', () => {
536 const queryClient = new QueryClient()
537​
538 queryClient.setQueryDefaults(queryKeyRef, {
539 enabled: ref(false),
540 })
541​
542 expect(QueryClientOrigin.prototype.setQueryDefaults).toHaveBeenCalledWith(
543 queryKeyUnref,
544 {
545 enabled: false,
546 },
547 )
548 })
549 })
550​
551 describe('getQueryDefaults', () => {
552 it('should properly unwrap parameters', () => {
553 const queryClient = new QueryClient()
554​
555 queryClient.getQueryDefaults(queryKeyRef)
556​
557 expect(QueryClientOrigin.prototype.getQueryDefaults).toHaveBeenCalledWith(
558 queryKeyUnref,
559 )
560 })
561 })
562​
563 describe('setMutationDefaults', () => {
564 it('should properly unwrap parameters', () => {
565 const queryClient = new QueryClient()
566​
567 queryClient.setMutationDefaults(queryKeyRef, {
568 mutationKey: queryKeyRef,
569 })
570​
571 expect(
572 QueryClientOrigin.prototype.setMutationDefaults,
573 ).toHaveBeenCalledWith(queryKeyUnref, {
574 mutationKey: queryKeyUnref,
575 })
576 })
577 })
578​
579 describe('getMutationDefaults', () => {
580 it('should properly unwrap parameters', () => {
581 const queryClient = new QueryClient()
582​
583 queryClient.getMutationDefaults(queryKeyRef)
584​
585 expect(
586 QueryClientOrigin.prototype.getMutationDefaults,
587 ).toHaveBeenCalledWith(queryKeyUnref)
588 })
589 })
590})