Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { ref } from 'vue-demi'
3import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
4import { QueryClient } from '../queryClient'
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6β
7vi.mock('@tanstack/query-core', async () => {
8 const actual = await vi.importActual<{
9 QueryClient: typeof QueryClientOrigin
10 }>('@tanstack/query-core')
11β
12 // Get the prototype methods dynamically
13 const prototypeMethods = Object.getOwnPropertyNames(
14 actual.QueryClient.prototype,
15 ).filter((prop): prop is keyof typeof actual.QueryClient.prototype => {
16 const descriptor = Object.getOwnPropertyDescriptor(
17 actual.QueryClient.prototype,
18 prop,
19 )
20 return typeof descriptor?.value === 'function' && prop !== 'constructor'
21 })
22β
23 // Spy on all methods in the prototype
24 prototypeMethods.forEach((method) => {
25 vi.spyOn(actual.QueryClient.prototype, method)
26 })
27β
28 return actual
29})
30β
31const queryKeyRef = ['foo', ref('bar')]
32const queryKeyUnref = ['foo', 'bar']
33β
34const fn = () => 'mock'
35β
36describe('QueryCache', () => {
37 beforeEach(() => {
38 vi.useFakeTimers()
39 vi.clearAllMocks()
40 })
41β
42 afterEach(() => {
43 vi.useRealTimers()
44 })
45β
46 describe('isFetching', () => {
47 it('should properly unwrap 1 parameter', () => {
48 const queryClient = new QueryClient()
49β
50 queryClient.isFetching({
51 queryKey: queryKeyRef,
52 })
53β
54 expect(QueryClientOrigin.prototype.isFetching).toHaveBeenCalledWith({
55 queryKey: queryKeyUnref,
56 })
57 })
58 })
59β
60 describe('isMutating', () => {
61 it('should properly unwrap 1 parameter', () => {
62 const queryClient = new QueryClient()
63β
64 queryClient.isMutating({
65 mutationKey: queryKeyRef,
66 })
67β
68 expect(QueryClientOrigin.prototype.isMutating).toHaveBeenCalledWith({
69 mutationKey: queryKeyUnref,
70 })
71 })
72 })
73β
74 describe('getQueryData', () => {
75 it('should properly unwrap 1 parameter', () => {
76 const queryClient = new QueryClient()
77β
78 queryClient.getQueryData(queryKeyRef)
79β
80 expect(QueryClientOrigin.prototype.getQueryData).toHaveBeenCalledWith(
81 queryKeyUnref,
82 )
83 })
84 })
85β
86 describe('ensureQueryData', () => {
87 it('should properly unwrap parameter', () => {
88 const queryClient = new QueryClient()
89β
90 queryClient.ensureQueryData({
91 queryKey: queryKeyRef,
92 queryFn: fn,
93 })
94β
95 expect(QueryClientOrigin.prototype.ensureQueryData).toHaveBeenCalledWith({
96 queryKey: queryKeyUnref,
97 queryFn: fn,
98 })
99 })
100 })
101β
102 describe('getQueriesData', () => {
103 it('should properly unwrap queryKey param', () => {
104 const queryClient = new QueryClient()
105β
106 queryClient.getQueriesData({ queryKey: queryKeyRef })
107β
108 expect(QueryClientOrigin.prototype.getQueriesData).toHaveBeenCalledWith({
109 queryKey: queryKeyUnref,
110 })
111 })
112β
113 it('should properly unwrap filters param', () => {
114 const queryClient = new QueryClient()
115β
116 queryClient.getQueriesData({ queryKey: queryKeyRef })
117β
118 expect(QueryClientOrigin.prototype.getQueriesData).toHaveBeenCalledWith({
119 queryKey: queryKeyUnref,
120 })
121 })
122 })
123β
124 describe('setQueryData', () => {
125 it('should properly unwrap 3 parameter', () => {
126 const queryClient = new QueryClient()
127β
128 queryClient.setQueryData(queryKeyRef, fn, {
129 updatedAt: ref(3),
130 })
131β
132 expect(QueryClientOrigin.prototype.setQueryData).toHaveBeenCalledWith(
133 queryKeyUnref,
134 fn,
135 { updatedAt: 3 },
136 )
137 })
138 })
139β
140 describe('setQueriesData', () => {
141 it('should properly unwrap params with queryKey', () => {
142 const queryClient = new QueryClient()
143β
144 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
145 updatedAt: ref(3),
146 })
147β
148 expect(QueryClientOrigin.prototype.setQueriesData).toHaveBeenCalledWith(
149 { queryKey: queryKeyUnref },
150 fn,
151 { updatedAt: 3 },
152 )
153 })
154β
155 it('should properly unwrap params with filters', () => {
156 const queryClient = new QueryClient()
157β
158 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
159 updatedAt: ref(3),
160 })
161β
162 expect(QueryClientOrigin.prototype.setQueriesData).toHaveBeenCalledWith(
163 { queryKey: queryKeyUnref },
164 fn,
165 { updatedAt: 3 },
166 )
167 })
168 })
169β
170 describe('getQueryState', () => {
171 it('should properly unwrap 1 parameter', () => {
172 const queryClient = new QueryClient()
173β
174 queryClient.getQueryState(queryKeyRef)
175β
176 expect(QueryClientOrigin.prototype.getQueryState).toHaveBeenCalledWith(
177 queryKeyUnref,
178 )
179 })
180 })
181β
182 describe('removeQueries', () => {
183 it('should properly unwrap 1 parameter', () => {
184 const queryClient = new QueryClient()
185β
186 queryClient.removeQueries({
187 queryKey: queryKeyRef,
188 })
189β
190 expect(QueryClientOrigin.prototype.removeQueries).toHaveBeenCalledWith({
191 queryKey: queryKeyUnref,
192 })
193 })
194 })
195β
196 describe('resetQueries', () => {
197 it('should properly unwrap 2 parameter', () => {
198 const queryClient = new QueryClient()
199β
200 queryClient.resetQueries(
201 {
202 queryKey: queryKeyRef,
203 },
204 { cancelRefetch: ref(false) },
205 )
206β
207 expect(QueryClientOrigin.prototype.resetQueries).toHaveBeenCalledWith(
208 {
209 queryKey: queryKeyUnref,
210 },
211 { cancelRefetch: false },
212 )
213 })
214 })
215β
216 describe('cancelQueries', () => {
217 it('should properly unwrap 2 parameter', () => {
218 const queryClient = new QueryClient()
219β
220 queryClient.cancelQueries(
221 {
222 queryKey: queryKeyRef,
223 },
224 { revert: ref(false) },
225 )
226β
227 expect(QueryClientOrigin.prototype.cancelQueries).toHaveBeenCalledWith(
228 {
229 queryKey: queryKeyUnref,
230 },
231 { revert: false },
232 )
233 })
234 })
235β
236 describe('invalidateQueries', () => {
237 it('should properly unwrap 2 parameter', () => {
238 const queryClient = new QueryClient()
239β
240 queryClient.invalidateQueries(
241 {
242 queryKey: queryKeyRef,
243 },
244 { cancelRefetch: ref(false) },
245 )
246β
247 expect(
248 QueryClientOrigin.prototype.invalidateQueries,
249 ).toHaveBeenCalledWith(
250 {
251 queryKey: queryKeyUnref,
252 refetchType: 'none',
253 },
254 { cancelRefetch: false },
255 )
256 })
257β
258 // #7694
259 it('should call invalidateQueries immediately and refetchQueries after sleep', async () => {
260 const invalidateQueries = vi.spyOn(
261 QueryClientOrigin.prototype,
262 'invalidateQueries',
263 )
264 const refetchQueries = vi.spyOn(
265 QueryClientOrigin.prototype,
266 'refetchQueries',
267 )
268β
269 const queryClient = new QueryClient()
270β
271 queryClient.invalidateQueries({
272 queryKey: queryKeyRef,
273 })
274β
275 expect(invalidateQueries).toHaveBeenCalled()
276 expect(refetchQueries).not.toHaveBeenCalled()
277β
278 await vi.advanceTimersByTimeAsync(0)
279β
280 expect(refetchQueries).toHaveBeenCalled()
281 })
282β
283 it('should call invalidateQueries immediately and not call refetchQueries', async () => {
284 const invalidateQueries = vi.spyOn(
285 QueryClientOrigin.prototype,
286 'invalidateQueries',
287 )
288 const refetchQueries = vi.spyOn(
289 QueryClientOrigin.prototype,
290 'refetchQueries',
291 )
292β
293 const queryClient = new QueryClient()
294β
295 queryClient.invalidateQueries({
296 queryKey: queryKeyRef,
297 refetchType: 'none',
298 })
299β
300 expect(invalidateQueries).toHaveBeenCalled()
301 expect(refetchQueries).not.toHaveBeenCalled()
302β
303 await vi.advanceTimersByTimeAsync(0)
304β
305 expect(refetchQueries).not.toHaveBeenCalled()
306 })
307 })
308β
309 describe('refetchQueries', () => {
310 it('should properly unwrap 2 parameter', () => {
311 const queryClient = new QueryClient()
312β
313 queryClient.refetchQueries(
314 {
315 queryKey: queryKeyRef,
316 },
317 { cancelRefetch: ref(false) },
318 )
319β
320 expect(QueryClientOrigin.prototype.refetchQueries).toHaveBeenCalledWith(
321 {
322 queryKey: queryKeyUnref,
323 },
324 { cancelRefetch: false },
325 )
326 })
327 })
328β
329 describe('fetchQuery', () => {
330 it('should properly unwrap parameter', () => {
331 const queryClient = new QueryClient()
332β
333 queryClient.fetchQuery({
334 queryKey: queryKeyRef,
335 })
336β
337 expect(QueryClientOrigin.prototype.fetchQuery).toHaveBeenCalledWith({
338 queryKey: queryKeyUnref,
339 })
340 })
341 })
342β
343 describe('prefetchQuery', () => {
344 it('should properly unwrap parameters', () => {
345 const queryClient = new QueryClient()
346β
347 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
348β
349 expect(QueryClientOrigin.prototype.prefetchQuery).toHaveBeenCalledWith({
350 queryKey: queryKeyUnref,
351 queryFn: fn,
352 })
353 })
354 })
355β
356 describe('fetchInfiniteQuery', () => {
357 it('should properly unwrap parameter', () => {
358 const queryClient = new QueryClient()
359β
360 queryClient.fetchInfiniteQuery({
361 queryKey: queryKeyRef,
362 initialPageParam: 0,
363 })
364β
365 expect(
366 QueryClientOrigin.prototype.fetchInfiniteQuery,
367 ).toHaveBeenCalledWith(
368 expect.objectContaining({
369 initialPageParam: 0,
370 queryKey: queryKeyUnref,
371 }),
372 )
373 })
374 it('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
375 const queryClient = new QueryClient()
376β
377 const options = infiniteQueryOptions({
378 queryKey: queryKeyUnref,
379 initialPageParam: 0,
380 getNextPageParam: () => 12,
381 })
382β
383 queryClient.fetchInfiniteQuery(options)
384β
385 expect(
386 QueryClientOrigin.prototype.fetchInfiniteQuery,
387 ).toHaveBeenCalledWith(
388 expect.objectContaining({
389 initialPageParam: 0,
390 queryKey: queryKeyUnref,
391 }),
392 )
393 })
394 })
395β
396 describe('prefetchInfiniteQuery', () => {
397 it('should properly unwrap parameters', () => {
398 const queryClient = new QueryClient()
399β
400 queryClient.prefetchInfiniteQuery({
401 queryKey: queryKeyRef,
402 queryFn: fn,
403 initialPageParam: 0,
404 })
405β
406 expect(
407 QueryClientOrigin.prototype.prefetchInfiniteQuery,
408 ).toHaveBeenCalledWith({
409 initialPageParam: 0,
410 queryKey: queryKeyUnref,
411 queryFn: fn,
412 })
413 })
414 })
415β
416 describe('setDefaultOptions', () => {
417 it('should properly unwrap parameters', () => {
418 const queryClient = new QueryClient()
419β
420 queryClient.setDefaultOptions({
421 queries: {
422 enabled: ref(false),
423 },
424 })
425β
426 expect(
427 QueryClientOrigin.prototype.setDefaultOptions,
428 ).toHaveBeenCalledWith({
429 queries: {
430 enabled: false,
431 },
432 })
433 })
434 })
435β
436 describe('setQueryDefaults', () => {
437 it('should properly unwrap parameters', () => {
438 const queryClient = new QueryClient()
439β
440 queryClient.setQueryDefaults(queryKeyRef, {
441 enabled: ref(false),
442 })
443β
444 expect(QueryClientOrigin.prototype.setQueryDefaults).toHaveBeenCalledWith(
445 queryKeyUnref,
446 {
447 enabled: false,
448 },
449 )
450 })
451 })
452β
453 describe('getQueryDefaults', () => {
454 it('should properly unwrap parameters', () => {
455 const queryClient = new QueryClient()
456β
457 queryClient.getQueryDefaults(queryKeyRef)
458β
459 expect(QueryClientOrigin.prototype.getQueryDefaults).toHaveBeenCalledWith(
460 queryKeyUnref,
461 )
462 })
463 })
464β
465 describe('setMutationDefaults', () => {
466 it('should properly unwrap parameters', () => {
467 const queryClient = new QueryClient()
468β
469 queryClient.setMutationDefaults(queryKeyRef, {
470 mutationKey: queryKeyRef,
471 })
472β
473 expect(
474 QueryClientOrigin.prototype.setMutationDefaults,
475 ).toHaveBeenCalledWith(queryKeyUnref, {
476 mutationKey: queryKeyUnref,
477 })
478 })
479 })
480β
481 describe('getMutationDefaults', () => {
482 it('should properly unwrap parameters', () => {
483 const queryClient = new QueryClient()
484β
485 queryClient.getMutationDefaults(queryKeyRef)
486β
487 expect(
488 QueryClientOrigin.prototype.getMutationDefaults,
489 ).toHaveBeenCalledWith(queryKeyUnref)
490 })
491 })
492})