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