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).toBeCalledWith({
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).toBeCalledWith({
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).toBeCalledWith(
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).toBeCalledWith({
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).toBeCalledWith({
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).toBeCalledWith({
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).toBeCalledWith(
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).toBeCalledWith(
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).toBeCalledWith(
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).toBeCalledWith(
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).toBeCalledWith({
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).toBeCalledWith(
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).toBeCalledWith(
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(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
248 {
249 queryKey: queryKeyUnref,
250 refetchType: 'none',
251 },
252 { cancelRefetch: false },
253 )
254 })
255β
256 // #7694
257 it('should call invalidateQueries immediately and refetchQueries after sleep', async () => {
258 const invalidateQueries = vi.spyOn(
259 QueryClientOrigin.prototype,
260 'invalidateQueries',
261 )
262 const refetchQueries = vi.spyOn(
263 QueryClientOrigin.prototype,
264 'refetchQueries',
265 )
266β
267 const queryClient = new QueryClient()
268β
269 queryClient.invalidateQueries({
270 queryKey: queryKeyRef,
271 })
272β
273 expect(invalidateQueries).toBeCalled()
274 expect(refetchQueries).not.toBeCalled()
275β
276 await vi.advanceTimersByTimeAsync(0)
277β
278 expect(refetchQueries).toBeCalled()
279 })
280β
281 it('should call invalidateQueries immediately and not call refetchQueries', async () => {
282 const invalidateQueries = vi.spyOn(
283 QueryClientOrigin.prototype,
284 'invalidateQueries',
285 )
286 const refetchQueries = vi.spyOn(
287 QueryClientOrigin.prototype,
288 'refetchQueries',
289 )
290β
291 const queryClient = new QueryClient()
292β
293 queryClient.invalidateQueries({
294 queryKey: queryKeyRef,
295 refetchType: 'none',
296 })
297β
298 expect(invalidateQueries).toBeCalled()
299 expect(refetchQueries).not.toBeCalled()
300β
301 await vi.advanceTimersByTimeAsync(0)
302β
303 expect(refetchQueries).not.toBeCalled()
304 })
305 })
306β
307 describe('refetchQueries', () => {
308 it('should properly unwrap 2 parameter', () => {
309 const queryClient = new QueryClient()
310β
311 queryClient.refetchQueries(
312 {
313 queryKey: queryKeyRef,
314 },
315 { cancelRefetch: ref(false) },
316 )
317β
318 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
319 {
320 queryKey: queryKeyUnref,
321 },
322 { cancelRefetch: false },
323 )
324 })
325 })
326β
327 describe('fetchQuery', () => {
328 it('should properly unwrap parameter', () => {
329 const queryClient = new QueryClient()
330β
331 queryClient.fetchQuery({
332 queryKey: queryKeyRef,
333 })
334β
335 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
336 queryKey: queryKeyUnref,
337 })
338 })
339 })
340β
341 describe('prefetchQuery', () => {
342 it('should properly unwrap parameters', () => {
343 const queryClient = new QueryClient()
344β
345 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
346β
347 expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith({
348 queryKey: queryKeyUnref,
349 queryFn: fn,
350 })
351 })
352 })
353β
354 describe('fetchInfiniteQuery', () => {
355 it('should properly unwrap parameter', () => {
356 const queryClient = new QueryClient()
357β
358 queryClient.fetchInfiniteQuery({
359 queryKey: queryKeyRef,
360 initialPageParam: 0,
361 })
362β
363 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith(
364 expect.objectContaining({
365 initialPageParam: 0,
366 queryKey: queryKeyUnref,
367 }),
368 )
369 })
370 it('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
371 const queryClient = new QueryClient()
372β
373 const options = infiniteQueryOptions({
374 queryKey: queryKeyUnref,
375 initialPageParam: 0,
376 getNextPageParam: () => 12,
377 })
378β
379 queryClient.fetchInfiniteQuery(options)
380β
381 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith(
382 expect.objectContaining({
383 initialPageParam: 0,
384 queryKey: queryKeyUnref,
385 }),
386 )
387 })
388 })
389β
390 describe('prefetchInfiniteQuery', () => {
391 it('should properly unwrap parameters', () => {
392 const queryClient = new QueryClient()
393β
394 queryClient.prefetchInfiniteQuery({
395 queryKey: queryKeyRef,
396 queryFn: fn,
397 initialPageParam: 0,
398 })
399β
400 expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({
401 initialPageParam: 0,
402 queryKey: queryKeyUnref,
403 queryFn: fn,
404 })
405 })
406 })
407β
408 describe('setDefaultOptions', () => {
409 it('should properly unwrap parameters', () => {
410 const queryClient = new QueryClient()
411β
412 queryClient.setDefaultOptions({
413 queries: {
414 enabled: ref(false),
415 },
416 })
417β
418 expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
419 queries: {
420 enabled: false,
421 },
422 })
423 })
424 })
425β
426 describe('setQueryDefaults', () => {
427 it('should properly unwrap parameters', () => {
428 const queryClient = new QueryClient()
429β
430 queryClient.setQueryDefaults(queryKeyRef, {
431 enabled: ref(false),
432 })
433β
434 expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
435 queryKeyUnref,
436 {
437 enabled: false,
438 },
439 )
440 })
441 })
442β
443 describe('getQueryDefaults', () => {
444 it('should properly unwrap parameters', () => {
445 const queryClient = new QueryClient()
446β
447 queryClient.getQueryDefaults(queryKeyRef)
448β
449 expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
450 queryKeyUnref,
451 )
452 })
453 })
454β
455 describe('setMutationDefaults', () => {
456 it('should properly unwrap parameters', () => {
457 const queryClient = new QueryClient()
458β
459 queryClient.setMutationDefaults(queryKeyRef, {
460 mutationKey: queryKeyRef,
461 })
462β
463 expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
464 queryKeyUnref,
465 {
466 mutationKey: queryKeyUnref,
467 },
468 )
469 })
470 })
471β
472 describe('getMutationDefaults', () => {
473 it('should properly unwrap parameters', () => {
474 const queryClient = new QueryClient()
475β
476 queryClient.getMutationDefaults(queryKeyRef)
477β
478 expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
479 queryKeyUnref,
480 )
481 })
482 })
483})