Skip to content

Commit e133b0d

Browse files
committed
tests: useQueries suspense
1 parent acab5d6 commit e133b0d

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
QueryCache,
99
QueryErrorResetBoundary,
1010
useInfiniteQuery,
11+
useQueries,
1112
useQuery,
1213
useQueryErrorResetBoundary,
1314
} from '..'
@@ -1011,3 +1012,113 @@ describe("useQuery's in Suspense mode", () => {
10111012
expect(rendered.queryByText('rendered')).not.toBeNull()
10121013
})
10131014
})
1015+
1016+
describe('useQueries with suspense', () => {
1017+
const queryClient = createQueryClient()
1018+
it('should suspend all queries in parallel', async () => {
1019+
const key1 = queryKey()
1020+
const key2 = queryKey()
1021+
const results: string[] = []
1022+
1023+
function Fallback() {
1024+
results.push('loading')
1025+
return <div>loading</div>
1026+
}
1027+
1028+
function Page() {
1029+
const result = useQueries({
1030+
queries: [
1031+
{
1032+
queryKey: key1,
1033+
queryFn: async () => {
1034+
results.push('1')
1035+
await sleep(10)
1036+
return '1'
1037+
},
1038+
suspense: true,
1039+
},
1040+
{
1041+
queryKey: key2,
1042+
queryFn: async () => {
1043+
results.push('2')
1044+
await sleep(20)
1045+
return '2'
1046+
},
1047+
suspense: true,
1048+
},
1049+
],
1050+
})
1051+
return (
1052+
<div>
1053+
<h1>data: {result.map((it) => it.data ?? 'null').join(',')}</h1>
1054+
</div>
1055+
)
1056+
}
1057+
1058+
const rendered = renderWithClient(
1059+
queryClient,
1060+
<React.Suspense fallback={<Fallback />}>
1061+
<Page />
1062+
</React.Suspense>,
1063+
)
1064+
await waitFor(() => rendered.getByText('loading'))
1065+
await waitFor(() => rendered.getByText('data: 1,2'))
1066+
1067+
expect(results).toEqual(['1', '2', 'loading'])
1068+
})
1069+
1070+
it('should allow to mix suspense with non-suspense', async () => {
1071+
const key1 = queryKey()
1072+
const key2 = queryKey()
1073+
const results: string[] = []
1074+
1075+
function Fallback() {
1076+
results.push('loading')
1077+
return <div>loading</div>
1078+
}
1079+
1080+
function Page() {
1081+
const result = useQueries({
1082+
queries: [
1083+
{
1084+
queryKey: key1,
1085+
queryFn: async () => {
1086+
results.push('1')
1087+
await sleep(10)
1088+
return '1'
1089+
},
1090+
suspense: true,
1091+
},
1092+
{
1093+
queryKey: key2,
1094+
queryFn: async () => {
1095+
results.push('2')
1096+
await sleep(20)
1097+
return '2'
1098+
},
1099+
suspense: false,
1100+
},
1101+
],
1102+
})
1103+
return (
1104+
<div>
1105+
<h1>data: {result.map((it) => it.data ?? 'null').join(',')}</h1>
1106+
<h2>status: {result.map((it) => it.status).join(',')}</h2>
1107+
</div>
1108+
)
1109+
}
1110+
1111+
const rendered = renderWithClient(
1112+
queryClient,
1113+
<React.Suspense fallback={<Fallback />}>
1114+
<Page />
1115+
</React.Suspense>,
1116+
)
1117+
await waitFor(() => rendered.getByText('loading'))
1118+
await waitFor(() => rendered.getByText('status: success,loading'))
1119+
await waitFor(() => rendered.getByText('data: 1,null'))
1120+
await waitFor(() => rendered.getByText('data: 1,2'))
1121+
1122+
expect(results).toEqual(['1', '2', 'loading'])
1123+
})
1124+
})

0 commit comments

Comments
 (0)