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

queryClient.test.ts

483 lines11.8 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).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})