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

queryClient.test.ts

433 lines10.8 KB

1import { 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'
6import { flushPromises } from './test-utils'
7​
8vi.mock('@tanstack/query-core')
9​
10const queryKeyRef = ['foo', ref('bar')]
11const queryKeyUnref = ['foo', 'bar']
12​
13const fn = () => 'mock'
14​
15describe('QueryCache', () => {
16 describe('isFetching', () => {
17 test('should properly unwrap 1 parameter', async () => {
18 const queryClient = new QueryClient()
19​
20 queryClient.isFetching({
21 queryKey: queryKeyRef,
22 })
23​
24 expect(QueryClientOrigin.prototype.isFetching).toBeCalledWith({
25 queryKey: queryKeyUnref,
26 })
27 })
28 })
29​
30 describe('isMutating', () => {
31 test('should properly unwrap 1 parameter', async () => {
32 const queryClient = new QueryClient()
33​
34 queryClient.isMutating({
35 mutationKey: queryKeyRef,
36 })
37​
38 expect(QueryClientOrigin.prototype.isMutating).toBeCalledWith({
39 mutationKey: queryKeyUnref,
40 })
41 })
42 })
43​
44 describe('getQueryData', () => {
45 test('should properly unwrap 1 parameter', async () => {
46 const queryClient = new QueryClient()
47​
48 queryClient.getQueryData(queryKeyRef)
49​
50 expect(QueryClientOrigin.prototype.getQueryData).toBeCalledWith(
51 queryKeyUnref,
52 )
53 })
54 })
55​
56 describe('getQueriesData', () => {
57 test('should properly unwrap queryKey param', async () => {
58 const queryClient = new QueryClient()
59​
60 queryClient.getQueriesData({ queryKey: queryKeyRef })
61​
62 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
63 queryKey: queryKeyUnref,
64 })
65 })
66​
67 test('should properly unwrap filters param', async () => {
68 const queryClient = new QueryClient()
69​
70 queryClient.getQueriesData({ queryKey: queryKeyRef })
71​
72 expect(QueryClientOrigin.prototype.getQueriesData).toBeCalledWith({
73 queryKey: queryKeyUnref,
74 })
75 })
76 })
77​
78 describe('setQueryData', () => {
79 test('should properly unwrap 3 parameter', async () => {
80 const queryClient = new QueryClient()
81​
82 queryClient.setQueryData(queryKeyRef, fn, {
83 updatedAt: ref(3),
84 })
85​
86 expect(QueryClientOrigin.prototype.setQueryData).toBeCalledWith(
87 queryKeyUnref,
88 fn,
89 { updatedAt: 3 },
90 )
91 })
92 })
93​
94 describe('setQueriesData', () => {
95 test('should properly unwrap params with queryKey', async () => {
96 const queryClient = new QueryClient()
97​
98 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
99 updatedAt: ref(3),
100 })
101​
102 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
103 { queryKey: queryKeyUnref },
104 fn,
105 { updatedAt: 3 },
106 )
107 })
108​
109 test('should properly unwrap params with filters', async () => {
110 const queryClient = new QueryClient()
111​
112 queryClient.setQueriesData({ queryKey: queryKeyRef }, fn, {
113 updatedAt: ref(3),
114 })
115​
116 expect(QueryClientOrigin.prototype.setQueriesData).toBeCalledWith(
117 { queryKey: queryKeyUnref },
118 fn,
119 { updatedAt: 3 },
120 )
121 })
122 })
123​
124 describe('getQueryState', () => {
125 test('should properly unwrap 1 parameter', async () => {
126 const queryClient = new QueryClient()
127​
128 queryClient.getQueryState(queryKeyRef)
129​
130 expect(QueryClientOrigin.prototype.getQueryState).toBeCalledWith(
131 queryKeyUnref,
132 )
133 })
134 })
135​
136 describe('removeQueries', () => {
137 test('should properly unwrap 1 parameter', async () => {
138 const queryClient = new QueryClient()
139​
140 queryClient.removeQueries({
141 queryKey: queryKeyRef,
142 })
143​
144 expect(QueryClientOrigin.prototype.removeQueries).toBeCalledWith({
145 queryKey: queryKeyUnref,
146 })
147 })
148 })
149​
150 describe('resetQueries', () => {
151 test('should properly unwrap 2 parameter', async () => {
152 const queryClient = new QueryClient()
153​
154 queryClient.resetQueries(
155 {
156 queryKey: queryKeyRef,
157 },
158 { cancelRefetch: ref(false) },
159 )
160​
161 expect(QueryClientOrigin.prototype.resetQueries).toBeCalledWith(
162 {
163 queryKey: queryKeyUnref,
164 },
165 { cancelRefetch: false },
166 )
167 })
168 })
169​
170 describe('cancelQueries', () => {
171 test('should properly unwrap 2 parameter', async () => {
172 const queryClient = new QueryClient()
173​
174 queryClient.cancelQueries(
175 {
176 queryKey: queryKeyRef,
177 },
178 { revert: ref(false) },
179 )
180​
181 expect(QueryClientOrigin.prototype.cancelQueries).toBeCalledWith(
182 {
183 queryKey: queryKeyUnref,
184 },
185 { revert: false },
186 )
187 })
188 })
189​
190 describe('invalidateQueries', () => {
191 test('should properly unwrap 2 parameter', () => {
192 const queryClient = new QueryClient()
193​
194 queryClient.invalidateQueries(
195 {
196 queryKey: queryKeyRef,
197 },
198 { cancelRefetch: ref(false) },
199 )
200​
201 expect(QueryClientOrigin.prototype.invalidateQueries).toBeCalledWith(
202 {
203 queryKey: queryKeyUnref,
204 refetchType: 'none',
205 },
206 { cancelRefetch: false },
207 )
208 })
209​
210 // #7694
211 test('should call invalidateQueries immediately and refetchQueries after flushPromises', async () => {
212 const invalidateQueries = vi.spyOn(
213 QueryClientOrigin.prototype,
214 'invalidateQueries',
215 )
216 const refetchQueries = vi.spyOn(
217 QueryClientOrigin.prototype,
218 'refetchQueries',
219 )
220​
221 const queryClient = new QueryClient()
222​
223 queryClient.invalidateQueries({
224 queryKey: queryKeyRef,
225 })
226​
227 expect(invalidateQueries).toBeCalled()
228 expect(refetchQueries).not.toBeCalled()
229​
230 await flushPromises()
231​
232 expect(refetchQueries).toBeCalled()
233 })
234​
235 test('should call invalidateQueries immediately and not call refetchQueries', async () => {
236 const invalidateQueries = vi.spyOn(
237 QueryClientOrigin.prototype,
238 'invalidateQueries',
239 )
240 const refetchQueries = vi.spyOn(
241 QueryClientOrigin.prototype,
242 'refetchQueries',
243 )
244​
245 const queryClient = new QueryClient()
246​
247 queryClient.invalidateQueries({
248 queryKey: queryKeyRef,
249 refetchType: 'none',
250 })
251​
252 expect(invalidateQueries).toBeCalled()
253 expect(refetchQueries).not.toBeCalled()
254​
255 await flushPromises()
256​
257 expect(refetchQueries).not.toBeCalled()
258 })
259 })
260​
261 describe('refetchQueries', () => {
262 test('should properly unwrap 2 parameter', async () => {
263 const queryClient = new QueryClient()
264​
265 queryClient.refetchQueries(
266 {
267 queryKey: queryKeyRef,
268 },
269 { cancelRefetch: ref(false) },
270 )
271​
272 expect(QueryClientOrigin.prototype.refetchQueries).toBeCalledWith(
273 {
274 queryKey: queryKeyUnref,
275 },
276 { cancelRefetch: false },
277 )
278 })
279 })
280​
281 describe('fetchQuery', () => {
282 test('should properly unwrap parameter', async () => {
283 const queryClient = new QueryClient()
284​
285 queryClient.fetchQuery({
286 queryKey: queryKeyRef,
287 })
288​
289 expect(QueryClientOrigin.prototype.fetchQuery).toBeCalledWith({
290 queryKey: queryKeyUnref,
291 })
292 })
293 })
294​
295 describe('prefetchQuery', () => {
296 test('should properly unwrap parameters', async () => {
297 const queryClient = new QueryClient()
298​
299 queryClient.prefetchQuery({ queryKey: queryKeyRef, queryFn: fn })
300​
301 expect(QueryClientOrigin.prototype.prefetchQuery).toBeCalledWith({
302 queryKey: queryKeyUnref,
303 queryFn: fn,
304 })
305 })
306 })
307​
308 describe('fetchInfiniteQuery', () => {
309 test('should properly unwrap parameter', async () => {
310 const queryClient = new QueryClient()
311​
312 queryClient.fetchInfiniteQuery({
313 queryKey: queryKeyRef,
314 initialPageParam: 0,
315 })
316​
317 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
318 initialPageParam: 0,
319 queryKey: queryKeyUnref,
320 })
321 })
322 test('should properly unwrap parameter using infiniteQueryOptions with unref', async () => {
323 const queryClient = new QueryClient()
324​
325 const options = infiniteQueryOptions({
326 queryKey: queryKeyUnref,
327 initialPageParam: 0,
328 getNextPageParam: () => 12,
329 })
330​
331 queryClient.fetchInfiniteQuery(options)
332​
333 expect(QueryClientOrigin.prototype.fetchInfiniteQuery).toBeCalledWith({
334 initialPageParam: 0,
335 queryKey: queryKeyUnref,
336 })
337 })
338 })
339​
340 describe('prefetchInfiniteQuery', () => {
341 test('should properly unwrap parameters', async () => {
342 const queryClient = new QueryClient()
343​
344 queryClient.prefetchInfiniteQuery({
345 queryKey: queryKeyRef,
346 queryFn: fn,
347 initialPageParam: 0,
348 })
349​
350 expect(QueryClientOrigin.prototype.prefetchInfiniteQuery).toBeCalledWith({
351 initialPageParam: 0,
352 queryKey: queryKeyUnref,
353 queryFn: fn,
354 })
355 })
356 })
357​
358 describe('setDefaultOptions', () => {
359 test('should properly unwrap parameters', async () => {
360 const queryClient = new QueryClient()
361​
362 queryClient.setDefaultOptions({
363 queries: {
364 enabled: ref(false),
365 },
366 })
367​
368 expect(QueryClientOrigin.prototype.setDefaultOptions).toBeCalledWith({
369 queries: {
370 enabled: false,
371 },
372 })
373 })
374 })
375​
376 describe('setQueryDefaults', () => {
377 test('should properly unwrap parameters', async () => {
378 const queryClient = new QueryClient()
379​
380 queryClient.setQueryDefaults(queryKeyRef, {
381 enabled: ref(false),
382 })
383​
384 expect(QueryClientOrigin.prototype.setQueryDefaults).toBeCalledWith(
385 queryKeyUnref,
386 {
387 enabled: false,
388 },
389 )
390 })
391 })
392​
393 describe('getQueryDefaults', () => {
394 test('should properly unwrap parameters', async () => {
395 const queryClient = new QueryClient()
396​
397 queryClient.getQueryDefaults(queryKeyRef)
398​
399 expect(QueryClientOrigin.prototype.getQueryDefaults).toBeCalledWith(
400 queryKeyUnref,
401 )
402 })
403 })
404​
405 describe('setMutationDefaults', () => {
406 test('should properly unwrap parameters', async () => {
407 const queryClient = new QueryClient()
408​
409 queryClient.setMutationDefaults(queryKeyRef, {
410 mutationKey: queryKeyRef,
411 })
412​
413 expect(QueryClientOrigin.prototype.setMutationDefaults).toBeCalledWith(
414 queryKeyUnref,
415 {
416 mutationKey: queryKeyUnref,
417 },
418 )
419 })
420 })
421​
422 describe('getMutationDefaults', () => {
423 test('should properly unwrap parameters', async () => {
424 const queryClient = new QueryClient()
425​
426 queryClient.getMutationDefaults(queryKeyRef)
427​
428 expect(QueryClientOrigin.prototype.getMutationDefaults).toBeCalledWith(
429 queryKeyUnref,
430 )
431 })
432 })
433})