Open History to see the last commit
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})