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

queryClient.test.ts

475 lines11.7 KB

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