9
9
10
10
let React ;
11
11
let Scheduler ;
12
+ let waitForAll ;
13
+ let assertLog ;
12
14
let ReactNoop ;
13
15
let useState ;
14
16
let act ;
@@ -32,6 +34,10 @@ describe('act warnings', () => {
32
34
startTransition = React . startTransition ;
33
35
getCacheForType = React . unstable_getCacheForType ;
34
36
caches = [ ] ;
37
+
38
+ const InternalTestUtils = require ( 'internal-test-utils' ) ;
39
+ waitForAll = InternalTestUtils . waitForAll ;
40
+ assertLog = InternalTestUtils . assertLog ;
35
41
} ) ;
36
42
37
43
function createTextCache ( ) {
@@ -134,17 +140,17 @@ describe('act warnings', () => {
134
140
}
135
141
}
136
142
137
- function withActEnvironment ( value , scope ) {
143
+ async function withActEnvironment ( value , scope ) {
138
144
const prevValue = global . IS_REACT_ACT_ENVIRONMENT ;
139
145
global . IS_REACT_ACT_ENVIRONMENT = value ;
140
146
try {
141
- return scope ( ) ;
147
+ return await scope ( ) ;
142
148
} finally {
143
149
global . IS_REACT_ACT_ENVIRONMENT = prevValue ;
144
150
}
145
151
}
146
152
147
- test ( 'warns about unwrapped updates only if environment flag is enabled' , ( ) => {
153
+ test ( 'warns about unwrapped updates only if environment flag is enabled' , async ( ) => {
148
154
let setState ;
149
155
function App ( ) {
150
156
const [ state , _setState ] = useState ( 0 ) ;
@@ -154,34 +160,34 @@ describe('act warnings', () => {
154
160
155
161
const root = ReactNoop . createRoot ( ) ;
156
162
root . render ( < App /> ) ;
157
- expect ( Scheduler ) . toFlushAndYield ( [ 0 ] ) ;
163
+ await waitForAll ( [ 0 ] ) ;
158
164
expect ( root ) . toMatchRenderedOutput ( '0' ) ;
159
165
160
166
// Default behavior. Flag is undefined. No warning.
161
167
expect ( global . IS_REACT_ACT_ENVIRONMENT ) . toBe ( undefined ) ;
162
168
setState ( 1 ) ;
163
- expect ( Scheduler ) . toFlushAndYield ( [ 1 ] ) ;
169
+ await waitForAll ( [ 1 ] ) ;
164
170
expect ( root ) . toMatchRenderedOutput ( '1' ) ;
165
171
166
172
// Flag is true. Warn.
167
- withActEnvironment ( true , ( ) => {
173
+ await withActEnvironment ( true , async ( ) => {
168
174
expect ( ( ) => setState ( 2 ) ) . toErrorDev (
169
175
'An update to App inside a test was not wrapped in act' ,
170
176
) ;
171
- expect ( Scheduler ) . toFlushAndYield ( [ 2 ] ) ;
177
+ await waitForAll ( [ 2 ] ) ;
172
178
expect ( root ) . toMatchRenderedOutput ( '2' ) ;
173
179
} ) ;
174
180
175
181
// Flag is false. No warning.
176
- withActEnvironment ( false , ( ) => {
182
+ await withActEnvironment ( false , async ( ) => {
177
183
setState ( 3 ) ;
178
- expect ( Scheduler ) . toFlushAndYield ( [ 3 ] ) ;
184
+ await waitForAll ( [ 3 ] ) ;
179
185
expect ( root ) . toMatchRenderedOutput ( '3' ) ;
180
186
} ) ;
181
187
} ) ;
182
188
183
189
// @gate __DEV__
184
- test ( 'act warns if the environment flag is not enabled' , ( ) => {
190
+ test ( 'act warns if the environment flag is not enabled' , async ( ) => {
185
191
let setState ;
186
192
function App ( ) {
187
193
const [ state , _setState ] = useState ( 0 ) ;
@@ -191,7 +197,7 @@ describe('act warnings', () => {
191
197
192
198
const root = ReactNoop . createRoot ( ) ;
193
199
root . render ( < App /> ) ;
194
- expect ( Scheduler ) . toFlushAndYield ( [ 0 ] ) ;
200
+ await waitForAll ( [ 0 ] ) ;
195
201
expect ( root ) . toMatchRenderedOutput ( '0' ) ;
196
202
197
203
// Default behavior. Flag is undefined. Warn.
@@ -204,20 +210,20 @@ describe('act warnings', () => {
204
210
'The current testing environment is not configured to support act(...)' ,
205
211
{ withoutStack : true } ,
206
212
) ;
207
- expect ( Scheduler ) . toHaveYielded ( [ 1 ] ) ;
213
+ assertLog ( [ 1 ] ) ;
208
214
expect ( root ) . toMatchRenderedOutput ( '1' ) ;
209
215
210
216
// Flag is true. Don't warn.
211
- withActEnvironment ( true , ( ) => {
217
+ await withActEnvironment ( true , ( ) => {
212
218
act ( ( ) => {
213
219
setState ( 2 ) ;
214
220
} ) ;
215
- expect ( Scheduler ) . toHaveYielded ( [ 2 ] ) ;
221
+ assertLog ( [ 2 ] ) ;
216
222
expect ( root ) . toMatchRenderedOutput ( '2' ) ;
217
223
} ) ;
218
224
219
225
// Flag is false. Warn.
220
- withActEnvironment ( false , ( ) => {
226
+ await withActEnvironment ( false , ( ) => {
221
227
expect ( ( ) => {
222
228
act ( ( ) => {
223
229
setState ( 1 ) ;
@@ -226,13 +232,13 @@ describe('act warnings', () => {
226
232
'The current testing environment is not configured to support act(...)' ,
227
233
{ withoutStack : true } ,
228
234
) ;
229
- expect ( Scheduler ) . toHaveYielded ( [ 1 ] ) ;
235
+ assertLog ( [ 1 ] ) ;
230
236
expect ( root ) . toMatchRenderedOutput ( '1' ) ;
231
237
} ) ;
232
238
} ) ;
233
239
234
- test ( 'warns if root update is not wrapped' , ( ) => {
235
- withActEnvironment ( true , ( ) => {
240
+ test ( 'warns if root update is not wrapped' , async ( ) => {
241
+ await withActEnvironment ( true , ( ) => {
236
242
const root = ReactNoop . createRoot ( ) ;
237
243
expect ( ( ) => root . render ( 'Hi' ) ) . toErrorDev (
238
244
// TODO: Better error message that doesn't make it look like "Root" is
@@ -244,7 +250,7 @@ describe('act warnings', () => {
244
250
} ) ;
245
251
246
252
// @gate __DEV__
247
- test ( 'warns if class update is not wrapped' , ( ) => {
253
+ test ( 'warns if class update is not wrapped' , async ( ) => {
248
254
let app ;
249
255
class App extends React . Component {
250
256
state = { count : 0 } ;
@@ -254,7 +260,7 @@ describe('act warnings', () => {
254
260
}
255
261
}
256
262
257
- withActEnvironment ( true , ( ) => {
263
+ await withActEnvironment ( true , ( ) => {
258
264
const root = ReactNoop . createRoot ( ) ;
259
265
act ( ( ) => {
260
266
root . render ( < App /> ) ;
@@ -266,18 +272,18 @@ describe('act warnings', () => {
266
272
} ) ;
267
273
268
274
// @gate __DEV__
269
- test ( 'warns even if update is synchronous' , ( ) => {
275
+ test ( 'warns even if update is synchronous' , async ( ) => {
270
276
let setState ;
271
277
function App ( ) {
272
278
const [ state , _setState ] = useState ( 0 ) ;
273
279
setState = _setState ;
274
280
return < Text text = { state } /> ;
275
281
}
276
282
277
- withActEnvironment ( true , ( ) => {
283
+ await withActEnvironment ( true , ( ) => {
278
284
const root = ReactNoop . createRoot ( ) ;
279
285
act ( ( ) => root . render ( < App /> ) ) ;
280
- expect ( Scheduler ) . toHaveYielded ( [ 0 ] ) ;
286
+ assertLog ( [ 0 ] ) ;
281
287
expect ( root ) . toMatchRenderedOutput ( '0' ) ;
282
288
283
289
// Even though this update is synchronous, we should still fire a warning,
@@ -286,14 +292,14 @@ describe('act warnings', () => {
286
292
'An update to App inside a test was not wrapped in act(...)' ,
287
293
) ;
288
294
289
- expect ( Scheduler ) . toHaveYielded ( [ 1 ] ) ;
295
+ assertLog ( [ 1 ] ) ;
290
296
expect ( root ) . toMatchRenderedOutput ( '1' ) ;
291
297
} ) ;
292
298
} ) ;
293
299
294
300
// @gate __DEV__
295
301
// @gate enableLegacyCache
296
- test ( 'warns if Suspense retry is not wrapped' , ( ) => {
302
+ test ( 'warns if Suspense retry is not wrapped' , async ( ) => {
297
303
function App ( ) {
298
304
return (
299
305
< Suspense fallback = { < Text text = "Loading..." /> } >
@@ -302,12 +308,12 @@ describe('act warnings', () => {
302
308
) ;
303
309
}
304
310
305
- withActEnvironment ( true , ( ) => {
311
+ await withActEnvironment ( true , ( ) => {
306
312
const root = ReactNoop . createRoot ( ) ;
307
313
act ( ( ) => {
308
314
root . render ( < App /> ) ;
309
315
} ) ;
310
- expect ( Scheduler ) . toHaveYielded ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
316
+ assertLog ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
311
317
expect ( root ) . toMatchRenderedOutput ( 'Loading...' ) ;
312
318
313
319
// This is a retry, not a ping, because we already showed a fallback.
@@ -321,7 +327,7 @@ describe('act warnings', () => {
321
327
322
328
// @gate __DEV__
323
329
// @gate enableLegacyCache
324
- test ( 'warns if Suspense ping is not wrapped' , ( ) => {
330
+ test ( 'warns if Suspense ping is not wrapped' , async ( ) => {
325
331
function App ( { showMore} ) {
326
332
return (
327
333
< Suspense fallback = { < Text text = "Loading..." /> } >
@@ -330,20 +336,20 @@ describe('act warnings', () => {
330
336
) ;
331
337
}
332
338
333
- withActEnvironment ( true , ( ) => {
339
+ await withActEnvironment ( true , ( ) => {
334
340
const root = ReactNoop . createRoot ( ) ;
335
341
act ( ( ) => {
336
342
root . render ( < App showMore = { false } /> ) ;
337
343
} ) ;
338
- expect ( Scheduler ) . toHaveYielded ( [ '(empty)' ] ) ;
344
+ assertLog ( [ '(empty)' ] ) ;
339
345
expect ( root ) . toMatchRenderedOutput ( '(empty)' ) ;
340
346
341
347
act ( ( ) => {
342
348
startTransition ( ( ) => {
343
349
root . render ( < App showMore = { true } /> ) ;
344
350
} ) ;
345
351
} ) ;
346
- expect ( Scheduler ) . toHaveYielded ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
352
+ assertLog ( [ 'Suspend! [Async]' , 'Loading...' ] ) ;
347
353
expect ( root ) . toMatchRenderedOutput ( '(empty)' ) ;
348
354
349
355
// This is a ping, not a retry, because no fallback is showing.
0 commit comments