Skip to content

Commit 55cee0a

Browse files
authored
test({react,preact}-query/useSuspenseQueries): add test for not suspending but only refetching the stale query when one query has stale and the other has fresh cached data (#10349)
1 parent 289fe2e commit 55cee0a

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,74 @@ describe('useSuspenseQueries 2', () => {
10781078
expect(queryFn1).toHaveBeenCalledTimes(0)
10791079
})
10801080

1081+
it('should not suspend and only refetch the stale query when one query has stale and the other has fresh cached data', async () => {
1082+
const key1 = queryKey()
1083+
const key2 = queryKey()
1084+
1085+
queryClient.setQueryData(key1, 'cached1')
1086+
queryClient.setQueryData(key2, 'cached2')
1087+
1088+
// Advance past staleTime (min 1000ms in suspense) so key1 becomes stale before mount
1089+
vi.advanceTimersByTime(1000)
1090+
1091+
// Make key2 fresh again by resetting its data
1092+
queryClient.setQueryData(key2, 'cached2')
1093+
1094+
const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))
1095+
1096+
function Page() {
1097+
const [result1, result2] = useSuspenseQueries({
1098+
queries: [
1099+
{
1100+
queryKey: key1,
1101+
queryFn: () => sleep(10).then(() => 'data1'),
1102+
},
1103+
{
1104+
queryKey: key2,
1105+
queryFn: queryFn2,
1106+
},
1107+
],
1108+
})
1109+
1110+
return (
1111+
<div>
1112+
<div>data1: {result1.data}</div>
1113+
<div>data2: {result2.data}</div>
1114+
</div>
1115+
)
1116+
}
1117+
1118+
const rendered = renderWithClient(
1119+
queryClient,
1120+
<Suspense fallback={<div>loading</div>}>
1121+
<Page />
1122+
</Suspense>,
1123+
)
1124+
1125+
// No suspend, cached data shown immediately
1126+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
1127+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1128+
1129+
// key2 is fresh, no refetch
1130+
expect(queryFn2).toHaveBeenCalledTimes(0)
1131+
1132+
// key1 background refetch completes
1133+
await vi.advanceTimersByTimeAsync(11)
1134+
1135+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
1136+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1137+
1138+
// key2 is still fresh, no refetch triggered
1139+
expect(queryFn2).toHaveBeenCalledTimes(0)
1140+
1141+
// after key1 refetch completes, key2 is still fresh with no refetch triggered
1142+
await vi.advanceTimersByTimeAsync(10)
1143+
1144+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
1145+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1146+
expect(queryFn2).toHaveBeenCalledTimes(0)
1147+
})
1148+
10811149
it('should not suspend but refetch when all queries have stale cached data', async () => {
10821150
const key1 = queryKey()
10831151
const key2 = queryKey()

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,74 @@ describe('useSuspenseQueries 2', () => {
10561056
expect(queryFn1).toHaveBeenCalledTimes(0)
10571057
})
10581058

1059+
it('should not suspend and only refetch the stale query when one query has stale and the other has fresh cached data', async () => {
1060+
const key1 = queryKey()
1061+
const key2 = queryKey()
1062+
1063+
queryClient.setQueryData(key1, 'cached1')
1064+
queryClient.setQueryData(key2, 'cached2')
1065+
1066+
// Advance past staleTime (min 1000ms in suspense) so key1 becomes stale before mount
1067+
vi.advanceTimersByTime(1000)
1068+
1069+
// Make key2 fresh again by resetting its data
1070+
queryClient.setQueryData(key2, 'cached2')
1071+
1072+
const queryFn2 = vi.fn(() => sleep(20).then(() => 'data2'))
1073+
1074+
function Page() {
1075+
const [result1, result2] = useSuspenseQueries({
1076+
queries: [
1077+
{
1078+
queryKey: key1,
1079+
queryFn: () => sleep(10).then(() => 'data1'),
1080+
},
1081+
{
1082+
queryKey: key2,
1083+
queryFn: queryFn2,
1084+
},
1085+
],
1086+
})
1087+
1088+
return (
1089+
<div>
1090+
<div>data1: {result1.data}</div>
1091+
<div>data2: {result2.data}</div>
1092+
</div>
1093+
)
1094+
}
1095+
1096+
const rendered = renderWithClient(
1097+
queryClient,
1098+
<React.Suspense fallback={<div>loading</div>}>
1099+
<Page />
1100+
</React.Suspense>,
1101+
)
1102+
1103+
// No suspend, cached data shown immediately
1104+
expect(rendered.getByText('data1: cached1')).toBeInTheDocument()
1105+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1106+
1107+
// key2 is fresh, no refetch
1108+
expect(queryFn2).toHaveBeenCalledTimes(0)
1109+
1110+
// key1 background refetch completes
1111+
await act(() => vi.advanceTimersByTimeAsync(11))
1112+
1113+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
1114+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1115+
1116+
// key2 is still fresh, no refetch triggered
1117+
expect(queryFn2).toHaveBeenCalledTimes(0)
1118+
1119+
// after key1 refetch completes, key2 is still fresh with no refetch triggered
1120+
await act(() => vi.advanceTimersByTimeAsync(10))
1121+
1122+
expect(rendered.getByText('data1: data1')).toBeInTheDocument()
1123+
expect(rendered.getByText('data2: cached2')).toBeInTheDocument()
1124+
expect(queryFn2).toHaveBeenCalledTimes(0)
1125+
})
1126+
10591127
it('should not suspend but refetch when all queries have stale cached data', async () => {
10601128
const key1 = queryKey()
10611129
const key2 = queryKey()

0 commit comments

Comments
 (0)