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

queryClient.test.ts

467 lines11.5 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​
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})