1
1
import { fireEvent , waitFor } from '@testing-library/react'
2
- import { ErrorBoundary } from 'react-error-boundary'
3
2
import * as React from 'react'
4
-
5
- import { createQueryClient , queryKey , renderWithClient , sleep } from './utils'
3
+ import { ErrorBoundary } from 'react-error-boundary'
6
4
import type { UseInfiniteQueryResult , UseQueryResult } from '..'
7
5
import {
8
6
QueryCache ,
@@ -12,6 +10,7 @@ import {
12
10
useQuery ,
13
11
useQueryErrorResetBoundary ,
14
12
} from '..'
13
+ import { createQueryClient , queryKey , renderWithClient , sleep } from './utils'
15
14
16
15
describe ( "useQuery's in Suspense mode" , ( ) => {
17
16
const queryCache = new QueryCache ( )
@@ -1061,6 +1060,7 @@ describe('useQueries with suspense', () => {
1061
1060
< Page />
1062
1061
</ React . Suspense > ,
1063
1062
)
1063
+
1064
1064
await waitFor ( ( ) => rendered . getByText ( 'loading' ) )
1065
1065
await waitFor ( ( ) => rendered . getByText ( 'data: 1,2' ) )
1066
1066
@@ -1121,4 +1121,118 @@ describe('useQueries with suspense', () => {
1121
1121
1122
1122
expect ( results ) . toEqual ( [ '1' , '2' , 'loading' ] )
1123
1123
} )
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
+ } )
1124
1238
} )
0 commit comments