Skip to content

Commit f65e7af

Browse files
dante01yoonTkDodo
andauthored
test: add useQueries with suspense tests (#4502)
* test: add useQueries with suspense tests shouldn't unmount before all promises fetched should suspend all queries in parallel - global configuration * fix should suspend all queries in parallel - global configuration - remove query suspense option Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 409a684 commit f65e7af

File tree

1 file changed

+117
-3
lines changed

1 file changed

+117
-3
lines changed

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

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { fireEvent, waitFor } from '@testing-library/react'
2-
import { ErrorBoundary } from 'react-error-boundary'
32
import * as React from 'react'
4-
5-
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
3+
import { ErrorBoundary } from 'react-error-boundary'
64
import type { UseInfiniteQueryResult, UseQueryResult } from '..'
75
import {
86
QueryCache,
@@ -12,6 +10,7 @@ import {
1210
useQuery,
1311
useQueryErrorResetBoundary,
1412
} from '..'
13+
import { createQueryClient, queryKey, renderWithClient, sleep } from './utils'
1514

1615
describe("useQuery's in Suspense mode", () => {
1716
const queryCache = new QueryCache()
@@ -1061,6 +1060,7 @@ describe('useQueries with suspense', () => {
10611060
<Page />
10621061
</React.Suspense>,
10631062
)
1063+
10641064
await waitFor(() => rendered.getByText('loading'))
10651065
await waitFor(() => rendered.getByText('data: 1,2'))
10661066

@@ -1121,4 +1121,118 @@ describe('useQueries with suspense', () => {
11211121

11221122
expect(results).toEqual(['1', '2', 'loading'])
11231123
})
1124+
1125+
it("shouldn't unmount before all promises fetched", async () => {
1126+
const key1 = queryKey()
1127+
const key2 = queryKey()
1128+
const results: string[] = []
1129+
const refs: number[] = []
1130+
1131+
function Fallback() {
1132+
results.push('loading')
1133+
return <div>loading</div>
1134+
}
1135+
1136+
function Page() {
1137+
const ref = React.useRef(Math.random())
1138+
const result = useQueries({
1139+
queries: [
1140+
{
1141+
queryKey: key1,
1142+
queryFn: async () => {
1143+
refs.push(ref.current)
1144+
results.push('1')
1145+
await sleep(10)
1146+
return '1'
1147+
},
1148+
suspense: true,
1149+
},
1150+
{
1151+
queryKey: key2,
1152+
queryFn: async () => {
1153+
refs.push(ref.current)
1154+
results.push('2')
1155+
await sleep(20)
1156+
return '2'
1157+
},
1158+
suspense: true,
1159+
},
1160+
],
1161+
})
1162+
return (
1163+
<div>
1164+
<h1>data: {result.map((it) => it.data ?? 'null').join(',')}</h1>
1165+
</div>
1166+
)
1167+
}
1168+
1169+
const rendered = renderWithClient(
1170+
queryClient,
1171+
<React.Suspense fallback={<Fallback />}>
1172+
<Page />
1173+
</React.Suspense>,
1174+
)
1175+
await waitFor(() => rendered.getByText('loading'))
1176+
expect(refs.length).toBe(2)
1177+
await waitFor(() => rendered.getByText('data: 1,2'))
1178+
expect(refs[0]).toBe(refs[1])
1179+
})
1180+
1181+
it('should suspend all queries in parallel - global configuration', async () => {
1182+
const queryClientSuspenseMode = createQueryClient({
1183+
defaultOptions: {
1184+
queries: {
1185+
suspense: true,
1186+
},
1187+
},
1188+
})
1189+
const key1 = queryKey()
1190+
const key2 = queryKey()
1191+
const results: string[] = []
1192+
1193+
function Fallback() {
1194+
results.push('loading')
1195+
return <div>loading</div>
1196+
}
1197+
1198+
function Page() {
1199+
const result = useQueries({
1200+
queries: [
1201+
{
1202+
queryKey: key1,
1203+
queryFn: async () => {
1204+
results.push('1')
1205+
await sleep(10)
1206+
return '1'
1207+
},
1208+
},
1209+
{
1210+
queryKey: key2,
1211+
queryFn: async () => {
1212+
results.push('2')
1213+
await sleep(20)
1214+
return '2'
1215+
},
1216+
},
1217+
],
1218+
})
1219+
return (
1220+
<div>
1221+
<h1>data: {result.map((it) => it.data ?? 'null').join(',')}</h1>
1222+
</div>
1223+
)
1224+
}
1225+
1226+
const rendered = renderWithClient(
1227+
queryClientSuspenseMode,
1228+
<React.Suspense fallback={<Fallback />}>
1229+
<Page />
1230+
</React.Suspense>,
1231+
)
1232+
1233+
await waitFor(() => rendered.getByText('loading'))
1234+
await waitFor(() => rendered.getByText('data: 1,2'))
1235+
1236+
expect(results).toEqual(['1', '2', 'loading'])
1237+
})
11241238
})

0 commit comments

Comments
 (0)