-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathuseSuspenseQueries.test.tsx
More file actions
889 lines (725 loc) · 22.5 KB
/
useSuspenseQueries.test.tsx
File metadata and controls
889 lines (725 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import { act, fireEvent, render } from '@testing-library/react'
import * as React from 'react'
import { ErrorBoundary } from 'react-error-boundary'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import {
QueryClient,
skipToken,
useSuspenseQueries,
useSuspenseQuery,
} from '..'
import { renderWithClient } from './utils'
import type { UseSuspenseQueryOptions } from '..'
type NumberQueryOptions = UseSuspenseQueryOptions<number>
const QUERY_DURATION = 1000
const createQuery: (id: number) => NumberQueryOptions = (id) => ({
queryKey: [id],
queryFn: () => sleep(QUERY_DURATION).then(() => id),
})
const resolveQueries = () => vi.advanceTimersByTimeAsync(QUERY_DURATION)
const queryClient = new QueryClient()
describe('useSuspenseQueries', () => {
const onSuspend = vi.fn()
const onQueriesResolution = vi.fn()
beforeAll(() => {
vi.useFakeTimers()
})
afterAll(() => {
vi.useRealTimers()
})
afterEach(() => {
queryClient.clear()
onSuspend.mockClear()
onQueriesResolution.mockClear()
})
function SuspenseFallback() {
React.useEffect(() => {
onSuspend()
}, [])
return <div>loading</div>
}
const withSuspenseWrapper = <T extends object>(Component: React.FC<T>) => {
function SuspendedComponent(props: T) {
return (
<React.Suspense fallback={<SuspenseFallback />}>
<Component {...props} />
</React.Suspense>
)
}
return SuspendedComponent
}
function QueriesContainer({
queries,
}: {
queries: Array<NumberQueryOptions>
}) {
const queriesResults = useSuspenseQueries(
{ queries, combine: (results) => results.map((r) => r.data) },
queryClient,
)
React.useEffect(() => {
onQueriesResolution(queriesResults)
}, [queriesResults])
return null
}
const TestComponent = withSuspenseWrapper(QueriesContainer)
it('should suspend on mount', () => {
render(<TestComponent queries={[1, 2].map(createQuery)} />)
expect(onSuspend).toHaveBeenCalledOnce()
})
it('should resolve queries', async () => {
render(<TestComponent queries={[1, 2].map(createQuery)} />)
await act(resolveQueries)
expect(onQueriesResolution).toHaveBeenCalledTimes(1)
expect(onQueriesResolution).toHaveBeenLastCalledWith([1, 2])
})
it('should not suspend on mount if query has been already fetched', () => {
const query = createQuery(1)
queryClient.setQueryData(query.queryKey, query.queryFn)
render(<TestComponent queries={[query]} />)
expect(onSuspend).not.toHaveBeenCalled()
})
it('should not break suspense when queries change without resolving', async () => {
const initQueries = [1, 2].map(createQuery)
const nextQueries = [3, 4, 5, 6].map(createQuery)
const { rerender } = render(<TestComponent queries={initQueries} />)
rerender(<TestComponent queries={nextQueries} />)
await act(resolveQueries)
expect(onSuspend).toHaveBeenCalledTimes(1)
expect(onQueriesResolution).toHaveBeenCalledTimes(1)
expect(onQueriesResolution).toHaveBeenLastCalledWith([3, 4, 5, 6])
})
it('should suspend only once per queries change', async () => {
const initQueries = [1, 2].map(createQuery)
const nextQueries = [3, 4, 5, 6].map(createQuery)
const { rerender } = render(<TestComponent queries={initQueries} />)
await act(resolveQueries)
rerender(<TestComponent queries={nextQueries} />)
await act(resolveQueries)
expect(onSuspend).toHaveBeenCalledTimes(2)
expect(onQueriesResolution).toHaveBeenCalledTimes(2)
expect(onQueriesResolution).toHaveBeenLastCalledWith([3, 4, 5, 6])
})
it('should only call combine after resolving', async () => {
const spy = vi.fn()
const key = queryKey()
function Page() {
const data = useSuspenseQueries({
queries: [1, 2, 3].map((value) => ({
queryKey: [...key, { value }],
queryFn: () => sleep(value * 10).then(() => ({ value: value * 10 })),
})),
combine: (result) => {
spy(result)
return 'data'
},
})
return <h1>{data}</h1>
}
const rendered = renderWithClient(
queryClient,
<React.Suspense fallback="loading">
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
expect(spy).not.toHaveBeenCalled()
await act(() => vi.advanceTimersByTimeAsync(30))
expect(rendered.getByText('data')).toBeInTheDocument()
expect(spy).toHaveBeenCalled()
})
it('should handle duplicate query keys without infinite loops', async () => {
const key = queryKey()
const localDuration = 10
let renderCount = 0
function getUserData() {
return {
queryKey: key,
queryFn: async () => {
await sleep(localDuration)
return { name: 'John Doe', age: 50 }
},
}
}
function getName() {
return {
...getUserData(),
select: (data: any) => data.name,
}
}
function getAge() {
return {
...getUserData(),
select: (data: any) => data.age,
}
}
function App() {
renderCount++
const [{ data }, { data: data2 }] = useSuspenseQueries({
queries: [getName(), getAge()],
})
React.useEffect(() => {
onQueriesResolution({ data, data2 })
}, [data, data2])
return (
<div>
<h1>Data</h1>
{JSON.stringify({ data }, null, 2)}
{JSON.stringify({ data2 }, null, 2)}
</div>
)
}
renderWithClient(
queryClient,
<React.Suspense fallback={<SuspenseFallback />}>
<App />
</React.Suspense>,
)
await act(() => vi.advanceTimersByTimeAsync(localDuration))
expect(onSuspend).toHaveBeenCalledTimes(1)
expect(onQueriesResolution).toHaveBeenCalledTimes(1)
await act(() => vi.advanceTimersByTimeAsync(100))
expect(onQueriesResolution).toHaveBeenCalledTimes(1)
expect(onQueriesResolution).toHaveBeenLastCalledWith({
data: 'John Doe',
data2: 50,
})
// With the infinite loop bug, renderCount would be very high (e.g. > 100)
// Without bug, it should be small (initial suspend + resolution = 2-3)
expect(renderCount).toBeLessThan(10)
})
})
describe('useSuspenseQueries 2', () => {
beforeEach(() => {
vi.useFakeTimers()
})
afterEach(() => {
vi.useRealTimers()
})
it('should suspend all queries in parallel', async () => {
const key1 = queryKey()
const key2 = queryKey()
const results: Array<string> = []
function Fallback() {
results.push('loading')
return <div>loading</div>
}
function Page() {
const result = useSuspenseQueries({
queries: [
{
queryKey: key1,
queryFn: () =>
sleep(10).then(() => {
results.push('1')
return '1'
}),
},
{
queryKey: key2,
queryFn: () =>
sleep(20).then(() => {
results.push('2')
return '2'
}),
},
],
})
return (
<div>
<h1>data: {result.map((item) => item.data ?? 'null').join(',')}</h1>
</div>
)
}
const rendered = renderWithClient(
queryClient,
<React.Suspense fallback={<Fallback />}>
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(20))
expect(rendered.getByText('data: 1,2')).toBeInTheDocument()
expect(results).toEqual(['loading', '1', '2'])
})
it("shouldn't unmount before all promises fetched", async () => {
const key1 = queryKey()
const key2 = queryKey()
const results: Array<string> = []
const refs: Array<number> = []
function Fallback() {
results.push('loading')
return <div>loading</div>
}
function Page() {
// eslint-disable-next-line react-hooks/purity
const ref = React.useRef(Math.random())
const result = useSuspenseQueries({
queries: [
{
queryKey: key1,
queryFn: () =>
sleep(10).then(() => {
refs.push(ref.current)
results.push('1')
return '1'
}),
},
{
queryKey: key2,
queryFn: () =>
sleep(20).then(() => {
refs.push(ref.current)
results.push('2')
return '2'
}),
},
],
})
return (
<div>
<h1>data: {result.map((item) => item.data ?? 'null').join(',')}</h1>
</div>
)
}
const rendered = renderWithClient(
queryClient,
<React.Suspense fallback={<Fallback />}>
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(20))
expect(rendered.getByText('data: 1,2')).toBeInTheDocument()
expect(refs.length).toBe(2)
expect(refs[0]).toBe(refs[1])
})
// this addresses the following issue:
// https://github.com/TanStack/query/issues/6344
it('should suspend on offline when query changes, and data should not be undefined', async () => {
function Page() {
const [id, setId] = React.useState(0)
const { data } = useSuspenseQuery({
queryKey: [id],
queryFn: () => sleep(10).then(() => `Data ${id}`),
})
// defensive guard here
if (data === undefined) {
throw new Error('data cannot be undefined')
}
return (
<>
<div>{data}</div>
<button onClick={() => setId(id + 1)}>fetch</button>
</>
)
}
const rendered = renderWithClient(
queryClient,
<React.Suspense fallback={<div>loading</div>}>
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('Data 0')).toBeInTheDocument()
// go offline
document.dispatchEvent(new CustomEvent('offline'))
fireEvent.click(rendered.getByText('fetch'))
expect(rendered.getByText('Data 0')).toBeInTheDocument()
// go back online
document.dispatchEvent(new CustomEvent('online'))
fireEvent.click(rendered.getByText('fetch'))
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
// query should resume
expect(rendered.getByText('Data 1')).toBeInTheDocument()
})
it('should throw error when queryKey changes and new query fails', async () => {
const consoleMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
const key = queryKey()
function Page() {
const [fail, setFail] = React.useState(false)
const { data } = useSuspenseQuery({
queryKey: [key, fail],
queryFn: () =>
sleep(10).then(() => {
if (fail) throw new Error('Suspense Error Bingo')
return 'data'
}),
retry: 0,
})
return (
<div>
<button onClick={() => setFail(true)}>trigger fail</button>
<div>rendered: {data}</div>
</div>
)
}
const rendered = renderWithClient(
queryClient,
<ErrorBoundary fallbackRender={() => <div>error boundary</div>}>
<React.Suspense fallback="loading">
<Page />
</React.Suspense>
</ErrorBoundary>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('rendered: data')).toBeInTheDocument()
fireEvent.click(rendered.getByText('trigger fail'))
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('error boundary')).toBeInTheDocument()
expect(consoleMock.mock.calls[0]?.[1]).toStrictEqual(
new Error('Suspense Error Bingo'),
)
consoleMock.mockRestore()
})
it('should keep previous data when wrapped in a transition', async () => {
const key = queryKey()
function Page() {
const [count, setCount] = React.useState(0)
const [isPending, startTransition] = React.useTransition()
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: () => sleep(10).then(() => 'data' + count),
})
return (
<div>
<button onClick={() => startTransition(() => setCount(count + 1))}>
inc
</button>
<div>{isPending ? 'pending' : data}</div>
</div>
)
}
const rendered = renderWithClient(
queryClient,
<React.Suspense fallback="loading">
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data0')).toBeInTheDocument()
fireEvent.click(rendered.getByText('inc'))
expect(rendered.getByText('pending')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data1')).toBeInTheDocument()
})
it('should not request old data inside transitions (issue #6486)', async () => {
const key = queryKey()
let queryFnCount = 0
function App() {
const [count, setCount] = React.useState(0)
return (
<div>
<button
onClick={() => React.startTransition(() => setCount(count + 1))}
>
inc
</button>
<React.Suspense fallback="loading">
<Page count={count} />
</React.Suspense>
</div>
)
}
function Page({ count }: { count: number }) {
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: () =>
sleep(10).then(() => {
queryFnCount++
return 'data' + count
}),
})
return (
<div>
<div>{String(data)}</div>
</div>
)
}
const rendered = renderWithClient(
queryClient,
<App />,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data0')).toBeInTheDocument()
fireEvent.click(rendered.getByText('inc'))
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data1')).toBeInTheDocument()
expect(queryFnCount).toBe(2)
})
it('should still suspense if queryClient has placeholderData config', async () => {
const key = queryKey()
const queryClientWithPlaceholder = new QueryClient({
defaultOptions: {
queries: {
placeholderData: (previousData: any) => previousData,
},
},
})
function Page() {
const [count, setCount] = React.useState(0)
const [isPending, startTransition] = React.useTransition()
const { data } = useSuspenseQuery({
queryKey: [key, count],
queryFn: () => sleep(10).then(() => 'data' + count),
})
return (
<div>
<button onClick={() => startTransition(() => setCount(count + 1))}>
inc
</button>
<div>{isPending ? 'pending' : data}</div>
</div>
)
}
const rendered = renderWithClient(
queryClientWithPlaceholder,
<React.Suspense fallback="loading">
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data0')).toBeInTheDocument()
fireEvent.click(rendered.getByText('inc'))
expect(rendered.getByText('pending')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('data1')).toBeInTheDocument()
})
it('should show error boundary even with gcTime:0 (#7853)', async () => {
const consoleMock = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
const key = queryKey()
let count = 0
function Page() {
useSuspenseQuery({
queryKey: key,
queryFn: () =>
sleep(10).then(() => {
count++
throw new Error('Query failed')
}),
gcTime: 0,
retry: false,
})
return null
}
function App() {
return (
<React.Suspense fallback="loading">
<ErrorBoundary
fallbackRender={() => {
return <div>There was an error!</div>
}}
>
<Page />
</ErrorBoundary>
</React.Suspense>
)
}
const rendered = renderWithClient(queryClient, <App />)
expect(rendered.getByText('loading')).toBeInTheDocument()
await act(() => vi.advanceTimersByTimeAsync(10))
expect(rendered.getByText('There was an error!')).toBeInTheDocument()
expect(count).toBe(1)
consoleMock.mockRestore()
})
describe('gc (with fake timers)', () => {
beforeAll(() => {
vi.useFakeTimers()
})
afterAll(() => {
vi.useRealTimers()
})
it('should gc when unmounted while fetching with low gcTime (#8159)', async () => {
const key = queryKey()
function Page() {
return (
<React.Suspense fallback="loading">
<Component />
</React.Suspense>
)
}
function Component() {
const { data } = useSuspenseQuery({
queryKey: key,
queryFn: () => sleep(3000).then(() => 'data'),
gcTime: 1000,
})
return <div>{data}</div>
}
function Page2() {
return <div>page2</div>
}
function App() {
const [show, setShow] = React.useState(true)
return (
<div>
{show ? <Page /> : <Page2 />}
<button onClick={() => setShow(false)}>hide</button>
</div>
)
}
const rendered = renderWithClient(queryClient, <App />)
expect(rendered.getByText('loading')).toBeInTheDocument()
fireEvent.click(rendered.getByText('hide'))
expect(rendered.getByText('page2')).toBeInTheDocument()
// wait for query to be resolved
await act(() => vi.advanceTimersByTimeAsync(3000))
expect(queryClient.getQueryData(key)).toBe('data')
// wait for gc
await act(() => vi.advanceTimersByTimeAsync(1000))
expect(queryClient.getQueryData(key)).toBe(undefined)
})
})
it('should log an error when skipToken is passed as queryFn', () => {
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
const key = queryKey()
function Page() {
useSuspenseQueries({
queries: [
{
queryKey: key,
// @ts-expect-error
// eslint-disable-next-line react-hooks/purity
queryFn: Math.random() >= 0 ? skipToken : () => Promise.resolve(5),
},
],
})
return null
}
function App() {
return (
<React.Suspense fallback="loading">
<Page />
</React.Suspense>
)
}
renderWithClient(queryClient, <App />)
expect(consoleErrorSpy).toHaveBeenCalledWith(
'skipToken is not allowed for useSuspenseQueries',
)
consoleErrorSpy.mockRestore()
})
it('should log an error when skipToken is used in development environment', () => {
const envCopy = process.env.NODE_ENV
process.env.NODE_ENV = 'development'
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
const key = queryKey()
function Page() {
useSuspenseQueries({
queries: [
{
queryKey: key,
queryFn: skipToken as any,
},
],
})
return null
}
renderWithClient(
queryClient,
<React.Suspense fallback="loading">
<Page />
</React.Suspense>,
)
expect(consoleErrorSpy).toHaveBeenCalledWith(
'skipToken is not allowed for useSuspenseQueries',
)
consoleErrorSpy.mockRestore()
process.env.NODE_ENV = envCopy
})
it('should not log an error when skipToken is used in production environment', () => {
const envCopy = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)
const key = queryKey()
function Page() {
useSuspenseQueries({
queries: [
{
queryKey: key,
queryFn: skipToken as any,
},
],
})
return null
}
renderWithClient(
queryClient,
<React.Suspense fallback="loading">
<Page />
</React.Suspense>,
)
expect(consoleErrorSpy).not.toHaveBeenCalled()
consoleErrorSpy.mockRestore()
process.env.NODE_ENV = envCopy
})
it('should only suspend queries that are pending when some queries already have data', async () => {
const key1 = queryKey()
const key2 = queryKey()
queryClient.setQueryData(key1, 'cached')
function Page() {
const [result1, result2] = useSuspenseQueries({
queries: [
{
queryKey: key1,
queryFn: () => sleep(2000).then(() => 'data1'),
},
{
queryKey: key2,
queryFn: () => sleep(1000).then(() => 'data2'),
},
],
})
return (
<div>
<div>data1: {result1.data}</div>
<div>data2: {result2.data}</div>
</div>
)
}
const rendered = renderWithClient(
queryClient,
<React.Suspense fallback={<div>loading</div>}>
<Page />
</React.Suspense>,
)
expect(rendered.getByText('loading')).toBeInTheDocument()
// key2 resolves: suspend lifts, key1 shows cached data, key2 shows fresh data
await act(() => vi.advanceTimersByTimeAsync(1000))
expect(rendered.getByText('data1: cached')).toBeInTheDocument()
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
// key1 stale timer fires, triggering background refetch
await act(() => vi.advanceTimersByTimeAsync(1000))
// key1 background refetch completes: key1 updates to fresh data
await act(() => vi.advanceTimersByTimeAsync(2000))
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
})
})