Skip to content

Commit 5ebdae1

Browse files
committed
test({react,preact}-query/useSuspenseQueries): add test for not suspending when all queries have fresh cached data
1 parent 75fab82 commit 5ebdae1

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,4 +908,52 @@ describe('useSuspenseQueries 2', () => {
908908
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
909909
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
910910
})
911+
912+
it('should not suspend and not refetch when all queries have fresh cached data', () => {
913+
const key1 = queryKey()
914+
const key2 = queryKey()
915+
916+
queryClient.setQueryData(key1, 'cached1')
917+
queryClient.setQueryData(key2, 'cached2')
918+
919+
const queryFn1 = vi.fn(() => sleep(20).then(() => 'data1'))
920+
const queryFn2 = vi.fn(() => sleep(10).then(() => 'data2'))
921+
922+
function Page() {
923+
const [result1, result2] = useSuspenseQueries({
924+
queries: [
925+
{
926+
queryKey: key1,
927+
queryFn: queryFn1,
928+
},
929+
{
930+
queryKey: key2,
931+
queryFn: queryFn2,
932+
},
933+
],
934+
})
935+
936+
return (
937+
<div>
938+
<div>data1: {result1.data}</div>
939+
<div>data2: {result2.data}</div>
940+
</div>
941+
)
942+
}
943+
944+
const rendered = renderWithClient(
945+
queryClient,
946+
<Suspense fallback={<div>loading</div>}>
947+
<Page />
948+
</Suspense>,
949+
)
950+
951+
// No suspend, fresh cached data shown immediately
952+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
953+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
954+
955+
// No background refetch because data is still fresh (within staleTime)
956+
expect(queryFn1).toHaveBeenCalledTimes(0)
957+
expect(queryFn2).toHaveBeenCalledTimes(0)
958+
})
911959
})

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,4 +886,52 @@ describe('useSuspenseQueries 2', () => {
886886
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
887887
expect(rendered.getByText('data2: data2')).toBeInTheDocument()
888888
})
889+
890+
it('should not suspend and not refetch when all queries have fresh cached data', () => {
891+
const key1 = queryKey()
892+
const key2 = queryKey()
893+
894+
queryClient.setQueryData(key1, 'cached1')
895+
queryClient.setQueryData(key2, 'cached2')
896+
897+
const queryFn1 = vi.fn(() => sleep(20).then(() => 'data1'))
898+
const queryFn2 = vi.fn(() => sleep(10).then(() => 'data2'))
899+
900+
function Page() {
901+
const [result1, result2] = useSuspenseQueries({
902+
queries: [
903+
{
904+
queryKey: key1,
905+
queryFn: queryFn1,
906+
},
907+
{
908+
queryKey: key2,
909+
queryFn: queryFn2,
910+
},
911+
],
912+
})
913+
914+
return (
915+
<div>
916+
<div>data1: {result1.data}</div>
917+
<div>data2: {result2.data}</div>
918+
</div>
919+
)
920+
}
921+
922+
const rendered = renderWithClient(
923+
queryClient,
924+
<React.Suspense fallback={<div>loading</div>}>
925+
<Page />
926+
</React.Suspense>,
927+
)
928+
929+
// No suspend, fresh cached data shown immediately
930+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
931+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
932+
933+
// No background refetch because data is still fresh (within staleTime)
934+
expect(queryFn1).toHaveBeenCalledTimes(0)
935+
expect(queryFn2).toHaveBeenCalledTimes(0)
936+
})
889937
})

0 commit comments

Comments
 (0)