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