Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryClient.test.ts

492 lines12.1 KB

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