12
12
let PropTypes ;
13
13
let React ;
14
14
let ReactDOMClient ;
15
- let ReactTestUtils ;
16
15
let act ;
17
16
18
17
function FunctionComponent ( props ) {
@@ -26,7 +25,6 @@ describe('ReactFunctionComponent', () => {
26
25
React = require ( 'react' ) ;
27
26
ReactDOMClient = require ( 'react-dom/client' ) ;
28
27
act = require ( 'internal-test-utils' ) . act ;
29
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
30
28
} ) ;
31
29
32
30
it ( 'should render stateless component' , async ( ) => {
@@ -161,25 +159,33 @@ describe('ReactFunctionComponent', () => {
161
159
) ;
162
160
} ) ;
163
161
164
- it ( 'should not throw when stateless component returns undefined' , ( ) => {
162
+ it ( 'should not throw when stateless component returns undefined' , async ( ) => {
165
163
function NotAComponent ( ) { }
166
- expect ( function ( ) {
167
- ReactTestUtils . renderIntoDocument (
168
- < div >
169
- < NotAComponent />
170
- </ div > ,
171
- ) ;
172
- } ) . not . toThrowError ( ) ;
164
+ const container = document . createElement ( 'div' ) ;
165
+ const root = ReactDOMClient . createRoot ( container ) ;
166
+ await expect (
167
+ act ( ( ) => {
168
+ root . render (
169
+ < div >
170
+ < NotAComponent />
171
+ </ div > ,
172
+ ) ;
173
+ } ) ,
174
+ ) . resolves . not . toThrowError ( ) ;
173
175
} ) ;
174
176
175
- it ( 'should throw on string refs in pure functions' , ( ) => {
177
+ it ( 'should throw on string refs in pure functions' , async ( ) => {
176
178
function Child ( ) {
177
179
return < div ref = "me" /> ;
178
180
}
179
181
180
- expect ( function ( ) {
181
- ReactTestUtils . renderIntoDocument ( < Child test = "test" /> ) ;
182
- } ) . toThrowError (
182
+ const container = document . createElement ( 'div' ) ;
183
+ const root = ReactDOMClient . createRoot ( container ) ;
184
+ await expect (
185
+ act ( ( ) => {
186
+ root . render ( < Child test = "test" /> ) ;
187
+ } ) ,
188
+ ) . rejects . toThrowError (
183
189
__DEV__
184
190
? 'Function components cannot have string refs. We recommend using useRef() instead.'
185
191
: // It happens because we don't save _owner in production for
@@ -193,7 +199,7 @@ describe('ReactFunctionComponent', () => {
193
199
) ;
194
200
} ) ;
195
201
196
- it ( 'should warn when given a string ref' , ( ) => {
202
+ it ( 'should warn when given a string ref' , async ( ) => {
197
203
function Indirection ( props ) {
198
204
return < div > { props . children } </ div > ;
199
205
}
@@ -208,9 +214,13 @@ describe('ReactFunctionComponent', () => {
208
214
}
209
215
}
210
216
211
- expect ( ( ) =>
212
- ReactTestUtils . renderIntoDocument ( < ParentUsingStringRef /> ) ,
213
- ) . toErrorDev (
217
+ await expect ( async ( ) => {
218
+ const container = document . createElement ( 'div' ) ;
219
+ const root = ReactDOMClient . createRoot ( container ) ;
220
+ await act ( ( ) => {
221
+ root . render ( < ParentUsingStringRef /> ) ;
222
+ } ) ;
223
+ } ) . toErrorDev (
214
224
'Warning: Function components cannot be given refs. ' +
215
225
'Attempts to access this ref will fail. ' +
216
226
'Did you mean to use React.forwardRef()?\n\n' +
@@ -222,11 +232,14 @@ describe('ReactFunctionComponent', () => {
222
232
' in ParentUsingStringRef (at **)' ,
223
233
) ;
224
234
225
- // No additional warnings should be logged
226
- ReactTestUtils . renderIntoDocument ( < ParentUsingStringRef /> ) ;
235
+ const container = document . createElement ( 'div' ) ;
236
+ const root = ReactDOMClient . createRoot ( container ) ;
237
+ await act ( ( ) => {
238
+ root . render ( < ParentUsingStringRef /> ) ;
239
+ } ) ;
227
240
} ) ;
228
241
229
- it ( 'should warn when given a function ref' , ( ) => {
242
+ it ( 'should warn when given a function ref' , async ( ) => {
230
243
function Indirection ( props ) {
231
244
return < div > { props . children } </ div > ;
232
245
}
@@ -246,9 +259,13 @@ describe('ReactFunctionComponent', () => {
246
259
}
247
260
}
248
261
249
- expect ( ( ) =>
250
- ReactTestUtils . renderIntoDocument ( < ParentUsingFunctionRef /> ) ,
251
- ) . toErrorDev (
262
+ await expect ( async ( ) => {
263
+ const container = document . createElement ( 'div' ) ;
264
+ const root = ReactDOMClient . createRoot ( container ) ;
265
+ await act ( ( ) => {
266
+ root . render ( < ParentUsingFunctionRef /> ) ;
267
+ } ) ;
268
+ } ) . toErrorDev (
252
269
'Warning: Function components cannot be given refs. ' +
253
270
'Attempts to access this ref will fail. ' +
254
271
'Did you mean to use React.forwardRef()?\n\n' +
@@ -260,11 +277,14 @@ describe('ReactFunctionComponent', () => {
260
277
' in ParentUsingFunctionRef (at **)' ,
261
278
) ;
262
279
263
- // No additional warnings should be logged
264
- ReactTestUtils . renderIntoDocument ( < ParentUsingFunctionRef /> ) ;
280
+ const container = document . createElement ( 'div' ) ;
281
+ const root = ReactDOMClient . createRoot ( container ) ;
282
+ await act ( ( ) => {
283
+ root . render ( < ParentUsingFunctionRef /> ) ;
284
+ } ) ;
265
285
} ) ;
266
286
267
- it ( 'deduplicates ref warnings based on element or owner' , ( ) => {
287
+ it ( 'deduplicates ref warnings based on element or owner' , async ( ) => {
268
288
// When owner uses JSX, we can use exact line location to dedupe warnings
269
289
class AnonymousParentUsingJSX extends React . Component {
270
290
render ( ) {
@@ -274,15 +294,24 @@ describe('ReactFunctionComponent', () => {
274
294
275
295
let instance1 ;
276
296
277
- expect ( ( ) => {
278
- instance1 = ReactTestUtils . renderIntoDocument (
279
- < AnonymousParentUsingJSX /> ,
280
- ) ;
297
+ await expect ( async ( ) => {
298
+ const container = document . createElement ( 'div' ) ;
299
+ const root = ReactDOMClient . createRoot ( container ) ;
300
+
301
+ await act ( ( ) => {
302
+ root . render (
303
+ < AnonymousParentUsingJSX ref = { current => ( instance1 = current ) } /> ,
304
+ ) ;
305
+ } ) ;
281
306
} ) . toErrorDev ( 'Warning: Function components cannot be given refs.' ) ;
282
307
// Should be deduped (offending element is on the same line):
283
308
instance1 . forceUpdate ( ) ;
284
- // Should also be deduped (offending element is on the same line):
285
- ReactTestUtils . renderIntoDocument ( < AnonymousParentUsingJSX /> ) ;
309
+ let container = document . createElement ( 'div' ) ;
310
+ let root = ReactDOMClient . createRoot ( container ) ;
311
+
312
+ await act ( ( ) => {
313
+ root . render ( < AnonymousParentUsingJSX /> ) ;
314
+ } ) ;
286
315
287
316
// When owner doesn't use JSX, and is anonymous, we warn once per internal instance.
288
317
class AnonymousParentNotUsingJSX extends React . Component {
@@ -295,15 +324,22 @@ describe('ReactFunctionComponent', () => {
295
324
}
296
325
297
326
let instance2 ;
298
- expect ( ( ) => {
299
- instance2 = ReactTestUtils . renderIntoDocument (
300
- < AnonymousParentNotUsingJSX /> ,
301
- ) ;
327
+ await expect ( async ( ) => {
328
+ container = document . createElement ( 'div' ) ;
329
+ root = ReactDOMClient . createRoot ( container ) ;
330
+ await act ( ( ) => {
331
+ root . render (
332
+ < AnonymousParentNotUsingJSX ref = { current => ( instance2 = current ) } /> ,
333
+ ) ;
334
+ } ) ;
302
335
} ) . toErrorDev ( 'Warning: Function components cannot be given refs.' ) ;
303
336
// Should be deduped (same internal instance, no additional warnings)
304
337
instance2 . forceUpdate ( ) ;
305
- // Could not be differentiated (since owner is anonymous and no source location)
306
- ReactTestUtils . renderIntoDocument ( < AnonymousParentNotUsingJSX /> ) ;
338
+ container = document . createElement ( 'div' ) ;
339
+ root = ReactDOMClient . createRoot ( container ) ;
340
+ await act ( ( ) => {
341
+ root . render ( < AnonymousParentNotUsingJSX /> ) ;
342
+ } ) ;
307
343
308
344
// When owner doesn't use JSX, but is named, we warn once per owner name
309
345
class NamedParentNotUsingJSX extends React . Component {
@@ -315,19 +351,28 @@ describe('ReactFunctionComponent', () => {
315
351
}
316
352
}
317
353
let instance3 ;
318
- expect ( ( ) => {
319
- instance3 = ReactTestUtils . renderIntoDocument ( < NamedParentNotUsingJSX /> ) ;
354
+ await expect ( async ( ) => {
355
+ container = document . createElement ( 'div' ) ;
356
+ root = ReactDOMClient . createRoot ( container ) ;
357
+ await act ( ( ) => {
358
+ root . render (
359
+ < NamedParentNotUsingJSX ref = { current => ( instance3 = current ) } /> ,
360
+ ) ;
361
+ } ) ;
320
362
} ) . toErrorDev ( 'Warning: Function components cannot be given refs.' ) ;
321
363
// Should be deduped (same owner name, no additional warnings):
322
364
instance3 . forceUpdate ( ) ;
323
- // Should also be deduped (same owner name, no additional warnings):
324
- ReactTestUtils . renderIntoDocument ( < NamedParentNotUsingJSX /> ) ;
365
+ container = document . createElement ( 'div' ) ;
366
+ root = ReactDOMClient . createRoot ( container ) ;
367
+ await act ( ( ) => {
368
+ root . render ( < NamedParentNotUsingJSX /> ) ;
369
+ } ) ;
325
370
} ) ;
326
371
327
372
// This guards against a regression caused by clearing the current debug fiber.
328
373
// https://github.com/facebook/react/issues/10831
329
374
// @gate !disableLegacyContext || !__DEV__
330
- it ( 'should warn when giving a function ref with context' , ( ) => {
375
+ it ( 'should warn when giving a function ref with context' , async ( ) => {
331
376
function Child ( ) {
332
377
return null ;
333
378
}
@@ -349,7 +394,13 @@ describe('ReactFunctionComponent', () => {
349
394
}
350
395
}
351
396
352
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Parent /> ) ) . toErrorDev (
397
+ await expect ( async ( ) => {
398
+ const container = document . createElement ( 'div' ) ;
399
+ const root = ReactDOMClient . createRoot ( container ) ;
400
+ await act ( ( ) => {
401
+ root . render ( < Parent /> ) ;
402
+ } ) ;
403
+ } ) . toErrorDev (
353
404
'Warning: Function components cannot be given refs. ' +
354
405
'Attempts to access this ref will fail. ' +
355
406
'Did you mean to use React.forwardRef()?\n\n' +
@@ -360,36 +411,40 @@ describe('ReactFunctionComponent', () => {
360
411
) ;
361
412
} ) ;
362
413
363
- it ( 'should provide a null ref' , ( ) => {
364
- function Child ( ) {
365
- return < div /> ;
366
- }
367
-
368
- const comp = ReactTestUtils . renderIntoDocument ( < Child /> ) ;
369
- expect ( comp ) . toBe ( null ) ;
370
- } ) ;
371
-
372
- it ( 'should use correct name in key warning' , ( ) => {
414
+ it ( 'should use correct name in key warning' , async ( ) => {
373
415
function Child ( ) {
374
416
return < div > { [ < span /> ] } </ div > ;
375
417
}
376
418
377
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Child /> ) ) . toErrorDev (
419
+ await expect ( async ( ) => {
420
+ const container = document . createElement ( 'div' ) ;
421
+ const root = ReactDOMClient . createRoot ( container ) ;
422
+ await act ( ( ) => {
423
+ root . render ( < Child /> ) ;
424
+ } ) ;
425
+ } ) . toErrorDev (
378
426
'Each child in a list should have a unique "key" prop.\n\n' +
379
427
'Check the render method of `Child`.' ,
380
428
) ;
381
429
} ) ;
382
430
383
431
// TODO: change this test after we deprecate default props support
384
432
// for function components
385
- it ( 'should support default props and prop types' , ( ) => {
433
+ it ( 'should support default props and prop types' , async ( ) => {
386
434
function Child ( props ) {
387
435
return < div > { props . test } </ div > ;
388
436
}
389
437
Child . defaultProps = { test : 2 } ;
390
438
Child . propTypes = { test : PropTypes . string } ;
391
439
392
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Child /> ) ) . toErrorDev ( [
440
+ await expect ( async ( ) => {
441
+ const container = document . createElement ( 'div' ) ;
442
+ const root = ReactDOMClient . createRoot ( container ) ;
443
+
444
+ await act ( ( ) => {
445
+ root . render ( < Child /> ) ;
446
+ } ) ;
447
+ } ) . toErrorDev ( [
393
448
'Warning: Child: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.' ,
394
449
'Warning: Failed prop type: Invalid prop `test` of type `number` ' +
395
450
'supplied to `Child`, expected `string`.\n' +
@@ -427,28 +482,46 @@ describe('ReactFunctionComponent', () => {
427
482
expect ( el . textContent ) . toBe ( 'en' ) ;
428
483
} ) ;
429
484
430
- it ( 'should work with arrow functions' , ( ) => {
485
+ it ( 'should work with arrow functions' , async ( ) => {
431
486
let Child = function ( ) {
432
487
return < div /> ;
433
488
} ;
434
489
// Will create a new bound function without a prototype, much like a native
435
490
// arrow function.
436
491
Child = Child . bind ( this ) ;
437
492
438
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Child /> ) ) . not . toThrow ( ) ;
493
+ await expect ( async ( ) => {
494
+ const container = document . createElement ( 'div' ) ;
495
+ const root = ReactDOMClient . createRoot ( container ) ;
496
+ await act ( ( ) => {
497
+ root . render ( < Child /> ) ;
498
+ } ) ;
499
+ } ) . not . toThrow ( ) ;
439
500
} ) ;
440
501
441
- it ( 'should allow simple functions to return null' , ( ) => {
502
+ it ( 'should allow simple functions to return null' , async ( ) => {
442
503
const Child = function ( ) {
443
504
return null ;
444
505
} ;
445
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Child /> ) ) . not . toThrow ( ) ;
506
+ await expect ( async ( ) => {
507
+ const container = document . createElement ( 'div' ) ;
508
+ const root = ReactDOMClient . createRoot ( container ) ;
509
+ await act ( ( ) => {
510
+ root . render ( < Child /> ) ;
511
+ } ) ;
512
+ } ) . not . toThrow ( ) ;
446
513
} ) ;
447
514
448
- it ( 'should allow simple functions to return false' , ( ) => {
515
+ it ( 'should allow simple functions to return false' , async ( ) => {
449
516
function Child ( ) {
450
517
return false ;
451
518
}
452
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Child /> ) ) . not . toThrow ( ) ;
519
+ const container = document . createElement ( 'div' ) ;
520
+ const root = ReactDOMClient . createRoot ( container ) ;
521
+ await expect (
522
+ act ( ( ) => {
523
+ root . render ( < Child /> ) ;
524
+ } ) ,
525
+ ) . resolves . not . toThrow ( ) ;
453
526
} ) ;
454
527
} ) ;
0 commit comments