Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { onScopeDispose, ref } from 'vue-demi'
3import { queryKey, sleep } from '@tanstack/query-test-utils'
4import { useQueries } from '../useQueries'
5import { useQueryClient } from '../useQueryClient'
6import { QueryClient } from '../queryClient'
7import type { MockedFunction } from 'vitest'
8β
9vi.mock('../useQueryClient')
10β
11describe('useQueries', () => {
12 beforeEach(() => {
13 vi.clearAllMocks()
14 vi.useFakeTimers()
15 })
16β
17 afterEach(() => {
18 vi.useRealTimers()
19 })
20β
21 it('should return result for each query', () => {
22 const key1 = queryKey()
23 const key2 = queryKey()
24 const queries = [
25 {
26 queryKey: key1,
27 queryFn: () => sleep(0).then(() => 'Some data'),
28 },
29 {
30 queryKey: key2,
31 queryFn: () => sleep(0).then(() => 'Some data'),
32 },
33 ]
34 const queriesState = useQueries({ queries })
35β
36 expect(queriesState.value).toMatchObject([
37 {
38 status: 'pending',
39 isPending: true,
40 isFetching: true,
41 isStale: true,
42 },
43 {
44 status: 'pending',
45 isPending: true,
46 isFetching: true,
47 isStale: true,
48 },
49 ])
50 })
51β
52 it('should resolve to success and update reactive state', async () => {
53 const key1 = queryKey()
54 const key2 = queryKey()
55 const queries = [
56 {
57 queryKey: key1,
58 queryFn: () => sleep(0).then(() => 'Some data'),
59 },
60 {
61 queryKey: key2,
62 queryFn: () => sleep(0).then(() => 'Some data'),
63 },
64 ]
65 const queriesState = useQueries({ queries })
66β
67 await vi.advanceTimersByTimeAsync(0)
68β
69 expect(queriesState.value).toMatchObject([
70 {
71 status: 'success',
72 isPending: false,
73 isFetching: false,
74 isStale: true,
75 },
76 {
77 status: 'success',
78 isPending: false,
79 isFetching: false,
80 isStale: true,
81 },
82 ])
83 })
84β
85 it('should reject one of the queries and update reactive state', async () => {
86 const key1 = queryKey()
87 const key2 = queryKey()
88 const queries = [
89 {
90 queryKey: key1,
91 queryFn: () =>
92 sleep(0).then(() => Promise.reject(new Error('Some error'))),
93 },
94 {
95 queryKey: key2,
96 queryFn: () => sleep(0).then(() => 'Some data'),
97 },
98 ]
99 const queriesState = useQueries({ queries })
100β
101 await vi.advanceTimersByTimeAsync(0)
102β
103 expect(queriesState.value).toMatchObject([
104 {
105 status: 'error',
106 isPending: false,
107 isFetching: false,
108 isStale: true,
109 },
110 {
111 status: 'success',
112 isPending: false,
113 isFetching: false,
114 isStale: true,
115 },
116 ])
117 })
118β
119 it('should return state for new queries', async () => {
120 const key1 = queryKey()
121 const key2 = queryKey()
122 const key3 = queryKey()
123 const key4 = queryKey()
124 const queries = ref([
125 {
126 queryKey: key1,
127 queryFn: () => sleep(0).then(() => 'value31'),
128 },
129 {
130 queryKey: key2,
131 queryFn: () => sleep(0).then(() => 'value32'),
132 },
133 {
134 queryKey: key3,
135 queryFn: () => sleep(0).then(() => 'value33'),
136 },
137 ])
138 const queriesState = useQueries({ queries })
139β
140 await vi.advanceTimersByTimeAsync(0)
141β
142 queries.value.splice(
143 0,
144 queries.value.length,
145 {
146 queryKey: key1,
147 queryFn: () => sleep(0).then(() => 'value31'),
148 },
149 {
150 queryKey: key4,
151 queryFn: () => sleep(0).then(() => 'value34'),
152 },
153 )
154β
155 await vi.advanceTimersByTimeAsync(0)
156 await vi.advanceTimersByTimeAsync(0)
157β
158 expect(queriesState.value.length).toEqual(2)
159 expect(queriesState.value).toMatchObject([
160 {
161 data: 'value31',
162 status: 'success',
163 isPending: false,
164 isFetching: false,
165 isStale: true,
166 },
167 {
168 data: 'value34',
169 status: 'success',
170 isPending: false,
171 isFetching: false,
172 isStale: true,
173 },
174 ])
175 })
176β
177 it('should stop listening to changes on onScopeDispose', async () => {
178 const key1 = queryKey()
179 const key2 = queryKey()
180 const onScopeDisposeMock = onScopeDispose as MockedFunction<
181 typeof onScopeDispose
182 >
183 onScopeDisposeMock.mockImplementationOnce((fn) => fn())
184β
185 const queries = [
186 {
187 queryKey: key1,
188 queryFn: () => sleep(0).then(() => 'Some data'),
189 },
190 {
191 queryKey: key2,
192 queryFn: () => sleep(0).then(() => 'Some data'),
193 },
194 ]
195 const queriesState = useQueries({ queries })
196 await vi.advanceTimersByTimeAsync(0)
197β
198 expect(queriesState.value).toMatchObject([
199 {
200 status: 'pending',
201 isPending: true,
202 isFetching: true,
203 isStale: true,
204 },
205 {
206 status: 'pending',
207 isPending: true,
208 isFetching: true,
209 isStale: true,
210 },
211 ])
212 })
213β
214 it('should use queryClient provided via options', async () => {
215 const key1 = queryKey()
216 const key2 = queryKey()
217 const queryClient = new QueryClient()
218 const queries = [
219 {
220 queryKey: key1,
221 queryFn: () => sleep(0).then(() => 'Some data'),
222 },
223 {
224 queryKey: key2,
225 queryFn: () => sleep(0).then(() => 'Some data'),
226 },
227 ]
228β
229 useQueries({ queries }, queryClient)
230 await vi.advanceTimersByTimeAsync(0)
231β
232 expect(useQueryClient).toHaveBeenCalledTimes(0)
233 })
234β
235 it('should combine queries', async () => {
236 const key1 = queryKey()
237 const key2 = queryKey()
238 const firstResult = 'first result'
239 const secondResult = 'second result'
240β
241 const queryClient = new QueryClient()
242 const queries = [
243 {
244 queryKey: key1,
245 queryFn: () => sleep(0).then(() => firstResult),
246 },
247 {
248 queryKey: key2,
249 queryFn: () => sleep(0).then(() => secondResult),
250 },
251 ]
252β
253 const queriesResult = useQueries(
254 {
255 queries,
256 combine: (results) => {
257 return {
258 combined: true,
259 res: results.map((res) => res.data),
260 }
261 },
262 },
263 queryClient,
264 )
265 await vi.advanceTimersByTimeAsync(0)
266β
267 expect(queriesResult.value).toMatchObject({
268 combined: true,
269 res: [firstResult, secondResult],
270 })
271 })
272β
273 it('should be `enabled` to accept getter function', async () => {
274 const key = queryKey()
275 const fetchFn = vi.fn(() => 'foo')
276 const checked = ref(false)
277β
278 useQueries({
279 queries: [
280 {
281 queryKey: key,
282 queryFn: fetchFn,
283 enabled: () => checked.value,
284 },
285 ],
286 })
287β
288 expect(fetchFn).not.toHaveBeenCalled()
289β
290 checked.value = true
291β
292 await vi.advanceTimersByTimeAsync(0)
293β
294 expect(fetchFn).toHaveBeenCalled()
295 })
296β
297 it('should allow getters for query keys', async () => {
298 const key = queryKey()
299 const fetchFn = vi.fn(() => 'foo')
300 const key1 = ref('key1')
301 const key2 = ref('key2')
302β
303 useQueries({
304 queries: [
305 {
306 queryKey: [...key, () => key1.value, () => key2.value],
307 queryFn: fetchFn,
308 },
309 ],
310 })
311β
312 expect(fetchFn).toHaveBeenCalledTimes(1)
313β
314 key1.value = 'key3'
315β
316 await vi.advanceTimersByTimeAsync(0)
317β
318 expect(fetchFn).toHaveBeenCalledTimes(2)
319β
320 key2.value = 'key4'
321β
322 await vi.advanceTimersByTimeAsync(0)
323β
324 expect(fetchFn).toHaveBeenCalledTimes(3)
325 })
326β
327 it('should allow arbitrarily nested getters for query keys', async () => {
328 const key = queryKey()
329 const fetchFn = vi.fn(() => 'foo')
330 const key1 = ref('key1')
331 const key2 = ref('key2')
332 const key3 = ref('key3')
333 const key4 = ref('key4')
334 const key5 = ref('key5')
335β
336 useQueries({
337 queries: [
338 {
339 queryKey: [
340 ...key,
341 key1,
342 () => key2.value,
343 { key: () => key3.value },
344 [{ foo: { bar: () => key4.value } }],
345 () => ({
346 foo: {
347 bar: {
348 baz: () => key5.value,
349 },
350 },
351 }),
352 ],
353 queryFn: fetchFn,
354 },
355 ],
356 })
357β
358 expect(fetchFn).toHaveBeenCalledTimes(1)
359β
360 key1.value = 'key1-updated'
361β
362 await vi.advanceTimersByTimeAsync(0)
363β
364 expect(fetchFn).toHaveBeenCalledTimes(2)
365β
366 key2.value = 'key2-updated'
367β
368 await vi.advanceTimersByTimeAsync(0)
369β
370 expect(fetchFn).toHaveBeenCalledTimes(3)
371β
372 key3.value = 'key3-updated'
373β
374 await vi.advanceTimersByTimeAsync(0)
375β
376 expect(fetchFn).toHaveBeenCalledTimes(4)
377β
378 key4.value = 'key4-updated'
379β
380 await vi.advanceTimersByTimeAsync(0)
381β
382 expect(fetchFn).toHaveBeenCalledTimes(5)
383β
384 key5.value = 'key5-updated'
385β
386 await vi.advanceTimersByTimeAsync(0)
387β
388 expect(fetchFn).toHaveBeenCalledTimes(6)
389 })
390β
391 it('should refetch only the specific query without affecting others', async () => {
392 const key1 = queryKey()
393 const key2 = queryKey()
394 let userCount = 0
395 let postCount = 0
396β
397 const queriesState = useQueries({
398 queries: [
399 {
400 queryKey: key1,
401 queryFn: () => sleep(10).then(() => `users-${++userCount}`),
402 },
403 {
404 queryKey: key2,
405 queryFn: () => sleep(20).then(() => `posts-${++postCount}`),
406 },
407 ],
408 })
409β
410 await vi.advanceTimersByTimeAsync(20)
411β
412 expect(queriesState.value[0].data).toBe('users-1')
413 expect(queriesState.value[1].data).toBe('posts-1')
414β
415 queriesState.value[0].refetch()
416 await vi.advanceTimersByTimeAsync(10)
417β
418 expect(queriesState.value[0].data).toBe('users-2')
419 expect(queriesState.value[1].data).toBe('posts-1')
420 })
421β
422 it('should warn when used outside of setup function in development mode', () => {
423 vi.stubEnv('NODE_ENV', 'development')
424 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
425β
426 try {
427 useQueries({
428 queries: [
429 {
430 queryKey: queryKey(),
431 queryFn: () => sleep(0).then(() => 'data'),
432 },
433 ],
434 })
435β
436 expect(warnSpy).toHaveBeenCalledWith(
437 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
438 )
439 } finally {
440 warnSpy.mockRestore()
441 vi.unstubAllEnvs()
442 }
443 })
444β
445 it('should work with options getter and be reactive', async () => {
446 const key = queryKey()
447 const fetchFn = vi.fn(() => 'foo')
448 const key1 = ref('key1')
449 const key2 = ref('key2')
450 const key3 = ref('key3')
451 const key4 = ref('key4')
452 const key5 = ref('key5')
453β
454 useQueries({
455 queries: () => [
456 {
457 queryKey: [
458 ...key,
459 key1,
460 key2.value,
461 { key: key3.value },
462 [{ foo: { bar: key4.value } }],
463 () => ({
464 foo: {
465 bar: {
466 baz: key5.value,
467 },
468 },
469 }),
470 ],
471 queryFn: fetchFn,
472 },
473 ],
474 })
475β
476 expect(fetchFn).toHaveBeenCalledTimes(1)
477β
478 key1.value = 'key1-updated'
479β
480 await vi.advanceTimersByTimeAsync(0)
481β
482 expect(fetchFn).toHaveBeenCalledTimes(2)
483β
484 key2.value = 'key2-updated'
485β
486 await vi.advanceTimersByTimeAsync(0)
487β
488 expect(fetchFn).toHaveBeenCalledTimes(3)
489β
490 key3.value = 'key3-updated'
491β
492 await vi.advanceTimersByTimeAsync(0)
493β
494 expect(fetchFn).toHaveBeenCalledTimes(4)
495β
496 key4.value = 'key4-updated'
497β
498 await vi.advanceTimersByTimeAsync(0)
499β
500 expect(fetchFn).toHaveBeenCalledTimes(5)
501β
502 key5.value = 'key5-updated'
503β
504 await vi.advanceTimersByTimeAsync(0)
505β
506 expect(fetchFn).toHaveBeenCalledTimes(6)
507 })
508})