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

queryClient.test.ts

546 lines13.0 KB

1import { ref } from 'vue-demi'
2import { QueryClient as QueryClientOrigin } from '@tanstack/query-core'
3​
4import { QueryClient } from '../queryClient'
5​
6jest.mock('@tanstack/query-core')
7​
8const queryKeyRef = ['foo', ref('bar')]
9const queryKeyUnref = ['foo', 'bar']
10​
11const fn = () => 'mock'
12​
13describe('QueryCache', () => {
14 // beforeAll(() => {
15 // jest.spyOn(QueryCacheOrigin.prototype, "find");
16 // jest.spyOn(QueryCacheOrigin.prototype, "findAll");
17 // });
18​
19 describe('isFetching', () => {
20 test('should properly unwrap 1 parameter', async () => {
21 const queryClient = new QueryClient()
22​
23 queryClient.isFetching({
24 queryKey: queryKeyRef,
25 })
26​
27 expect(QueryClientOrigin.prototype.isFetching).toBeCalledWith({
28 queryKey: queryKeyUnref,
29 })
30 })
31​
32 test('should properly unwrap 2 parameters', async () => {
33 const queryClient = new QueryClient()
34​
35 queryClient.isFetching(queryKeyRef, {
36 queryKey: queryKeyRef,
37 })
38​
39 expect(QueryClientOrigin.prototype.isFetching).toBeCalledWith(
40 queryKeyUnref,
41 {
42 queryKey: queryKeyUnref,
43 },
44 )
45 })
46 })
47​
48 describe('isMutating', () => {
49 test('should properly unwrap 1 parameter', async () => {
50 const queryClient = new QueryClient()
51​
52 queryClient.isMutating({
53 mutationKey: queryKeyRef,
54 })
55​
56 expect(QueryClientOrigin.prototype.isMutating).toBeCalledWith({
57 mutationKey: queryKeyUnref,
58 })
59 })
60 })
61​
62 describe('getQueryData', () => {
63 test('should properly unwrap 2 parameter', async () => {
64 const queryClient = new QueryClient()
65​
66 queryClient.getQueryData(queryKeyRef, {
67 queryKey: queryKeyRef,
68 })
69​
70 expect(QueryClientOrigin.prototype.getQueryData).toBeCalledWith(
71 queryKeyUnref,
72 {
73 queryKey: queryKeyUnref,
74 },
75 )
76 })
77 })
78​
79 describe('getQueriesData', () => {
80 test('should properly unwrap queryKey param', async () => {
81 const queryClient = new QueryClient()
82​
83 queryClient.getQueriesData(queryKeyRef)
84​
85 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith(
86 queryKeyUnref,
87 )
88 })
89​
90 test('should properly unwrap filters param', async () => {
91 const queryClient = new QueryClient()
92​
93 queryClient.getQueriesData({ queryKey: queryKeyRef })
94​
95 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
96 queryKey: queryKeyUnref,
97 })
98 })
99 })
100​
101 describe('setQueryData', () => {
102 test('should properly unwrap 3 parameter', async () => {
103 const queryClient = new QueryClient()
104​
105 queryClient.setQueryData(queryKeyRef, fn, { updatedAt: ref(3) })
106​
107 expect(QueryClientOrigin.prototype.setQueryData).toBeCalledWith(
108 queryKeyUnref,
109 fn,
110 { updatedAt: 3 },
111 )
112 })
113 })
114​
115 describe('setQueriesData', () => {
116 test('should properly unwrap params with queryKey', async () => {
117 const queryClient = new QueryClient()
118​
119 queryClient.setQueriesData(queryKeyRef, fn, { updatedAt: ref(3) })
120​
121 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
122 queryKeyUnref,
123 fn,
124 { updatedAt: 3 },
125 )
126 })
127​
128 test('should properly unwrap params with filters', async () => {
129 const queryClient = new QueryClient()
130​
131 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
132 updatedAt: ref(3),
133 })
134​
135 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
136 { queryKey: queryKeyUnref },
137 fn,
138 { updatedAt: 3 },
139 )
140 })
141 })
142​
143 describe('getQueryState', () => {
144 test('should properly unwrap 2 parameter', async () => {
145 const queryClient = new QueryClient()
146​
147 queryClient.getQueryState(queryKeyRef, { queryKey: queryKeyRef })
148​
149 expect(QueryClientOrigin.prototype.getQueryState).toBeCalledWith(
150 queryKeyUnref,
151 { queryKey: queryKeyUnref },
152 )
153 })
154 })
155​
156 describe('removeQueries', () => {
157 test('should properly unwrap 1 parameter', async () => {
158 const queryClient = new QueryClient()
159​
160 queryClient.removeQueries({
161 queryKey: queryKeyRef,
162 })
163​
164 expect(QueryClientOrigin.prototype.removeQueries).toBeCalledWith({
165 queryKey: queryKeyUnref,
166 })
167 })
168​
169 test('should properly unwrap 2 parameter', async () => {
170 const queryClient = new QueryClient()
171​
172 queryClient.removeQueries(queryKeyRef, {
173 queryKey: queryKeyRef,
174 })
175​
176 expect(QueryClientOrigin.prototype.removeQueries).toBeCalledWith(
177 queryKeyUnref,
178 {
179 queryKey: queryKeyUnref,
180 },
181 )
182 })
183 })
184​
185 describe('resetQueries', () => {
186 test('should properly unwrap 2 parameter', async () => {
187 const queryClient = new QueryClient()
188​
189 queryClient.resetQueries(
190 {
191 queryKey: queryKeyRef,
192 },
193 { cancelRefetch: ref(false) },
194 )
195​
196 expect(QueryClientOrigin.prototype.resetQueries).toBeCalledWith(
197 {
198 queryKey: queryKeyUnref,
199 },
200 { cancelRefetch: false },
201 )
202 })
203​
204 test('should properly unwrap 3 parameters', async () => {
205 const queryClient = new QueryClient()
206​
207 queryClient.resetQueries(
208 queryKeyRef,
209 {
210 queryKey: queryKeyRef,
211 },
212 { cancelRefetch: ref(false) },
213 )
214​
215 expect(QueryClientOrigin.prototype.resetQueries).toBeCalledWith(
216 queryKeyUnref,
217 {
218 queryKey: queryKeyUnref,
219 },
220 { cancelRefetch: false },
221 )
222 })
223 })
224​
225 describe('cancelQueries', () => {
226 test('should properly unwrap 2 parameter', async () => {
227 const queryClient = new QueryClient()
228​
229 queryClient.cancelQueries(
230 {
231 queryKey: queryKeyRef,
232 },
233 { revert: ref(false) },
234 )
235​
236 expect(QueryClientOrigin.prototype.cancelQueries).toBeCalledWith(
237 {
238 queryKey: queryKeyUnref,
239 },
240 { revert: false },
241 )
242 })
243​
244 test('should properly unwrap 3 parameters', async () => {
245 const queryClient = new QueryClient()
246​
247 queryClient.cancelQueries(
248 queryKeyRef,
249 {
250 queryKey: queryKeyRef,
251 },
252 { revert: ref(false) },
253 )
254​
255 expect(QueryClientOrigin.prototype.cancelQueries).toBeCalledWith(
256 queryKeyUnref,
257 {
258 queryKey: queryKeyUnref,
259 },
260 { revert: false },
261 )
262 })
263 })
264​
265 describe('invalidateQueries', () => {
266 test('should properly unwrap 2 parameter', async () => {
267 const queryClient = new QueryClient()
268​
269 queryClient.invalidateQueries(
270 {
271 queryKey: queryKeyRef,
272 },
273 { cancelRefetch: ref(false) },
274 )
275​
276 expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
277 {
278 queryKey: queryKeyUnref,
279 },
280 { cancelRefetch: false },
281 )
282 })
283​
284 test('should properly unwrap 3 parameters', async () => {
285 const queryClient = new QueryClient()
286​
287 queryClient.invalidateQueries(
288 queryKeyRef,
289 {
290 queryKey: queryKeyRef,
291 },
292 { cancelRefetch: ref(false) },
293 )
294​
295 expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
296 queryKeyUnref,
297 {
298 queryKey: queryKeyUnref,
299 },
300 { cancelRefetch: false },
301 )
302 })
303 })
304​
305 describe('refetchQueries', () => {
306 test('should properly unwrap 2 parameter', async () => {
307 const queryClient = new QueryClient()
308​
309 queryClient.refetchQueries(
310 {
311 queryKey: queryKeyRef,
312 },
313 { cancelRefetch: ref(false) },
314 )
315​
316 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
317 {
318 queryKey: queryKeyUnref,
319 },
320 { cancelRefetch: false },
321 )
322 })
323​
324 test('should properly unwrap 3 parameters', async () => {
325 const queryClient = new QueryClient()
326​
327 queryClient.refetchQueries(
328 queryKeyRef,
329 {
330 queryKey: queryKeyRef,
331 },
332 { cancelRefetch: ref(false) },
333 )
334​
335 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
336 queryKeyUnref,
337 {
338 queryKey: queryKeyUnref,
339 },
340 { cancelRefetch: false },
341 )
342 })
343 })
344​
345 describe('fetchQuery', () => {
346 test('should properly unwrap 1 parameter', async () => {
347 const queryClient = new QueryClient()
348​
349 queryClient.fetchQuery({
350 queryKey: queryKeyRef,
351 })
352​
353 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
354 queryKey: queryKeyUnref,
355 })
356 })
357​
358 test('should properly unwrap 2 parameters', async () => {
359 const queryClient = new QueryClient()
360​
361 queryClient.fetchQuery(queryKeyRef, {
362 queryKey: queryKeyRef,
363 })
364​
365 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith(
366 queryKeyUnref,
367 {
368 queryKey: queryKeyUnref,
369 },
370 undefined,
371 )
372 })
373​
374 test('should properly unwrap 3 parameters', async () => {
375 const queryClient = new QueryClient()
376​
377 queryClient.fetchQuery(queryKeyRef, fn, {
378 queryKey: queryKeyRef,
379 })
380​
381 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith(
382 queryKeyUnref,
383 fn,
384 {
385 queryKey: queryKeyUnref,
386 },
387 )
388 })
389 })
390​
391 describe('prefetchQuery', () => {
392 test('should properly unwrap parameters', async () => {
393 const queryClient = new QueryClient()
394​
395 queryClient.prefetchQuery(queryKeyRef, fn, { queryKey: queryKeyRef })
396​
397 expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith(
398 queryKeyUnref,
399 fn,
400 {
401 queryKey: queryKeyUnref,
402 },
403 )
404 })
405 })
406​
407 describe('fetchInfiniteQuery', () => {
408 test('should properly unwrap 1 parameter', async () => {
409 const queryClient = new QueryClient()
410​
411 queryClient.fetchInfiniteQuery({
412 queryKey: queryKeyRef,
413 })
414​
415 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
416 queryKey: queryKeyUnref,
417 })
418 })
419​
420 test('should properly unwrap 2 parameters', async () => {
421 const queryClient = new QueryClient()
422​
423 queryClient.fetchInfiniteQuery(queryKeyRef, {
424 queryKey: queryKeyRef,
425 })
426​
427 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith(
428 queryKeyUnref,
429 {
430 queryKey: queryKeyUnref,
431 },
432 undefined,
433 )
434 })
435​
436 test('should properly unwrap 3 parameters', async () => {
437 const queryClient = new QueryClient()
438​
439 queryClient.fetchInfiniteQuery(queryKeyRef, fn, {
440 queryKey: queryKeyRef,
441 })
442​
443 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith(
444 queryKeyUnref,
445 fn,
446 {
447 queryKey: queryKeyUnref,
448 },
449 )
450 })
451 })
452​
453 describe('prefetchInfiniteQuery', () => {
454 test('should properly unwrap parameters', async () => {
455 const queryClient = new QueryClient()
456​
457 queryClient.prefetchInfiniteQuery(queryKeyRef, fn, {
458 queryKey: queryKeyRef,
459 })
460​
461 expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith(
462 queryKeyUnref,
463 fn,
464 {
465 queryKey: queryKeyUnref,
466 },
467 )
468 })
469 })
470​
471 describe('setDefaultOptions', () => {
472 test('should properly unwrap parameters', async () => {
473 const queryClient = new QueryClient()
474​
475 queryClient.setDefaultOptions({
476 queries: {
477 enabled: ref(false),
478 },
479 })
480​
481 expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
482 queries: {
483 enabled: false,
484 },
485 })
486 })
487 })
488​
489 describe('setQueryDefaults', () => {
490 test('should properly unwrap parameters', async () => {
491 const queryClient = new QueryClient()
492​
493 queryClient.setQueryDefaults(queryKeyRef, {
494 enabled: ref(false),
495 })
496​
497 expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
498 queryKeyUnref,
499 {
500 enabled: false,
501 },
502 )
503 })
504 })
505​
506 describe('getQueryDefaults', () => {
507 test('should properly unwrap parameters', async () => {
508 const queryClient = new QueryClient()
509​
510 queryClient.getQueryDefaults(queryKeyRef)
511​
512 expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
513 queryKeyUnref,
514 )
515 })
516 })
517​
518 describe('setMutationDefaults', () => {
519 test('should properly unwrap parameters', async () => {
520 const queryClient = new QueryClient()
521​
522 queryClient.setMutationDefaults(queryKeyRef, {
523 mutationKey: queryKeyRef,
524 })
525​
526 expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
527 queryKeyUnref,
528 {
529 mutationKey: queryKeyUnref,
530 },
531 )
532 })
533 })
534​
535 describe('getMutationDefaults', () => {
536 test('should properly unwrap parameters', async () => {
537 const queryClient = new QueryClient()
538​
539 queryClient.getMutationDefaults(queryKeyRef)
540​
541 expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
542 queryKeyUnref,
543 )
544 })
545 })
546})