Skip to content

Commit 8a59b2d

Browse files
authored
test({react,preact}-query/useSuspenseQueries): add test for suspending when the faster query already has data (#10210)
1 parent 0eca134 commit 8a59b2d

2 files changed

Lines changed: 108 additions & 2 deletions

File tree

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ describe('useSuspenseQueries 2', () => {
856856
process.env.NODE_ENV = envCopy
857857
})
858858

859-
it('should only suspend queries that are pending when some queries already have data', async () => {
859+
it('should only suspend queries that are pending when the slower query already has data', async () => {
860860
const key1 = queryKey()
861861
const key2 = queryKey()
862862

@@ -909,6 +909,59 @@ describe('useSuspenseQueries 2', () => {
909909
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
910910
})
911911

912+
it('should only suspend queries that are pending when the faster query already has data', async () => {
913+
const key1 = queryKey()
914+
const key2 = queryKey()
915+
916+
queryClient.setQueryData(key2, 'cached')
917+
918+
function Page() {
919+
const [result1, result2] = useSuspenseQueries({
920+
queries: [
921+
{
922+
queryKey: key1,
923+
queryFn: () => sleep(2000).then(() => 'data1'),
924+
},
925+
{
926+
queryKey: key2,
927+
queryFn: () => sleep(1000).then(() => 'data2'),
928+
},
929+
],
930+
})
931+
932+
return (
933+
<div>
934+
<div>data1: {result1.data}</div>
935+
<div>data2: {result2.data}</div>
936+
</div>
937+
)
938+
}
939+
940+
const rendered = renderWithClient(
941+
queryClient,
942+
<Suspense fallback={<div>loading</div>}>
943+
<Page />
944+
</Suspense>,
945+
)
946+
947+
expect(rendered.getByText('loading')).toBeInTheDocument()
948+
949+
// key1 resolves: suspend lifts, key1 shows fresh data, key2 shows cached data
950+
await vi.advanceTimersByTimeAsync(2000)
951+
952+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
953+
expect(rendered.getByText('data2: cached')).toBeInTheDocument()
954+
955+
// key2 stale timer fires, triggering background refetch
956+
await vi.advanceTimersByTimeAsync(1000)
957+
958+
// key2 background refetch completes: key2 updates to fresh data
959+
await vi.advanceTimersByTimeAsync(1000)
960+
961+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
962+
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
963+
})
964+
912965
it('should not suspend and not refetch when all queries have fresh cached data', () => {
913966
const key1 = queryKey()
914967
const key2 = queryKey()

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ describe('useSuspenseQueries 2', () => {
834834
process.env.NODE_ENV = envCopy
835835
})
836836

837-
it('should only suspend queries that are pending when some queries already have data', async () => {
837+
it('should only suspend queries that are pending when the slower query already has data', async () => {
838838
const key1 = queryKey()
839839
const key2 = queryKey()
840840

@@ -887,6 +887,59 @@ describe('useSuspenseQueries 2', () => {
887887
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
888888
})
889889

890+
it('should only suspend queries that are pending when the faster query already has data', async () => {
891+
const key1 = queryKey()
892+
const key2 = queryKey()
893+
894+
queryClient.setQueryData(key2, 'cached')
895+
896+
function Page() {
897+
const [result1, result2] = useSuspenseQueries({
898+
queries: [
899+
{
900+
queryKey: key1,
901+
queryFn: () => sleep(2000).then(() => 'data1'),
902+
},
903+
{
904+
queryKey: key2,
905+
queryFn: () => sleep(1000).then(() => 'data2'),
906+
},
907+
],
908+
})
909+
910+
return (
911+
<div>
912+
<div>data1: {result1.data}</div>
913+
<div>data2: {result2.data}</div>
914+
</div>
915+
)
916+
}
917+
918+
const rendered = renderWithClient(
919+
queryClient,
920+
<React.Suspense fallback={<div>loading</div>}>
921+
<Page />
922+
</React.Suspense>,
923+
)
924+
925+
expect(rendered.getByText('loading')).toBeInTheDocument()
926+
927+
// key1 resolves: suspend lifts, key1 shows fresh data, key2 shows cached data
928+
await act(() => vi.advanceTimersByTimeAsync(2000))
929+
930+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
931+
expect(rendered.getByText('data2: cached')).toBeInTheDocument()
932+
933+
// key2 stale timer fires, triggering background refetch
934+
await act(() => vi.advanceTimersByTimeAsync(1000))
935+
936+
// key2 background refetch completes: key2 updates to fresh data
937+
await act(() => vi.advanceTimersByTimeAsync(1000))
938+
939+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
940+
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
941+
})
942+
890943
it('should not suspend and not refetch when all queries have fresh cached data', () => {
891944
const key1 = queryKey()
892945
const key2 = queryKey()

0 commit comments

Comments
 (0)