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

useQuery.test.ts

843 lines23.3 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import {
3 computed,
4 isReactive,
5 isReadonly,
6 isVue3,
7 onScopeDispose,
8 reactive,
9 ref,
10} from 'vue-demi'
11import {
12 QueryObserver,
13 experimental_streamedQuery,
14 noop,
15 skipToken,
16} from '@tanstack/query-core'
17import { createKey, sleep } from '@tanstack/query-test-utils'
18import { keepPreviousData } from '..'
19import { useQuery } from '../useQuery'
20import { useBaseQuery } from '../useBaseQuery'
21import { useQueryClient } from '../useQueryClient'
22import type { Query, QueryObserverResult } from '@tanstack/query-core'
23​
24vi.mock(import('../useQueryClient'))
25vi.mock(import('../useBaseQuery'))
26​
27describe('useQuery', () => {
28 beforeEach(() => {
29 vi.useFakeTimers()
30 })
31​
32 afterEach(() => {
33 vi.useRealTimers()
34 })
35​
36 it('should properly execute query', () => {
37 const key = createKey()
38 const queryFn = () => sleep(0).then(() => 'Some data')
39​
40 useQuery({
41 queryKey: key,
42 queryFn,
43 staleTime: 1000,
44 })
45​
46 expect(useBaseQuery).toHaveBeenCalledWith(
47 QueryObserver,
48 {
49 queryKey: key,
50 queryFn,
51 staleTime: 1000,
52 },
53 undefined,
54 )
55 })
56​
57 it('should work with options getter', async () => {
58 const key = createKey()
59 const query = useQuery(() => ({
60 queryKey: key,
61 queryFn: () => sleep(0).then(() => 'result01'),
62 }))
63​
64 await vi.advanceTimersByTimeAsync(0)
65 expect(query).toMatchObject({
66 status: { value: 'success' },
67 data: { value: 'result01' },
68 isPending: { value: false },
69 isFetching: { value: false },
70 isFetched: { value: true },
71 isSuccess: { value: true },
72 })
73 })
74​
75 it('should work with options getter and be reactive', async () => {
76 const key = createKey()
77 const keyRef = ref('key011')
78 const resultRef = ref('result02')
79 const query = useQuery(() => ({
80 queryKey: [...key, keyRef.value],
81 queryFn: () => sleep(0).then(() => resultRef.value),
82 }))
83​
84 await vi.advanceTimersByTimeAsync(0)
85 expect(query).toMatchObject({
86 status: { value: 'success' },
87 data: { value: 'result02' },
88 isPending: { value: false },
89 isFetching: { value: false },
90 isFetched: { value: true },
91 isSuccess: { value: true },
92 })
93​
94 resultRef.value = 'result021'
95 keyRef.value = 'key012'
96 await vi.advanceTimersByTimeAsync(0)
97 expect(query).toMatchObject({
98 status: { value: 'success' },
99 data: { value: 'result021' },
100 isPending: { value: false },
101 isFetching: { value: false },
102 isFetched: { value: true },
103 isSuccess: { value: true },
104 })
105 })
106​
107 it('should return pending status initially', () => {
108 const key = createKey()
109 const query = useQuery({
110 queryKey: key,
111 queryFn: () => sleep(0).then(() => 'Some data'),
112 })
113​
114 expect(query).toMatchObject({
115 status: { value: 'pending' },
116 isPending: { value: true },
117 isFetching: { value: true },
118 isStale: { value: true },
119 })
120 })
121​
122 it('should resolve to success and update reactive state: useQuery(key, dataFn)', async () => {
123 const key = createKey()
124 const query = useQuery({
125 queryKey: key,
126 queryFn: () => sleep(0).then(() => 'result2'),
127 })
128​
129 await vi.advanceTimersByTimeAsync(0)
130 expect(query).toMatchObject({
131 status: { value: 'success' },
132 data: { value: 'result2' },
133 isPending: { value: false },
134 isFetching: { value: false },
135 isFetched: { value: true },
136 isSuccess: { value: true },
137 })
138 })
139​
140 it('should resolve to success and update reactive state: useQuery(optionsObj)', async () => {
141 const key = createKey()
142 const query = useQuery({
143 queryKey: key,
144 queryFn: () => sleep(0).then(() => 'result31'),
145 enabled: true,
146 })
147​
148 await vi.advanceTimersByTimeAsync(0)
149 expect(query).toMatchObject({
150 status: { value: 'success' },
151 data: { value: 'result31' },
152 isPending: { value: false },
153 isFetching: { value: false },
154 isFetched: { value: true },
155 isSuccess: { value: true },
156 })
157 })
158​
159 it('should resolve to success and update reactive state: useQuery(key, optionsObj)', async () => {
160 const key = createKey()
161 const query = useQuery({
162 queryKey: key,
163 queryFn: () => sleep(0).then(() => 'result32'),
164 enabled: true,
165 })
166​
167 await vi.advanceTimersByTimeAsync(0)
168 expect(query).toMatchObject({
169 status: { value: 'success' },
170 data: { value: 'result32' },
171 isPending: { value: false },
172 isFetching: { value: false },
173 isFetched: { value: true },
174 isSuccess: { value: true },
175 })
176 })
177​
178 it('should reject and update reactive state', async () => {
179 const key = createKey()
180 const query = useQuery({
181 queryKey: key,
182 queryFn: () =>
183 sleep(0).then(() => Promise.reject(new Error('Some error'))),
184 })
185​
186 await vi.advanceTimersByTimeAsync(0)
187 expect(query).toMatchObject({
188 status: { value: 'error' },
189 data: { value: undefined },
190 error: { value: { message: 'Some error' } },
191 isPending: { value: false },
192 isFetching: { value: false },
193 isFetched: { value: true },
194 isError: { value: true },
195 failureCount: { value: 1 },
196 failureReason: { value: { message: 'Some error' } },
197 })
198 })
199​
200 it('should update query on reactive (Ref) key change', async () => {
201 const key = createKey()
202 const secondKeyRef = ref('key7')
203 const query = useQuery({
204 queryKey: [...key, secondKeyRef],
205 queryFn: () => sleep(10).then(() => 'Some data'),
206 })
207​
208 await vi.advanceTimersByTimeAsync(10)
209 expect(query).toMatchObject({
210 status: { value: 'success' },
211 })
212​
213 secondKeyRef.value = 'key8'
214 await vi.advanceTimersByTimeAsync(0)
215 expect(query).toMatchObject({
216 status: { value: 'pending' },
217 data: { value: undefined },
218 })
219 await vi.advanceTimersByTimeAsync(10)
220 expect(query).toMatchObject({
221 status: { value: 'success' },
222 })
223 })
224​
225 it("should update query when an option is passed as Ref and it's changed", async () => {
226 const key = createKey()
227 const enabled = ref(false)
228 const query = useQuery({
229 queryKey: key,
230 queryFn: () => sleep(10).then(() => 'Some data'),
231 enabled,
232 })
233​
234 await vi.advanceTimersByTimeAsync(0)
235 expect(query).toMatchObject({
236 fetchStatus: { value: 'idle' },
237 data: { value: undefined },
238 })
239​
240 enabled.value = true
241 await vi.advanceTimersByTimeAsync(0)
242 expect(query).toMatchObject({
243 fetchStatus: { value: 'fetching' },
244 data: { value: undefined },
245 })
246 await vi.advanceTimersByTimeAsync(10)
247 expect(query).toMatchObject({
248 status: { value: 'success' },
249 })
250 })
251​
252 it('should properly execute dependent queries', async () => {
253 const key1 = createKey()
254 const key2 = createKey()
255 const { data } = useQuery({
256 queryKey: key1,
257 queryFn: () => sleep(0).then(() => 'Some data'),
258 })
259​
260 const enabled = computed(() => !!data.value)
261​
262 const dependentQueryFn = vi.fn<() => Promise<string>>(() =>
263 sleep(10).then(() => 'Some data'),
264 )
265 const { fetchStatus, status } = useQuery(
266 reactive({
267 queryKey: key2,
268 queryFn: dependentQueryFn,
269 enabled,
270 }),
271 )
272​
273 expect(data.value).toBeUndefined()
274 expect(fetchStatus.value).toBe('idle')
275 expect(dependentQueryFn).not.toHaveBeenCalled()
276 await vi.advanceTimersByTimeAsync(0)
277 expect(data.value).toBe('Some data')
278 expect(fetchStatus.value).toBe('fetching')
279 await vi.advanceTimersByTimeAsync(10)
280 expect(fetchStatus.value).toBe('idle')
281 expect(status.value).toBe('success')
282 expect(dependentQueryFn).toHaveBeenCalledTimes(1)
283 expect(dependentQueryFn).toHaveBeenCalledWith(
284 expect.objectContaining({ queryKey: key2 }),
285 )
286 })
287​
288 it('should stop listening to changes on onScopeDispose', async () => {
289 const key = createKey()
290 const queryClient = useQueryClient()
291 const onScopeDisposeMock = vi.mocked(onScopeDispose)
292 onScopeDisposeMock.mockImplementationOnce((fn) => fn())
293​
294 const { status } = useQuery({
295 queryKey: key,
296 queryFn: () => sleep(0).then(() => 'Some data'),
297 })
298​
299 expect(status.value).toBe('pending')
300 await vi.advanceTimersByTimeAsync(0)
301 expect(queryClient.getQueryData(key)).toBe('Some data')
302 expect(status.value).toBe('pending')
303 await vi.advanceTimersByTimeAsync(0)
304 expect(status.value).toBe('pending')
305 })
306​
307 it('should use the current value for the queryKey when refetch is called', async () => {
308 const key = createKey()
309 const queryFn = vi.fn<() => string>(() => 'foo')
310 const keyRef = ref('key11')
311 const query = useQuery({
312 queryKey: [...key, keyRef],
313 queryFn,
314 enabled: false,
315 })
316​
317 expect(queryFn).not.toHaveBeenCalled()
318​
319 await query.refetch()
320 expect(queryFn).toHaveBeenCalledTimes(1)
321 expect(queryFn).toHaveBeenNthCalledWith(
322 1,
323 expect.objectContaining({
324 queryKey: [...key, 'key11'],
325 }),
326 )
327​
328 keyRef.value = 'key12'
329 await query.refetch()
330 expect(queryFn).toHaveBeenCalledTimes(2)
331 expect(queryFn).toHaveBeenNthCalledWith(
332 2,
333 expect.objectContaining({
334 queryKey: [...key, 'key12'],
335 }),
336 )
337 })
338​
339 it.runIf(isVue3)(
340 'should return deeply reactive and readonly data by default',
341 async () => {
342 const key = createKey()
343 const { data } = useQuery({
344 queryKey: key,
345 queryFn: () => sleep(10).then(() => ({ nested: { count: 0 } })),
346 })
347​
348 await vi.advanceTimersByTimeAsync(10)
349 expect(data.value).toStrictEqual({ nested: { count: 0 } })
350 expect(isReactive(data.value?.nested)).toBe(true)
351 expect(isReadonly(data.value?.nested)).toBe(true)
352 },
353 )
354​
355 it('should return data in a shallow ref when shallow is true', async () => {
356 const key = createKey()
357 const { data } = useQuery({
358 queryKey: key,
359 queryFn: () => sleep(10).then(() => ({ nested: { count: 0 } })),
360 shallow: true,
361 })
362​
363 await vi.advanceTimersByTimeAsync(10)
364 expect(data.value).toStrictEqual({ nested: { count: 0 } })
365 expect(isReactive(data.value?.nested)).toBe(false)
366 expect(isReadonly(data.value?.nested)).toBe(false)
367 })
368​
369 it('should be `enabled` to accept getter function', async () => {
370 const key = createKey()
371 const queryFn = vi.fn<() => string>(() => 'foo')
372 const checked = ref(false)
373​
374 useQuery({
375 queryKey: key,
376 queryFn,
377 enabled: () => checked.value,
378 })
379​
380 expect(queryFn).not.toHaveBeenCalled()
381​
382 checked.value = true
383 await vi.advanceTimersByTimeAsync(0)
384 expect(queryFn).toHaveBeenCalledWith(
385 expect.objectContaining({ queryKey: key }),
386 )
387 })
388​
389 it('should allow getters for query keys', async () => {
390 const key = createKey()
391 const queryFn = vi.fn<() => string>(() => 'foo')
392 const key1 = ref('key1')
393 const key2 = ref('key2')
394​
395 useQuery({
396 queryKey: [...key, () => key1.value, () => key2.value],
397 queryFn,
398 })
399​
400 expect(queryFn).toHaveBeenCalledTimes(1)
401​
402 key1.value = 'key3'
403 await vi.advanceTimersByTimeAsync(0)
404 expect(queryFn).toHaveBeenCalledTimes(2)
405​
406 key2.value = 'key4'
407 await vi.advanceTimersByTimeAsync(0)
408 expect(queryFn).toHaveBeenCalledTimes(3)
409 })
410​
411 it('should allow arbitrarily nested getters for query keys', async () => {
412 const key = createKey()
413 const queryFn = vi.fn<() => string>(() => 'foo')
414 const key1 = ref('key1')
415 const key2 = ref('key2')
416 const key3 = ref('key3')
417 const key4 = ref('key4')
418 const key5 = ref('key5')
419​
420 useQuery({
421 queryKey: [
422 ...key,
423 key1,
424 () => key2.value,
425 { key: () => key3.value },
426 [{ foo: { bar: () => key4.value } }],
427 () => ({
428 foo: {
429 bar: {
430 baz: () => key5.value,
431 },
432 },
433 }),
434 ],
435 queryFn,
436 })
437​
438 expect(queryFn).toHaveBeenCalledTimes(1)
439​
440 key1.value = 'key1-updated'
441 await vi.advanceTimersByTimeAsync(0)
442 expect(queryFn).toHaveBeenCalledTimes(2)
443​
444 key2.value = 'key2-updated'
445 await vi.advanceTimersByTimeAsync(0)
446 expect(queryFn).toHaveBeenCalledTimes(3)
447​
448 key3.value = 'key3-updated'
449 await vi.advanceTimersByTimeAsync(0)
450 expect(queryFn).toHaveBeenCalledTimes(4)
451​
452 key4.value = 'key4-updated'
453 await vi.advanceTimersByTimeAsync(0)
454 expect(queryFn).toHaveBeenCalledTimes(5)
455​
456 key5.value = 'key5-updated'
457 await vi.advanceTimersByTimeAsync(0)
458 expect(queryFn).toHaveBeenCalledTimes(6)
459 })
460​
461 it('should allow a getter for the whole query key', async () => {
462 const key = createKey()
463 const queryFn = vi.fn<() => Promise<string>>(() =>
464 sleep(10).then(() => 'foo'),
465 )
466 const key1 = ref('key1')
467​
468 useQuery({
469 queryKey: () => [...key, key1.value],
470 queryFn,
471 })
472​
473 expect(queryFn).toHaveBeenCalledTimes(1)
474​
475 key1.value = 'key3'
476 await vi.advanceTimersByTimeAsync(10)
477 expect(queryFn).toHaveBeenCalledTimes(2)
478 })
479​
480 it('should derive data via select without changing what is cached', async () => {
481 const key = createKey()
482 const query = useQuery({
483 queryKey: key,
484 queryFn: () => sleep(10).then(() => ['a', 'b', 'c']),
485 select: (posts) => posts.length,
486 })
487​
488 await vi.advanceTimersByTimeAsync(10)
489 expect(query).toMatchObject({
490 status: { value: 'success' },
491 data: { value: 3 },
492 })
493​
494 const queryClient = useQueryClient()
495​
496 expect(queryClient.getQueryData(key)).toStrictEqual(['a', 'b', 'c'])
497 })
498​
499 it('should stay disabled until the dependent value is set', async () => {
500 const key = createKey()
501 const postId = ref<number>()
502 const queryFn = vi.fn<() => Promise<string>>(() =>
503 sleep(10).then(() => 'Some data'),
504 )
505​
506 const query = useQuery({
507 queryKey: [...key, postId],
508 queryFn,
509 enabled: () => postId.value != null,
510 })
511​
512 await vi.advanceTimersByTimeAsync(10)
513 expect(queryFn).not.toHaveBeenCalled()
514 expect(query).toMatchObject({ status: { value: 'pending' } })
515​
516 postId.value = 1
517 await vi.advanceTimersByTimeAsync(10)
518 expect(queryFn).toHaveBeenCalledTimes(1)
519 expect(query).toMatchObject({
520 status: { value: 'success' },
521 data: { value: 'Some data' },
522 })
523 })
524​
525 it('should skip the query while a computed queryFn resolves to skipToken, and run it once defined', async () => {
526 const key = createKey()
527 const postId = ref<number>()
528 const queryFn = vi.fn<() => Promise<string>>(() =>
529 sleep(10).then(() => 'Some data'),
530 )
531​
532 const query = useQuery({
533 queryKey: [...key, postId],
534 queryFn: computed(() => (postId.value != null ? queryFn : skipToken)),
535 })
536​
537 await vi.advanceTimersByTimeAsync(10)
538 expect(queryFn).not.toHaveBeenCalled()
539 expect(query).toMatchObject({ status: { value: 'pending' } })
540​
541 postId.value = 1
542 await vi.advanceTimersByTimeAsync(10)
543 expect(queryFn).toHaveBeenCalledTimes(1)
544 expect(query).toMatchObject({
545 status: { value: 'success' },
546 data: { value: 'Some data' },
547 })
548 })
549​
550 it('should seed from initialData and skip the loading state', () => {
551 const key = createKey()
552 const query = useQuery({
553 queryKey: key,
554 queryFn: () => sleep(10).then(() => 'data'),
555 initialData: 'initial',
556 })
557​
558 expect(query).toMatchObject({
559 status: { value: 'success' },
560 data: { value: 'initial' },
561 })
562 })
563​
564 it('should still fetch in the background and replace initialData with the fetched value', async () => {
565 const key = createKey()
566 const queryFn = vi.fn<() => Promise<string>>(() =>
567 sleep(10).then(() => 'data'),
568 )
569​
570 const query = useQuery({
571 queryKey: key,
572 queryFn,
573 initialData: 'initial',
574 })
575​
576 await vi.advanceTimersByTimeAsync(10)
577 expect(queryFn).toHaveBeenCalledTimes(1)
578 expect(query).toMatchObject({
579 status: { value: 'success' },
580 data: { value: 'data' },
581 })
582 })
583​
584 it('should keep initialData visible alongside the error when a refetch fails', async () => {
585 const key = createKey()
586​
587 const query = useQuery({
588 queryKey: key,
589 queryFn: () =>
590 sleep(10).then(() => Promise.reject(new Error('Some error'))),
591 initialData: 'initial',
592 retry: false,
593 })
594​
595 expect(query).toMatchObject({
596 status: { value: 'success' },
597 data: { value: 'initial' },
598 isError: { value: false },
599 })
600 await vi.advanceTimersByTimeAsync(10)
601 expect(query).toMatchObject({
602 status: { value: 'error' },
603 data: { value: 'initial' },
604 isError: { value: true },
605 })
606 })
607​
608 it('should keep the previous page visible while the next page loads with keepPreviousData', async () => {
609 const key = createKey()
610 const page = ref(0)
611 const queryFn = vi.fn<(pageParam: number) => Promise<string>>(
612 (pageParam: number) => sleep(10).then(() => `page-${pageParam}`),
613 )
614​
615 const query = useQuery({
616 queryKey: [...key, page],
617 queryFn: () => queryFn(page.value),
618 placeholderData: keepPreviousData,
619 })
620​
621 await vi.advanceTimersByTimeAsync(10)
622 expect(query).toMatchObject({
623 data: { value: 'page-0' },
624 isPlaceholderData: { value: false },
625 })
626​
627 page.value = 1
628 await vi.advanceTimersByTimeAsync(0)
629 expect(query).toMatchObject({
630 data: { value: 'page-0' },
631 isPlaceholderData: { value: true },
632 })
633 await vi.advanceTimersByTimeAsync(10)
634 expect(query).toMatchObject({
635 data: { value: 'page-1' },
636 isPlaceholderData: { value: false },
637 })
638 })
639​
640 describe('throwOnError', () => {
641 it('should evaluate throwOnError when query is expected to throw', async () => {
642 const key = createKey()
643 const throwOnError =
644 vi.fn<
645 (
646 error: Error,
647 query: Query<unknown, Error, unknown, Array<string>>,
648 ) => boolean
649 >()
650 useQuery({
651 queryKey: key,
652 queryFn: () =>
653 sleep(0).then(() => Promise.reject(new Error('Some error'))),
654 retry: false,
655 throwOnError,
656 })
657​
658 await vi.advanceTimersByTimeAsync(0)
659 expect(throwOnError).toHaveBeenCalledTimes(1)
660 expect(throwOnError).toHaveBeenCalledWith(
661 Error('Some error'),
662 expect.objectContaining({
663 state: expect.objectContaining({ status: 'error' }),
664 }),
665 )
666 })
667 })
668​
669 describe('outside scope warning', () => {
670 it('should warn when used outside of setup function in development mode', () => {
671 vi.stubEnv('NODE_ENV', 'development')
672 const consoleWarnMock = vi
673 .spyOn(console, 'warn')
674 .mockImplementation(() => {})
675​
676 useQuery({
677 queryKey: createKey(),
678 queryFn: () => sleep(0).then(() => 'data'),
679 })
680​
681 expect(consoleWarnMock).toHaveBeenCalledWith(
682 '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.',
683 )
684 })
685 })
686​
687 describe('suspense', () => {
688 it('should return a Promise', () => {
689 const key = createKey()
690 const query = useQuery({
691 queryKey: key,
692 queryFn: () => sleep(0).then(() => 'Some data'),
693 })
694 const result = query.suspense()
695​
696 expect(result).toBeInstanceOf(Promise)
697 })
698​
699 it('should resolve after being enabled', async () => {
700 const key = createKey()
701 const queryFn = vi.fn<() => Promise<string>>(() =>
702 sleep(10).then(() => 'Some data'),
703 )
704 const onResolve =
705 vi.fn<(result: QueryObserverResult<string, Error>) => void>()
706 const isEnabled = ref(false)
707 const query = useQuery({
708 queryKey: key,
709 queryFn,
710 enabled: isEnabled,
711 })
712​
713 query.suspense().then(onResolve)
714 await vi.advanceTimersByTimeAsync(10)
715 expect(queryFn).not.toHaveBeenCalled()
716 expect(onResolve).not.toHaveBeenCalled()
717​
718 isEnabled.value = true
719 await vi.advanceTimersByTimeAsync(10)
720 expect(queryFn).toHaveBeenCalledTimes(1)
721 expect(onResolve).toHaveBeenCalledTimes(1)
722 expect(onResolve).toHaveBeenCalledWith(
723 expect.objectContaining({ data: 'Some data' }),
724 )
725 })
726​
727 it('should resolve immediately without refetching when the data is fresh', () => {
728 const key = createKey()
729 const queryFn = vi.fn<() => Promise<string>>(() =>
730 sleep(10).then(() => 'Some data'),
731 )
732​
733 const query = useQuery({
734 queryKey: key,
735 queryFn,
736 staleTime: 10000,
737 initialData: 'foo',
738 })
739​
740 return query.suspense().then(() => {
741 expect(queryFn).toHaveBeenCalledTimes(0)
742 })
743 })
744​
745 it('should not throw from suspense by default', async () => {
746 const key = createKey()
747 const query = useQuery({
748 queryKey: key,
749 queryFn: () =>
750 sleep(10).then(() => Promise.reject(new Error('Some error'))),
751 staleTime: 10000,
752 })
753​
754 const suspensePromise = query.suspense()
755 await vi.advanceTimersByTimeAsync(10)
756 await expect(suspensePromise).resolves.toMatchObject({
757 status: 'error',
758 isError: true,
759 })
760 })
761​
762 it('should throw from suspense when throwOnError is true', async ({
763 onTestFinished,
764 }) => {
765 const key = createKey()
766 const throwOnError = vi.fn<() => boolean>().mockReturnValue(true)
767 const query = useQuery({
768 queryKey: key,
769 queryFn: () =>
770 sleep(10).then(() => Promise.reject(new Error('Some error'))),
771 staleTime: 10000,
772 throwOnError,
773 })
774​
775 // The error watcher also throws, which Vue 3 surfaces as an unhandled rejection
776 process.on('unhandledRejection', noop)
777 onTestFinished(() => {
778 process.off('unhandledRejection', noop)
779 })
780​
781 await Promise.all([
782 expect(query.suspense()).rejects.toThrow('Some error'),
783 vi.advanceTimersByTimeAsync(10),
784 ])
785 expect(throwOnError).toHaveBeenCalledTimes(2)
786 expect(throwOnError).toHaveBeenNthCalledWith(
787 1,
788 Error('Some error'),
789 expect.objectContaining({
790 state: expect.objectContaining({ status: 'error' }),
791 }),
792 )
793 expect(throwOnError).toHaveBeenNthCalledWith(
794 2,
795 Error('Some error'),
796 expect.objectContaining({
797 state: expect.objectContaining({ status: 'error' }),
798 }),
799 )
800 })
801​
802 it('should release suspense when setQueryData is called while fetch is in-flight', async () => {
803 const key = createKey()
804​
805 const query = useQuery({
806 queryKey: key,
807 queryFn: () => sleep(10000).then(() => 'fetched'),
808 })
809​
810 const suspensePromise = query.suspense()
811​
812 const queryClient = useQueryClient()
813 queryClient.setQueryData(key, 'manual data')
814​
815 await vi.advanceTimersByTimeAsync(0)
816 const result = await suspensePromise
817 expect(result.data).toBe('manual data')
818 })
819​
820 it('should release suspense when streamedQuery receives first chunk', async () => {
821 const key = createKey()
822​
823 async function* numberGenerator() {
824 await sleep(10)
825 yield 'chunk1'
826 await sleep(10)
827 yield 'chunk2'
828 }
829​
830 const query = useQuery({
831 queryKey: key,
832 queryFn: experimental_streamedQuery({
833 streamFn: () => numberGenerator(),
834 }),
835 })
836​
837 const suspensePromise = query.suspense()
838 await vi.advanceTimersByTimeAsync(10)
839 const result = await suspensePromise
840 expect(result.data).toStrictEqual(['chunk1'])
841 })
842 })
843})