Skip to content

Commit 0eca134

Browse files
authored
test({react,preact}-query/useSuspenseQueries): add test for not suspending but refetching when all queries have stale cached data (#10209)
1 parent 391db36 commit 0eca134

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

packages/preact-query/src/__tests__/useSuspenseQueries.test.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,4 +956,60 @@ describe('useSuspenseQueries 2', () => {
956956
expect(queryFn1).toHaveBeenCalledTimes(0)
957957
expect(queryFn2).toHaveBeenCalledTimes(0)
958958
})
959+
960+
it('should not suspend but refetch when all queries have stale cached data', async () => {
961+
const key1 = queryKey()
962+
const key2 = queryKey()
963+
964+
queryClient.setQueryData(key1, 'cached1')
965+
queryClient.setQueryData(key2, 'cached2')
966+
967+
// Advance past staleTime (min 1000ms in suspense) so data becomes stale before mount
968+
vi.advanceTimersByTime(1000)
969+
970+
function Page() {
971+
const [result1, result2] = useSuspenseQueries({
972+
queries: [
973+
{
974+
queryKey: key1,
975+
queryFn: () => sleep(20).then(() => 'data1'),
976+
},
977+
{
978+
queryKey: key2,
979+
queryFn: () => sleep(10).then(() => 'data2'),
980+
},
981+
],
982+
})
983+
984+
return (
985+
<div>
986+
<div>data1: {result1.data}</div>
987+
<div>data2: {result2.data}</div>
988+
</div>
989+
)
990+
}
991+
992+
const rendered = renderWithClient(
993+
queryClient,
994+
<Suspense fallback={<div>loading</div>}>
995+
<Page />
996+
</Suspense>,
997+
)
998+
999+
// No suspend, stale cached data shown immediately with background refetch started
1000+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
1001+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1002+
1003+
// key2 background refetch completes
1004+
await vi.advanceTimersByTimeAsync(11)
1005+
1006+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
1007+
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
1008+
1009+
// key1 background refetch completes
1010+
await vi.advanceTimersByTimeAsync(10)
1011+
1012+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
1013+
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
1014+
})
9591015
})

packages/react-query/src/__tests__/useSuspenseQueries.test.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,4 +934,60 @@ describe('useSuspenseQueries 2', () => {
934934
expect(queryFn1).toHaveBeenCalledTimes(0)
935935
expect(queryFn2).toHaveBeenCalledTimes(0)
936936
})
937+
938+
it('should not suspend but refetch when all queries have stale cached data', async () => {
939+
const key1 = queryKey()
940+
const key2 = queryKey()
941+
942+
queryClient.setQueryData(key1, 'cached1')
943+
queryClient.setQueryData(key2, 'cached2')
944+
945+
// Advance past staleTime (min 1000ms in suspense) so data becomes stale before mount
946+
vi.advanceTimersByTime(1000)
947+
948+
function Page() {
949+
const [result1, result2] = useSuspenseQueries({
950+
queries: [
951+
{
952+
queryKey: key1,
953+
queryFn: () => sleep(20).then(() => 'data1'),
954+
},
955+
{
956+
queryKey: key2,
957+
queryFn: () => sleep(10).then(() => 'data2'),
958+
},
959+
],
960+
})
961+
962+
return (
963+
<div>
964+
<div>data1: {result1.data}</div>
965+
<div>data2: {result2.data}</div>
966+
</div>
967+
)
968+
}
969+
970+
const rendered = renderWithClient(
971+
queryClient,
972+
<React.Suspense fallback={<div>loading</div>}>
973+
<Page />
974+
</React.Suspense>,
975+
)
976+
977+
// No suspend, stale cached data shown immediately with background refetch started
978+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
979+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
980+
981+
// key2 background refetch completes
982+
await act(() => vi.advanceTimersByTimeAsync(11))
983+
984+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
985+
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
986+
987+
// key1 background refetch completes
988+
await act(() => vi.advanceTimersByTimeAsync(10))
989+
990+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
991+
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
992+
})
937993
})

0 commit comments

Comments
 (0)