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