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

createQueries.svelte.test.ts

934 lines25.8 KB

1import { afterEach, describe, expect, expectTypeOf, it, vi } from 'vitest'
2import { QueryClient, createQueries } from '../src/index.js'
3import { promiseWithResolvers, withEffectRoot } from './utils.svelte.js'
4import type {
5 CreateQueryOptions,
6 CreateQueryResult,
7 QueryFunction,
8 QueryFunctionContext,
9 QueryKey,
10 skipToken,
11} from '../src/index.js'
12​
13describe('createQueries', () => {
14 const queryClient = new QueryClient()
15​
16 afterEach(() => {
17 queryClient.clear()
18 })
19​
20 it(
21 'should return the correct states',
22 withEffectRoot(async () => {
23 const key1 = ['test-1']
24 const key2 = ['test-2']
25 const results: Array<Array<CreateQueryResult>> = []
26 const { promise: promise1, resolve: resolve1 } = promiseWithResolvers()
27 const { promise: promise2, resolve: resolve2 } = promiseWithResolvers()
28​
29 const result = createQueries(
30 () => ({
31 queries: [
32 {
33 queryKey: key1,
34 queryFn: () => promise1,
35 },
36 {
37 queryKey: key2,
38 queryFn: () => promise2,
39 },
40 ],
41 }),
42 () => queryClient,
43 )
44​
45 $effect(() => {
46 results.push([{ ...result[0] }, { ...result[1] }])
47 })
48​
49 resolve1(1)
50​
51 await vi.waitFor(() => expect(result[0].data).toBe(1))
52​
53 resolve2(2)
54 await vi.waitFor(() => expect(result[1].data).toBe(2))
55​
56 expect(results.length).toBe(3)
57 expect(results[0]).toMatchObject([
58 { data: undefined },
59 { data: undefined },
60 ])
61 expect(results[1]).toMatchObject([{ data: 1 }, { data: undefined }])
62 expect(results[2]).toMatchObject([{ data: 1 }, { data: 2 }])
63 }),
64 )
65​
66 it(
67 'handles type parameter - tuple of tuples',
68 withEffectRoot(() => {
69 const key1 = ['test-key-1']
70 const key2 = ['test-key-2']
71 const key3 = ['test-key-3']
72​
73 const result1 = createQueries<
74 [[number], [string], [Array<string>, boolean]]
75 >(
76 () => ({
77 queries: [
78 {
79 queryKey: key1,
80 queryFn: () => 1,
81 },
82 {
83 queryKey: key2,
84 queryFn: () => 'string',
85 },
86 {
87 queryKey: key3,
88 queryFn: () => ['string[]'],
89 },
90 ],
91 }),
92 () => queryClient,
93 )
94​
95 expectTypeOf(result1[0]).toEqualTypeOf<
96 CreateQueryResult<number, unknown>
97 >()
98 expectTypeOf(result1[1]).toEqualTypeOf<
99 CreateQueryResult<string, unknown>
100 >()
101 expectTypeOf(result1[2]).toEqualTypeOf<
102 CreateQueryResult<Array<string>, boolean>
103 >()
104 expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
105 expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
106 expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
107 expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
108​
109 // TData (3rd element) takes precedence over TQueryFnData (1st element)
110 const result2 = createQueries<
111 [[string, unknown, string], [string, unknown, number]]
112 >(
113 () => ({
114 queries: [
115 {
116 queryKey: key1,
117 queryFn: () => 'string',
118 select: (a) => {
119 expectTypeOf(a).toEqualTypeOf<string>()
120 return a.toLowerCase()
121 },
122 },
123 {
124 queryKey: key2,
125 queryFn: () => 'string',
126 select: (a) => {
127 expectTypeOf(a).toEqualTypeOf<string>()
128 return parseInt(a)
129 },
130 },
131 ],
132 }),
133 () => queryClient,
134 )
135​
136 expectTypeOf(result2[0]).toEqualTypeOf<
137 CreateQueryResult<string, unknown>
138 >()
139 expectTypeOf(result2[1]).toEqualTypeOf<
140 CreateQueryResult<number, unknown>
141 >()
142 expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
143 expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
144​
145 // types should be enforced
146 createQueries<[[string, unknown, string], [string, boolean, number]]>(
147 () => ({
148 queries: [
149 {
150 queryKey: key1,
151 queryFn: () => 'string',
152 select: (a) => {
153 expectTypeOf(a).toEqualTypeOf<string>()
154 return a.toLowerCase()
155 },
156 placeholderData: 'string',
157 // @ts-expect-error (initialData: string)
158 initialData: 123,
159 },
160 {
161 queryKey: key2,
162 queryFn: () => 'string',
163 select: (a) => {
164 expectTypeOf(a).toEqualTypeOf<string>()
165 return parseInt(a)
166 },
167 placeholderData: 'string',
168 // @ts-expect-error (initialData: string)
169 initialData: 123,
170 },
171 ],
172 }),
173 () => queryClient,
174 )
175​
176 // field names should be enforced
177 createQueries<[[string]]>(
178 () => ({
179 queries: [
180 {
181 queryKey: key1,
182 queryFn: () => 'string',
183 },
184 ],
185 }),
186 () => queryClient,
187 )
188 }),
189 )
190​
191 it(
192 'handles type parameter - tuple of objects',
193 withEffectRoot(() => {
194 const key1 = ['test-key-1']
195 const key2 = ['test-key-2']
196 const key3 = ['test-key-3']
197​
198 const result1 = createQueries<
199 [
200 { queryFnData: number },
201 { queryFnData: string },
202 { queryFnData: Array<string>; error: boolean },
203 ]
204 >(
205 () => ({
206 queries: [
207 {
208 queryKey: key1,
209 queryFn: () => 1,
210 },
211 {
212 queryKey: key2,
213 queryFn: () => 'string',
214 },
215 {
216 queryKey: key3,
217 queryFn: () => ['string[]'],
218 },
219 ],
220 }),
221 () => queryClient,
222 )
223​
224 expectTypeOf(result1[0]).toEqualTypeOf<
225 CreateQueryResult<number, unknown>
226 >()
227 expectTypeOf(result1[1]).toEqualTypeOf<
228 CreateQueryResult<string, unknown>
229 >()
230 expectTypeOf(result1[2]).toEqualTypeOf<
231 CreateQueryResult<Array<string>, boolean>
232 >()
233 expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
234 expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
235 expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
236 expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
237​
238 // TData (data prop) takes precedence over TQueryFnData (queryFnData prop)
239 const result2 = createQueries<
240 [
241 { queryFnData: string; data: string },
242 { queryFnData: string; data: number },
243 ]
244 >(
245 () => ({
246 queries: [
247 {
248 queryKey: key1,
249 queryFn: () => 'string',
250 select: (a) => {
251 expectTypeOf(a).toEqualTypeOf<string>()
252 return a.toLowerCase()
253 },
254 },
255 {
256 queryKey: key2,
257 queryFn: () => 'string',
258 select: (a) => {
259 expectTypeOf(a).toEqualTypeOf<string>()
260 return parseInt(a)
261 },
262 },
263 ],
264 }),
265 () => queryClient,
266 )
267​
268 expectTypeOf(result2[0]).toEqualTypeOf<
269 CreateQueryResult<string, unknown>
270 >()
271 expectTypeOf(result2[1]).toEqualTypeOf<
272 CreateQueryResult<number, unknown>
273 >()
274 expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
275 expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
276​
277 // can pass only TData (data prop) although TQueryFnData will be left unknown
278 const result3 = createQueries<[{ data: string }, { data: number }]>(
279 () => ({
280 queries: [
281 {
282 queryKey: key1,
283 queryFn: () => 'string',
284 select: (a) => {
285 expectTypeOf(a).toEqualTypeOf<unknown>()
286 return a as string
287 },
288 },
289 {
290 queryKey: key2,
291 queryFn: () => 'string',
292 select: (a) => {
293 expectTypeOf(a).toEqualTypeOf<unknown>()
294 return a as number
295 },
296 },
297 ],
298 }),
299 () => queryClient,
300 )
301​
302 expectTypeOf(result3[0]).toEqualTypeOf<
303 CreateQueryResult<string, unknown>
304 >()
305 expectTypeOf(result3[1]).toEqualTypeOf<
306 CreateQueryResult<number, unknown>
307 >()
308 expectTypeOf(result3[0].data).toEqualTypeOf<string | undefined>()
309 expectTypeOf(result3[1].data).toEqualTypeOf<number | undefined>()
310​
311 // types should be enforced
312 createQueries<
313 [
314 { queryFnData: string; data: string },
315 { queryFnData: string; data: number; error: boolean },
316 ]
317 >(
318 () => ({
319 queries: [
320 {
321 queryKey: key1,
322 queryFn: () => 'string',
323 select: (a) => {
324 expectTypeOf(a).toEqualTypeOf<string>()
325 return a.toLowerCase()
326 },
327 placeholderData: 'string',
328 // @ts-expect-error (initialData: string)
329 initialData: 123,
330 },
331 {
332 queryKey: key2,
333 queryFn: () => 'string',
334 select: (a) => {
335 expectTypeOf(a).toEqualTypeOf<string>()
336 return parseInt(a)
337 },
338 placeholderData: 'string',
339 // @ts-expect-error (initialData: string)
340 initialData: 123,
341 },
342 ],
343 }),
344 () => queryClient,
345 )
346​
347 // field names should be enforced
348 createQueries<[{ queryFnData: string }]>(
349 () => ({
350 queries: [
351 {
352 queryKey: key1,
353 queryFn: () => 'string',
354 },
355 ],
356 }),
357 () => queryClient,
358 )
359 }),
360 )
361​
362 it(
363 'handles array literal without type parameter to infer result type',
364 withEffectRoot(() => {
365 const key1 = ['test-key-1']
366 const key2 = ['test-key-2']
367 const key3 = ['test-key-3']
368 const key4 = ['test-key-4']
369​
370 // Array.map preserves TQueryFnData
371 const result1 = createQueries(
372 () => ({
373 queries: Array(50).map((_, i) => ({
374 queryKey: ['key', i] as const,
375 queryFn: () => i + 10,
376 })),
377 }),
378 () => queryClient,
379 )
380​
381 expectTypeOf(result1).toEqualTypeOf<
382 Array<CreateQueryResult<number, Error>>
383 >()
384 if (result1[0]) {
385 expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
386 }
387​
388 // Array.map preserves TData
389 const result2 = createQueries(
390 () => ({
391 queries: Array(50).map((_, i) => ({
392 queryKey: ['key', i] as const,
393 queryFn: () => i + 10,
394 select: (data: number) => data.toString(),
395 })),
396 }),
397 () => queryClient,
398 )
399​
400 expectTypeOf(result2).toEqualTypeOf<
401 Array<CreateQueryResult<string, Error>>
402 >()
403​
404 const result3 = createQueries(
405 () => ({
406 queries: [
407 {
408 queryKey: key1,
409 queryFn: () => 1,
410 },
411 {
412 queryKey: key2,
413 queryFn: () => 'string',
414 },
415 {
416 queryKey: key3,
417 queryFn: () => ['string[]'],
418 select: () => 123,
419 },
420 ],
421 }),
422 () => queryClient,
423 )
424​
425 expectTypeOf(result3[0]).toEqualTypeOf<CreateQueryResult<number, Error>>()
426 expectTypeOf(result3[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
427 expectTypeOf(result3[2]).toEqualTypeOf<CreateQueryResult<number, Error>>()
428 expectTypeOf(result3[0].data).toEqualTypeOf<number | undefined>()
429 expectTypeOf(result3[1].data).toEqualTypeOf<string | undefined>()
430 // select takes precedence over queryFn
431 expectTypeOf(result3[2].data).toEqualTypeOf<number | undefined>()
432​
433 // initialData/placeholderData are enforced
434 createQueries(
435 () => ({
436 queries: [
437 {
438 queryKey: key1,
439 queryFn: () => 'string',
440 placeholderData: 'string',
441 // @ts-expect-error (initialData: string)
442 initialData: 123,
443 },
444 {
445 queryKey: key2,
446 queryFn: () => 123,
447 // @ts-expect-error (placeholderData: number)
448 placeholderData: 'string',
449 initialData: 123,
450 },
451 ],
452 }),
453 () => queryClient,
454 )
455​
456 // select params are "indirectly" enforced
457 createQueries(
458 () => ({
459 queries: [
460 // unfortunately TS will not suggest the type for you
461 {
462 queryKey: key1,
463 queryFn: () => 'string',
464 },
465 // however you can add a type to the callback
466 {
467 queryKey: key2,
468 queryFn: () => 'string',
469 },
470 // the type you do pass is enforced
471 {
472 queryKey: key3,
473 queryFn: () => 'string',
474 },
475 {
476 queryKey: key4,
477 queryFn: () => 'string',
478 select: (a: string) => parseInt(a),
479 },
480 ],
481 }),
482 () => queryClient,
483 )
484​
485 // callbacks are also indirectly enforced with Array.map
486 createQueries(
487 () => ({
488 queries: Array(50).map((_, i) => ({
489 queryKey: ['key', i] as const,
490 queryFn: () => i + 10,
491 select: (data: number) => data.toString(),
492 })),
493 }),
494 () => queryClient,
495 )
496​
497 // results inference works when all the handlers are defined
498 const result4 = createQueries(
499 () => ({
500 queries: [
501 {
502 queryKey: key1,
503 queryFn: () => 'string',
504 },
505 {
506 queryKey: key2,
507 queryFn: () => 'string',
508 },
509 {
510 queryKey: key4,
511 queryFn: () => 'string',
512 select: (a: string) => parseInt(a),
513 },
514 ],
515 }),
516 () => queryClient,
517 )
518​
519 expectTypeOf(result4[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
520 expectTypeOf(result4[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
521 expectTypeOf(result4[2]).toEqualTypeOf<CreateQueryResult<number, Error>>()
522​
523 // handles when queryFn returns a Promise
524 const result5 = createQueries(
525 () => ({
526 queries: [
527 {
528 queryKey: key1,
529 queryFn: () => Promise.resolve('string'),
530 },
531 ],
532 }),
533 () => queryClient,
534 )
535​
536 expectTypeOf(result5[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
537​
538 // Array as const does not throw error
539 const result6 = createQueries(
540 () =>
541 ({
542 queries: [
543 {
544 queryKey: ['key1'],
545 queryFn: () => 'string',
546 },
547 {
548 queryKey: ['key1'],
549 queryFn: () => 123,
550 },
551 ],
552 }) as const,
553 () => queryClient,
554 )
555​
556 expectTypeOf(result6[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
557 expectTypeOf(result6[1]).toEqualTypeOf<CreateQueryResult<number, Error>>()
558​
559 // field names should be enforced - array literal
560 createQueries(
561 () => ({
562 queries: [
563 {
564 queryKey: key1,
565 queryFn: () => 'string',
566 },
567 ],
568 }),
569 () => queryClient,
570 )
571​
572 // field names should be enforced - Array.map() result
573 createQueries(
574 () => ({
575 // @ts-expect-error (invalidField)
576 queries: Array(10).map(() => ({
577 someInvalidField: '',
578 })),
579 }),
580 () => queryClient,
581 )
582​
583 // supports queryFn using fetch() to return Promise<any> - Array.map() result
584 createQueries(
585 () => ({
586 queries: Array(50).map((_, i) => ({
587 queryKey: ['key', i] as const,
588 queryFn: () =>
589 fetch('return Promise<any>').then((resp) => resp.json()),
590 })),
591 }),
592 () => queryClient,
593 )
594​
595 // supports queryFn using fetch() to return Promise<any> - array literal
596 createQueries(
597 () => ({
598 queries: [
599 {
600 queryKey: key1,
601 queryFn: () =>
602 fetch('return Promise<any>').then((resp) => resp.json()),
603 },
604 ],
605 }),
606 () => queryClient,
607 )
608 }),
609 )
610​
611 it(
612 'handles strongly typed queryFn factories and createQueries wrappers',
613 withEffectRoot(() => {
614 // QueryKey + queryFn factory
615 type QueryKeyA = ['queryA']
616 const getQueryKeyA = (): QueryKeyA => ['queryA']
617 type GetQueryFunctionA = () => QueryFunction<number, QueryKeyA>
618 const getQueryFunctionA: GetQueryFunctionA = () => () => {
619 return 1
620 }
621 type SelectorA = (data: number) => [number, string]
622 const getSelectorA = (): SelectorA => (data) => [data, data.toString()]
623​
624 type QueryKeyB = ['queryB', string]
625 const getQueryKeyB = (id: string): QueryKeyB => ['queryB', id]
626 type GetQueryFunctionB = () => QueryFunction<string, QueryKeyB>
627 const getQueryFunctionB: GetQueryFunctionB = () => () => {
628 return '1'
629 }
630 type SelectorB = (data: string) => [string, number]
631 const getSelectorB = (): SelectorB => (data) => [data, +data]
632​
633 // Wrapper with strongly typed array-parameter
634 function useWrappedQueries<
635 TQueryFnData,
636 TError,
637 TData,
638 TQueryKey extends QueryKey,
639 >(
640 queries: Array<
641 CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>
642 >,
643 ) {
644 return createQueries(
645 () => ({
646 queries: queries.map(
647 // no need to type the mapped query
648 (query) => {
649 const { queryFn: fn, queryKey: key } = query
650 expectTypeOf(fn).toEqualTypeOf<
651 | typeof skipToken
652 | QueryFunction<TQueryFnData, TQueryKey, never>
653 | undefined
654 >()
655 return {
656 queryKey: key,
657 queryFn: fn
658 ? (ctx: QueryFunctionContext<TQueryKey>) => {
659 // eslint-disable-next-line vitest/valid-expect
660 expectTypeOf<TQueryKey>(ctx.queryKey)
661 return (
662 fn as QueryFunction<TQueryFnData, TQueryKey>
663 ).call({}, ctx)
664 }
665 : undefined,
666 }
667 },
668 ),
669 }),
670 () => queryClient,
671 )
672 }
673​
674 const result = createQueries(
675 () => ({
676 queries: [
677 {
678 queryKey: getQueryKeyA(),
679 queryFn: getQueryFunctionA(),
680 },
681 {
682 queryKey: getQueryKeyB('id'),
683 queryFn: getQueryFunctionB(),
684 },
685 ],
686 }),
687 () => queryClient,
688 )
689​
690 expectTypeOf(result[0]).toEqualTypeOf<CreateQueryResult<number, Error>>()
691 expectTypeOf(result[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
692​
693 const withSelector = createQueries(
694 () => ({
695 queries: [
696 {
697 queryKey: getQueryKeyA(),
698 queryFn: getQueryFunctionA(),
699 select: getSelectorA(),
700 },
701 {
702 queryKey: getQueryKeyB('id'),
703 queryFn: getQueryFunctionB(),
704 select: getSelectorB(),
705 },
706 ],
707 }),
708 () => queryClient,
709 )
710​
711 expectTypeOf(withSelector[0]).toEqualTypeOf<
712 CreateQueryResult<[number, string], Error>
713 >()
714 expectTypeOf(withSelector[1]).toEqualTypeOf<
715 CreateQueryResult<[string, number], Error>
716 >()
717​
718 const withWrappedQueries = useWrappedQueries(
719 Array(10).map(() => ({
720 queryKey: getQueryKeyA(),
721 queryFn: getQueryFunctionA(),
722 select: getSelectorA(),
723 })),
724 )
725​
726 expectTypeOf(withWrappedQueries).toEqualTypeOf<
727 Array<CreateQueryResult<number, Error>>
728 >()
729 }),
730 )
731​
732 it(
733 'should track results',
734 withEffectRoot(async () => {
735 const key1 = ['test-track-results']
736 const results: Array<Array<CreateQueryResult>> = []
737 let count = 0
738​
739 const result = createQueries(
740 () => ({
741 queries: [
742 {
743 queryKey: key1,
744 queryFn: () => Promise.resolve(++count),
745 },
746 ],
747 }),
748 () => queryClient,
749 )
750​
751 $effect(() => {
752 results.push([result[0]])
753 })
754​
755 await vi.waitFor(() => expect(result[0].data).toBe(1))
756​
757 expect(results.length).toBe(2)
758 expect(results[0]).toMatchObject([{ data: undefined }])
759 expect(results[1]).toMatchObject([{ data: 1 }])
760​
761 // Trigger refetch
762 result[0].refetch()
763​
764 await vi.waitFor(() => expect(result[0].data).toBe(2))
765​
766 // Only one render for data update, no render for isFetching transition
767 expect(results.length).toBe(3)
768 expect(results[2]).toMatchObject([{ data: 2 }])
769 }),
770 )
771​
772 it(
773 'should combine queries',
774 withEffectRoot(async () => {
775 const key1 = ['test-combine-1']
776 const key2 = ['test-combine-2']
777​
778 const { promise: promise1, resolve: resolve1 } =
779 promiseWithResolvers<string>()
780 const { promise: promise2, resolve: resolve2 } =
781 promiseWithResolvers<string>()
782​
783 const queries = createQueries(
784 () => ({
785 queries: [
786 {
787 queryKey: key1,
788 queryFn: () => promise1,
789 },
790 {
791 queryKey: key2,
792 queryFn: () => promise2,
793 },
794 ],
795 combine: (results) => {
796 return {
797 combined: true,
798 res: results
799 .flatMap((res) => (res.data ? [res.data] : []))
800 .join(','),
801 }
802 },
803 }),
804 () => queryClient,
805 )
806​
807 // Initially both queries are loading
808 expect(queries).toEqual({
809 combined: true,
810 res: '',
811 })
812​
813 // Resolve the first query
814 resolve1('first result')
815 await vi.waitFor(() => expect(queries.res).toBe('first result'))
816​
817 // Resolve the second query
818 resolve2('second result')
819 await vi.waitFor(() =>
820 expect(queries.res).toBe('first result,second result'),
821 )
822​
823 expect(queries).toEqual({
824 combined: true,
825 res: 'first result,second result',
826 })
827 }),
828 )
829​
830 it(
831 'should track property access through combine function',
832 withEffectRoot(async () => {
833 const key1 = ['test-track-combine-1']
834 const key2 = ['test-track-combine-2']
835 let count = 0
836 const results: Array<unknown> = []
837​
838 const { promise: promise1, resolve: resolve1 } =
839 promiseWithResolvers<string>()
840 const { promise: promise2, resolve: resolve2 } =
841 promiseWithResolvers<string>()
842 const { promise: promise3, resolve: resolve3 } =
843 promiseWithResolvers<string>()
844 const { promise: promise4, resolve: resolve4 } =
845 promiseWithResolvers<string>()
846​
847 const queries = createQueries(
848 () => ({
849 queries: [
850 {
851 queryKey: key1,
852 queryFn: () => (count === 0 ? promise1 : promise3),
853 },
854 {
855 queryKey: key2,
856 queryFn: () => (count === 0 ? promise2 : promise4),
857 },
858 ],
859 combine: (queryResults) => {
860 return {
861 combined: true,
862 refetch: () =>
863 Promise.all(queryResults.map((res) => res.refetch())),
864 res: queryResults
865 .flatMap((res) => (res.data ? [res.data] : []))
866 .join(','),
867 }
868 },
869 }),
870 () => queryClient,
871 )
872​
873 $effect(() => {
874 results.push({ ...queries })
875 })
876​
877 // Initially both queries are loading
878 await vi.waitFor(() =>
879 expect(results[0]).toStrictEqual({
880 combined: true,
881 refetch: expect.any(Function),
882 res: '',
883 }),
884 )
885​
886 // Resolve the first query
887 resolve1('first result ' + count)
888 await vi.waitFor(() => expect(queries.res).toBe('first result 0'))
889​
890 expect(results[1]).toStrictEqual({
891 combined: true,
892 refetch: expect.any(Function),
893 res: 'first result 0',
894 })
895​
896 // Resolve the second query
897 resolve2('second result ' + count)
898 await vi.waitFor(() =>
899 expect(queries.res).toBe('first result 0,second result 0'),
900 )
901​
902 expect(results[2]).toStrictEqual({
903 combined: true,
904 refetch: expect.any(Function),
905 res: 'first result 0,second result 0',
906 })
907​
908 // Increment count and refetch
909 count++
910 queries.refetch()
911​
912 // Resolve the refetched queries
913 resolve3('first result ' + count)
914 resolve4('second result ' + count)
915​
916 await vi.waitFor(() =>
917 expect(queries.res).toBe('first result 1,second result 1'),
918 )
919​
920 const length = results.length
921 expect(results.at(-1)).toStrictEqual({
922 combined: true,
923 refetch: expect.any(Function),
924 res: 'first result 1,second result 1',
925 })
926​
927 // Refetch again but with the same data
928 await queries.refetch()
929​
930 // No further re-render because data didn't change
931 expect(results.length).toBe(length)
932 }),
933 )
934})