18
18
let PropTypes ;
19
19
let React ;
20
20
let ReactDOMClient ;
21
- let ReactTestUtils ;
22
21
let act ;
23
22
24
23
describe ( 'ReactContextValidator' , ( ) => {
@@ -28,15 +27,14 @@ describe('ReactContextValidator', () => {
28
27
PropTypes = require ( 'prop-types' ) ;
29
28
React = require ( 'react' ) ;
30
29
ReactDOMClient = require ( 'react-dom/client' ) ;
31
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
32
30
act = require ( 'internal-test-utils' ) . act ;
33
31
} ) ;
34
32
35
33
// TODO: This behavior creates a runtime dependency on propTypes. We should
36
34
// ensure that this is not required for ES6 classes with Flow.
37
35
38
36
// @gate !disableLegacyContext
39
- it ( 'should filter out context not in contextTypes' , ( ) => {
37
+ it ( 'should filter out context not in contextTypes' , async ( ) => {
40
38
class Component extends React . Component {
41
39
render ( ) {
42
40
return < div /> ;
@@ -65,9 +63,14 @@ describe('ReactContextValidator', () => {
65
63
bar : PropTypes . number ,
66
64
} ;
67
65
68
- const instance = ReactTestUtils . renderIntoDocument (
69
- < ComponentInFooBarContext /> ,
70
- ) ;
66
+ let instance ;
67
+ const container = document . createElement ( 'div' ) ;
68
+ const root = ReactDOMClient . createRoot ( container ) ;
69
+ await act ( ( ) => {
70
+ root . render (
71
+ < ComponentInFooBarContext ref = { current => ( instance = current ) } /> ,
72
+ ) ;
73
+ } ) ;
71
74
expect ( instance . childRef . current . context ) . toEqual ( { foo : 'abc' } ) ;
72
75
} ) ;
73
76
@@ -160,7 +163,7 @@ describe('ReactContextValidator', () => {
160
163
// TODO (bvaughn) Remove this test and the associated behavior in the future.
161
164
// It has only been added in Fiber to match the (unintentional) behavior in Stack.
162
165
// @gate !disableLegacyContext || !__DEV__
163
- it ( 'should warn (but not error) if getChildContext method is missing' , ( ) => {
166
+ it ( 'should warn (but not error) if getChildContext method is missing' , async ( ) => {
164
167
class ComponentA extends React . Component {
165
168
static childContextTypes = {
166
169
foo : PropTypes . string . isRequired ,
@@ -178,16 +181,32 @@ describe('ReactContextValidator', () => {
178
181
}
179
182
}
180
183
181
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < ComponentA /> ) ) . toErrorDev (
184
+ await expect ( async ( ) => {
185
+ const container = document . createElement ( 'div' ) ;
186
+ const root = ReactDOMClient . createRoot ( container ) ;
187
+ await act ( ( ) => {
188
+ root . render ( < ComponentA /> ) ;
189
+ } ) ;
190
+ } ) . toErrorDev (
182
191
'Warning: ComponentA.childContextTypes is specified but there is no ' +
183
192
'getChildContext() method on the instance. You can either define ' +
184
193
'getChildContext() on ComponentA or remove childContextTypes from it.' ,
185
194
) ;
186
195
187
196
// Warnings should be deduped by component type
188
- ReactTestUtils . renderIntoDocument ( < ComponentA /> ) ;
197
+ let container = document . createElement ( 'div' ) ;
198
+ let root = ReactDOMClient . createRoot ( container ) ;
199
+ await act ( ( ) => {
200
+ root . render ( < ComponentA /> ) ;
201
+ } ) ;
189
202
190
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < ComponentB /> ) ) . toErrorDev (
203
+ await expect ( async ( ) => {
204
+ container = document . createElement ( 'div' ) ;
205
+ root = ReactDOMClient . createRoot ( container ) ;
206
+ await act ( ( ) => {
207
+ root . render ( < ComponentB /> ) ;
208
+ } ) ;
209
+ } ) . toErrorDev (
191
210
'Warning: ComponentB.childContextTypes is specified but there is no ' +
192
211
'getChildContext() method on the instance. You can either define ' +
193
212
'getChildContext() on ComponentB or remove childContextTypes from it.' ,
@@ -197,7 +216,7 @@ describe('ReactContextValidator', () => {
197
216
// TODO (bvaughn) Remove this test and the associated behavior in the future.
198
217
// It has only been added in Fiber to match the (unintentional) behavior in Stack.
199
218
// @gate !disableLegacyContext
200
- it ( 'should pass parent context if getChildContext method is missing' , ( ) => {
219
+ it ( 'should pass parent context if getChildContext method is missing' , async ( ) => {
201
220
class ParentContextProvider extends React . Component {
202
221
static childContextTypes = {
203
222
foo : PropTypes . string ,
@@ -233,9 +252,13 @@ describe('ReactContextValidator', () => {
233
252
foo : PropTypes . string . isRequired ,
234
253
} ;
235
254
236
- expect ( ( ) =>
237
- ReactTestUtils . renderIntoDocument ( < ParentContextProvider /> ) ,
238
- ) . toErrorDev ( [
255
+ await expect ( async ( ) => {
256
+ const container = document . createElement ( 'div' ) ;
257
+ const root = ReactDOMClient . createRoot ( container ) ;
258
+ await act ( ( ) => {
259
+ root . render ( < ParentContextProvider /> ) ;
260
+ } ) ;
261
+ } ) . toErrorDev ( [
239
262
'Warning: MiddleMissingContext.childContextTypes is specified but there is no ' +
240
263
'getChildContext() method on the instance. You can either define getChildContext() ' +
241
264
'on MiddleMissingContext or remove childContextTypes from it.' ,
@@ -366,7 +389,7 @@ describe('ReactContextValidator', () => {
366
389
} ) ;
367
390
368
391
// @gate !disableLegacyContext || !__DEV__
369
- it ( 'should warn if both contextType and contextTypes are defined' , ( ) => {
392
+ it ( 'should warn if both contextType and contextTypes are defined' , async ( ) => {
370
393
const Context = React . createContext ( ) ;
371
394
372
395
class ParentContextProvider extends React . Component {
@@ -402,38 +425,50 @@ describe('ReactContextValidator', () => {
402
425
}
403
426
}
404
427
405
- expect ( ( ) =>
406
- ReactTestUtils . renderIntoDocument (
407
- < ParentContextProvider >
408
- < ComponentA />
409
- </ ParentContextProvider > ,
410
- ) ,
411
- ) . toErrorDev (
428
+ await expect ( async ( ) => {
429
+ const container = document . createElement ( 'div' ) ;
430
+ const root = ReactDOMClient . createRoot ( container ) ;
431
+ await act ( ( ) => {
432
+ root . render (
433
+ < ParentContextProvider >
434
+ < ComponentA />
435
+ </ ParentContextProvider > ,
436
+ ) ;
437
+ } ) ;
438
+ } ) . toErrorDev (
412
439
'Warning: ComponentA declares both contextTypes and contextType static properties. ' +
413
440
'The legacy contextTypes property will be ignored.' ,
414
441
) ;
415
442
416
443
// Warnings should be deduped by component type
417
- ReactTestUtils . renderIntoDocument (
418
- < ParentContextProvider >
419
- < ComponentA />
420
- </ ParentContextProvider > ,
421
- ) ;
422
-
423
- expect ( ( ) =>
424
- ReactTestUtils . renderIntoDocument (
444
+ let container = document . createElement ( 'div' ) ;
445
+ let root = ReactDOMClient . createRoot ( container ) ;
446
+ await act ( ( ) => {
447
+ root . render (
425
448
< ParentContextProvider >
426
- < ComponentB />
449
+ < ComponentA />
427
450
</ ParentContextProvider > ,
428
- ) ,
429
- ) . toErrorDev (
451
+ ) ;
452
+ } ) ;
453
+
454
+ await expect ( async ( ) => {
455
+ container = document . createElement ( 'div' ) ;
456
+ root = ReactDOMClient . createRoot ( container ) ;
457
+ await act ( ( ) => {
458
+ root . render (
459
+ < ParentContextProvider >
460
+ < ComponentB />
461
+ </ ParentContextProvider > ,
462
+ ) ;
463
+ } ) ;
464
+ } ) . toErrorDev (
430
465
'Warning: ComponentB declares both contextTypes and contextType static properties. ' +
431
466
'The legacy contextTypes property will be ignored.' ,
432
467
) ;
433
468
} ) ;
434
469
435
470
// @gate enableRenderableContext || !__DEV__
436
- it ( 'should warn if an invalid contextType is defined' , ( ) => {
471
+ it ( 'should warn if an invalid contextType is defined' , async ( ) => {
437
472
const Context = React . createContext ( ) ;
438
473
class ComponentA extends React . Component {
439
474
static contextType = Context . Consumer ;
@@ -442,40 +477,54 @@ describe('ReactContextValidator', () => {
442
477
}
443
478
}
444
479
445
- expect ( ( ) => {
446
- ReactTestUtils . renderIntoDocument ( < ComponentA /> ) ;
480
+ await expect ( async ( ) => {
481
+ const container = document . createElement ( 'div' ) ;
482
+ const root = ReactDOMClient . createRoot ( container ) ;
483
+ await act ( ( ) => {
484
+ root . render ( < ComponentA /> ) ;
485
+ } ) ;
447
486
} ) . toErrorDev (
448
487
'Warning: ComponentA defines an invalid contextType. ' +
449
488
'contextType should point to the Context object returned by React.createContext(). ' +
450
489
'Did you accidentally pass the Context.Consumer instead?' ,
451
490
) ;
452
491
453
- // Warnings should be deduped by component type
454
- ReactTestUtils . renderIntoDocument ( < ComponentA /> ) ;
492
+ let container = document . createElement ( 'div' ) ;
493
+ let root = ReactDOMClient . createRoot ( container ) ;
494
+ await act ( ( ) => {
495
+ root . render ( < ComponentA /> ) ;
496
+ } ) ;
455
497
456
498
class ComponentB extends React . Component {
457
499
static contextType = Context . Provider ;
458
500
render ( ) {
459
501
return < div /> ;
460
502
}
461
503
}
462
- // This doesn't warn since Context.Provider === Context now.
463
- ReactTestUtils . renderIntoDocument ( < ComponentB /> ) ;
504
+ container = document . createElement ( 'div' ) ;
505
+ root = ReactDOMClient . createRoot ( container ) ;
506
+ await act ( ( ) => {
507
+ root . render ( < ComponentB /> ) ;
508
+ } ) ;
464
509
} ) ;
465
510
466
- it ( 'should not warn when class contextType is null' , ( ) => {
511
+ it ( 'should not warn when class contextType is null' , async ( ) => {
467
512
class Foo extends React . Component {
468
513
static contextType = null ; // Handy for conditional declaration
469
514
render ( ) {
470
515
return this . context . hello . world ;
471
516
}
472
517
}
473
- expect ( ( ) => {
474
- ReactTestUtils . renderIntoDocument ( < Foo /> ) ;
475
- } ) . toThrow ( "Cannot read property 'world' of undefined" ) ;
518
+ await expect ( async ( ) => {
519
+ const container = document . createElement ( 'div' ) ;
520
+ const root = ReactDOMClient . createRoot ( container ) ;
521
+ await act ( ( ) => {
522
+ root . render ( < Foo /> ) ;
523
+ } ) ;
524
+ } ) . rejects . toThrow ( "Cannot read properties of undefined (reading 'world')" ) ;
476
525
} ) ;
477
526
478
- it ( 'should warn when class contextType is undefined' , ( ) => {
527
+ it ( 'should warn when class contextType is undefined' , async ( ) => {
479
528
class Foo extends React . Component {
480
529
// This commonly happens with circular deps
481
530
// https://github.com/facebook/react/issues/13969
@@ -485,10 +534,16 @@ describe('ReactContextValidator', () => {
485
534
}
486
535
}
487
536
488
- expect ( ( ) => {
489
- expect ( ( ) => {
490
- ReactTestUtils . renderIntoDocument ( < Foo /> ) ;
491
- } ) . toThrow ( "Cannot read property 'world' of undefined" ) ;
537
+ await expect ( async ( ) => {
538
+ await expect ( async ( ) => {
539
+ const container = document . createElement ( 'div' ) ;
540
+ const root = ReactDOMClient . createRoot ( container ) ;
541
+ await act ( ( ) => {
542
+ root . render ( < Foo /> ) ;
543
+ } ) ;
544
+ } ) . rejects . toThrow (
545
+ "Cannot read properties of undefined (reading 'world')" ,
546
+ ) ;
492
547
} ) . toErrorDev (
493
548
'Foo defines an invalid contextType. ' +
494
549
'contextType should point to the Context object returned by React.createContext(). ' +
@@ -499,7 +554,7 @@ describe('ReactContextValidator', () => {
499
554
) ;
500
555
} ) ;
501
556
502
- it ( 'should warn when class contextType is an object' , ( ) => {
557
+ it ( 'should warn when class contextType is an object' , async ( ) => {
503
558
class Foo extends React . Component {
504
559
// Can happen due to a typo
505
560
static contextType = {
@@ -511,37 +566,49 @@ describe('ReactContextValidator', () => {
511
566
}
512
567
}
513
568
514
- expect ( ( ) => {
515
- expect ( ( ) => {
516
- ReactTestUtils . renderIntoDocument ( < Foo /> ) ;
517
- } ) . toThrow ( "Cannot read property 'hello' of undefined" ) ;
569
+ await expect ( async ( ) => {
570
+ await expect ( async ( ) => {
571
+ const container = document . createElement ( 'div' ) ;
572
+ const root = ReactDOMClient . createRoot ( container ) ;
573
+ await act ( ( ) => {
574
+ root . render ( < Foo /> ) ;
575
+ } ) ;
576
+ } ) . rejects . toThrow (
577
+ "Cannot read properties of undefined (reading 'hello')" ,
578
+ ) ;
518
579
} ) . toErrorDev (
519
580
'Foo defines an invalid contextType. ' +
520
581
'contextType should point to the Context object returned by React.createContext(). ' +
521
582
'However, it is set to an object with keys {x, y}.' ,
522
583
) ;
523
584
} ) ;
524
585
525
- it ( 'should warn when class contextType is a primitive' , ( ) => {
586
+ it ( 'should warn when class contextType is a primitive' , async ( ) => {
526
587
class Foo extends React . Component {
527
588
static contextType = 'foo' ;
528
589
render ( ) {
529
590
return this . context . hello . world ;
530
591
}
531
592
}
532
593
533
- expect ( ( ) => {
534
- expect ( ( ) => {
535
- ReactTestUtils . renderIntoDocument ( < Foo /> ) ;
536
- } ) . toThrow ( "Cannot read property 'world' of undefined" ) ;
594
+ await expect ( async ( ) => {
595
+ await expect ( async ( ) => {
596
+ const container = document . createElement ( 'div' ) ;
597
+ const root = ReactDOMClient . createRoot ( container ) ;
598
+ await act ( ( ) => {
599
+ root . render ( < Foo /> ) ;
600
+ } ) ;
601
+ } ) . rejects . toThrow (
602
+ "Cannot read properties of undefined (reading 'world')" ,
603
+ ) ;
537
604
} ) . toErrorDev (
538
605
'Foo defines an invalid contextType. ' +
539
606
'contextType should point to the Context object returned by React.createContext(). ' +
540
607
'However, it is set to a string.' ,
541
608
) ;
542
609
} ) ;
543
610
544
- it ( 'should warn if you define contextType on a function component' , ( ) => {
611
+ it ( 'should warn if you define contextType on a function component' , async ( ) => {
545
612
const Context = React . createContext ( ) ;
546
613
547
614
function ComponentA ( ) {
@@ -554,14 +621,30 @@ describe('ReactContextValidator', () => {
554
621
}
555
622
ComponentB . contextType = Context ;
556
623
557
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < ComponentA /> ) ) . toErrorDev (
624
+ await expect ( async ( ) => {
625
+ const container = document . createElement ( 'div' ) ;
626
+ const root = ReactDOMClient . createRoot ( container ) ;
627
+ await act ( ( ) => {
628
+ root . render ( < ComponentA /> ) ;
629
+ } ) ;
630
+ } ) . toErrorDev (
558
631
'Warning: ComponentA: Function components do not support contextType.' ,
559
632
) ;
560
633
561
634
// Warnings should be deduped by component type
562
- ReactTestUtils . renderIntoDocument ( < ComponentA /> ) ;
635
+ let container = document . createElement ( 'div' ) ;
636
+ let root = ReactDOMClient . createRoot ( container ) ;
637
+ await act ( ( ) => {
638
+ root . render ( < ComponentA /> ) ;
639
+ } ) ;
563
640
564
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < ComponentB /> ) ) . toErrorDev (
641
+ await expect ( async ( ) => {
642
+ container = document . createElement ( 'div' ) ;
643
+ root = ReactDOMClient . createRoot ( container ) ;
644
+ await act ( ( ) => {
645
+ root . render ( < ComponentB /> ) ;
646
+ } ) ;
647
+ } ) . toErrorDev (
565
648
'Warning: ComponentB: Function components do not support contextType.' ,
566
649
) ;
567
650
} ) ;
0 commit comments