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β
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 test('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 test('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 test('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('getQueriesData', () => {
87 test('should properly unwrap queryKey param', () => {
88 const queryClient = new QueryClient()
89β
90 queryClient.getQueriesData({ queryKey: queryKeyRef })
91β
92 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
93 queryKey: queryKeyUnref,
94 })
95 })
96β
97 test('should properly unwrap filters param', () => {
98 const queryClient = new QueryClient()
99β
100 queryClient.getQueriesData({ queryKey: queryKeyRef })
101β
102 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
103 queryKey: queryKeyUnref,
104 })
105 })
106 })
107β
108 describe('setQueryData', () => {
109 test('should properly unwrap 3 parameter', () => {
110 const queryClient = new QueryClient()
111β
112 queryClient.setQueryData(queryKeyRef, fn, {
113 updatedAt: ref(3),
114 })
115β
116 expect(QueryClientOrigin.prototype.setQueryData).toBeCalledWith(
117 queryKeyUnref,
118 fn,
119 { updatedAt: 3 },
120 )
121 })
122 })
123β
124 describe('setQueriesData', () => {
125 test('should properly unwrap params with queryKey', () => {
126 const queryClient = new QueryClient()
127β
128 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
129 updatedAt: ref(3),
130 })
131β
132 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
133 { queryKey: queryKeyUnref },
134 fn,
135 { updatedAt: 3 },
136 )
137 })
138β
139 test('should properly unwrap params with filters', () => {
140 const queryClient = new QueryClient()
141β
142 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
143 updatedAt: ref(3),
144 })
145β
146 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
147 { queryKey: queryKeyUnref },
148 fn,
149 { updatedAt: 3 },
150 )
151 })
152 })
153β
154 describe('getQueryState', () => {
155 test('should properly unwrap 1 parameter', () => {
156 const queryClient = new QueryClient()
157β
158 queryClient.getQueryState(queryKeyRef)
159β
160 expect(QueryClientOrigin.prototype.getQueryState).toBeCalledWith(
161 queryKeyUnref,
162 )
163 })
164 })
165β
166 describe('removeQueries', () => {
167 test('should properly unwrap 1 parameter', () => {
168 const queryClient = new QueryClient()
169β
170 queryClient.removeQueries({
171 queryKey: queryKeyRef,
172 })
173β
174 expect(QueryClientOrigin.prototype.removeQueries).toBeCalledWith({
175 queryKey: queryKeyUnref,
176 })
177 })
178 })
179β
180 describe('resetQueries', () => {
181 test('should properly unwrap 2 parameter', () => {
182 const queryClient = new QueryClient()
183β
184 queryClient.resetQueries(
185 {
186 queryKey: queryKeyRef,
187 },
188 { cancelRefetch: ref(false) },
189 )
190β
191 expect(QueryClientOrigin.prototype.resetQueries).toBeCalledWith(
192 {
193 queryKey: queryKeyUnref,
194 },
195 { cancelRefetch: false },
196 )
197 })
198 })
199β
200 describe('cancelQueries', () => {
201 test('should properly unwrap 2 parameter', () => {
202 const queryClient = new QueryClient()
203β
204 queryClient.cancelQueries(
205 {
206 queryKey: queryKeyRef,
207 },
208 { revert: ref(false) },
209 )
210β
211 expect(QueryClientOrigin.prototype.cancelQueries).toBeCalledWith(
212 {
213 queryKey: queryKeyUnref,
214 },
215 { revert: false },
216 )
217 })
218 })
219β
220 describe('invalidateQueries', () => {
221 test('should properly unwrap 2 parameter', () => {
222 const queryClient = new QueryClient()
223β
224 queryClient.invalidateQueries(
225 {
226 queryKey: queryKeyRef,
227 },
228 { cancelRefetch: ref(false) },
229 )
230β
231 expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
232 {
233 queryKey: queryKeyUnref,
234 refetchType: 'none',
235 },
236 { cancelRefetch: false },
237 )
238 })
239β
240 // #7694
241 test('should call invalidateQueries immediately and refetchQueries after sleep', async () => {
242 const invalidateQueries = vi.spyOn(
243 QueryClientOrigin.prototype,
244 'invalidateQueries',
245 )
246 const refetchQueries = vi.spyOn(
247 QueryClientOrigin.prototype,
248 'refetchQueries',
249 )
250β
251 const queryClient = new QueryClient()
252β
253 queryClient.invalidateQueries({
254 queryKey: queryKeyRef,
255 })
256β
257 expect(invalidateQueries).toBeCalled()
258 expect(refetchQueries).not.toBeCalled()
259β
260 await vi.advanceTimersByTimeAsync(0)
261β
262 expect(refetchQueries).toBeCalled()
263 })
264β
265 test('should call invalidateQueries immediately and not call refetchQueries', async () => {
266 const invalidateQueries = vi.spyOn(
267 QueryClientOrigin.prototype,
268 'invalidateQueries',
269 )
270 const refetchQueries = vi.spyOn(
271 QueryClientOrigin.prototype,
272 'refetchQueries',
273 )
274β
275 const queryClient = new QueryClient()
276β
277 queryClient.invalidateQueries({
278 queryKey: queryKeyRef,
279 refetchType: 'none',
280 })
281β
282 expect(invalidateQueries).toBeCalled()
283 expect(refetchQueries).not.toBeCalled()
284β
285 await vi.advanceTimersByTimeAsync(0)
286β
287 expect(refetchQueries).not.toBeCalled()
288 })
289 })
290β
291 describe('refetchQueries', () => {
292 test('should properly unwrap 2 parameter', () => {
293 const queryClient = new QueryClient()
294β
295 queryClient.refetchQueries(
296 {
297 queryKey: queryKeyRef,
298 },
299 { cancelRefetch: ref(false) },
300 )
301β
302 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
303 {
304 queryKey: queryKeyUnref,
305 },
306 { cancelRefetch: false },
307 )
308 })
309 })
310β
311 describe('fetchQuery', () => {
312 test('should properly unwrap parameter', () => {
313 const queryClient = new QueryClient()
314β
315 queryClient.fetchQuery({
316 queryKey: queryKeyRef,
317 })
318β
319 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
320 queryKey: queryKeyUnref,
321 })
322 })
323 })
324β
325 describe('prefetchQuery', () => {
326 test('should properly unwrap parameters', () => {
327 const queryClient = new QueryClient()
328β
329 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
330β
331 expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith({
332 queryKey: queryKeyUnref,
333 queryFn: fn,
334 })
335 })
336 })
337β
338 describe('fetchInfiniteQuery', () => {
339 test('should properly unwrap parameter', () => {
340 const queryClient = new QueryClient()
341β
342 queryClient.fetchInfiniteQuery({
343 queryKey: queryKeyRef,
344 initialPageParam: 0,
345 })
346β
347 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith(
348 expect.objectContaining({
349 initialPageParam: 0,
350 queryKey: queryKeyUnref,
351 }),
352 )
353 })
354 test('should properly unwrap parameter using infiniteQueryOptions with unref', () => {
355 const queryClient = new QueryClient()
356β
357 const options = infiniteQueryOptions({
358 queryKey: queryKeyUnref,
359 initialPageParam: 0,
360 getNextPageParam: () => 12,
361 })
362β
363 queryClient.fetchInfiniteQuery(options)
364β
365 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith(
366 expect.objectContaining({
367 initialPageParam: 0,
368 queryKey: queryKeyUnref,
369 }),
370 )
371 })
372 })
373β
374 describe('prefetchInfiniteQuery', () => {
375 test('should properly unwrap parameters', () => {
376 const queryClient = new QueryClient()
377β
378 queryClient.prefetchInfiniteQuery({
379 queryKey: queryKeyRef,
380 queryFn: fn,
381 initialPageParam: 0,
382 })
383β
384 expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({
385 initialPageParam: 0,
386 queryKey: queryKeyUnref,
387 queryFn: fn,
388 })
389 })
390 })
391β
392 describe('setDefaultOptions', () => {
393 test('should properly unwrap parameters', () => {
394 const queryClient = new QueryClient()
395β
396 queryClient.setDefaultOptions({
397 queries: {
398 enabled: ref(false),
399 },
400 })
401β
402 expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
403 queries: {
404 enabled: false,
405 },
406 })
407 })
408 })
409β
410 describe('setQueryDefaults', () => {
411 test('should properly unwrap parameters', () => {
412 const queryClient = new QueryClient()
413β
414 queryClient.setQueryDefaults(queryKeyRef, {
415 enabled: ref(false),
416 })
417β
418 expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
419 queryKeyUnref,
420 {
421 enabled: false,
422 },
423 )
424 })
425 })
426β
427 describe('getQueryDefaults', () => {
428 test('should properly unwrap parameters', () => {
429 const queryClient = new QueryClient()
430β
431 queryClient.getQueryDefaults(queryKeyRef)
432β
433 expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
434 queryKeyUnref,
435 )
436 })
437 })
438β
439 describe('setMutationDefaults', () => {
440 test('should properly unwrap parameters', () => {
441 const queryClient = new QueryClient()
442β
443 queryClient.setMutationDefaults(queryKeyRef, {
444 mutationKey: queryKeyRef,
445 })
446β
447 expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
448 queryKeyUnref,
449 {
450 mutationKey: queryKeyUnref,
451 },
452 )
453 })
454 })
455β
456 describe('getMutationDefaults', () => {
457 test('should properly unwrap parameters', () => {
458 const queryClient = new QueryClient()
459β
460 queryClient.getMutationDefaults(queryKeyRef)
461β
462 expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
463 queryKeyUnref,
464 )
465 })
466 })
467})