1111
1212let PropTypes ;
1313let React ;
14- let ReactDOM ;
14+ let ReactDOMClient ;
1515let ReactTestUtils ;
16+ let act ;
1617
1718function FunctionComponent ( props ) {
1819 return < div > { props . name } </ div > ;
@@ -23,44 +24,58 @@ describe('ReactFunctionComponent', () => {
2324 jest . resetModules ( ) ;
2425 PropTypes = require ( 'prop-types' ) ;
2526 React = require ( 'react' ) ;
26- ReactDOM = require ( 'react-dom' ) ;
27+ ReactDOMClient = require ( 'react-dom/client' ) ;
28+ act = require ( 'internal-test-utils' ) . act ;
2729 ReactTestUtils = require ( 'react-dom/test-utils' ) ;
2830 } ) ;
2931
30- it ( 'should render stateless component' , ( ) => {
32+ it ( 'should render stateless component' , async ( ) => {
3133 const el = document . createElement ( 'div' ) ;
32- ReactDOM . render ( < FunctionComponent name = "A" /> , el ) ;
34+
35+ const root = ReactDOMClient . createRoot ( el ) ;
36+ await act ( ( ) => {
37+ root . render ( < FunctionComponent name = "A" /> ) ;
38+ } ) ;
3339
3440 expect ( el . textContent ) . toBe ( 'A' ) ;
3541 } ) ;
3642
37- it ( 'should update stateless component' , ( ) => {
43+ it ( 'should update stateless component' , async ( ) => {
3844 class Parent extends React . Component {
3945 render ( ) {
4046 return < FunctionComponent { ...this . props } /> ;
4147 }
4248 }
4349
4450 const el = document . createElement ( 'div' ) ;
45- ReactDOM . render ( < Parent name = "A" /> , el ) ;
51+
52+ const root = ReactDOMClient . createRoot ( el ) ;
53+ await act ( ( ) => {
54+ root . render ( < Parent name = "A" /> ) ;
55+ } ) ;
4656 expect ( el . textContent ) . toBe ( 'A' ) ;
4757
48- ReactDOM . render ( < Parent name = "B" /> , el ) ;
58+ await act ( ( ) => {
59+ root . render ( < Parent name = "B" /> ) ;
60+ } ) ;
4961 expect ( el . textContent ) . toBe ( 'B' ) ;
5062 } ) ;
5163
52- it ( 'should unmount stateless component' , ( ) => {
64+ it ( 'should unmount stateless component' , async ( ) => {
5365 const container = document . createElement ( 'div' ) ;
5466
55- ReactDOM . render ( < FunctionComponent name = "A" /> , container ) ;
67+ const root = ReactDOMClient . createRoot ( container ) ;
68+ await act ( ( ) => {
69+ root . render ( < FunctionComponent name = "A" /> ) ;
70+ } ) ;
5671 expect ( container . textContent ) . toBe ( 'A' ) ;
5772
58- ReactDOM . unmountComponentAtNode ( container ) ;
73+ root . unmount ( ) ;
5974 expect ( container . textContent ) . toBe ( '' ) ;
6075 } ) ;
6176
6277 // @gate !disableLegacyContext
63- it ( 'should pass context thru stateless component' , ( ) => {
78+ it ( 'should pass context thru stateless component' , async ( ) => {
6479 class Child extends React . Component {
6580 static contextTypes = {
6681 test : PropTypes . string . isRequired ,
@@ -90,32 +105,41 @@ describe('ReactFunctionComponent', () => {
90105 }
91106
92107 const el = document . createElement ( 'div' ) ;
93- ReactDOM . render ( < GrandParent test = "test" /> , el ) ;
108+
109+ const root = ReactDOMClient . createRoot ( el ) ;
110+ await act ( ( ) => {
111+ root . render ( < GrandParent test = "test" /> ) ;
112+ } ) ;
94113
95114 expect ( el . textContent ) . toBe ( 'test' ) ;
96115
97- ReactDOM . render ( < GrandParent test = "mest" /> , el ) ;
116+ await act ( ( ) => {
117+ root . render ( < GrandParent test = "mest" /> ) ;
118+ } ) ;
98119
99120 expect ( el . textContent ) . toBe ( 'mest' ) ;
100121 } ) ;
101122
102- it ( 'should warn for getDerivedStateFromProps on a function component' , ( ) => {
123+ it ( 'should warn for getDerivedStateFromProps on a function component' , async ( ) => {
103124 function FunctionComponentWithChildContext ( ) {
104125 return null ;
105126 }
106127 FunctionComponentWithChildContext . getDerivedStateFromProps = function ( ) { } ;
107128
108129 const container = document . createElement ( 'div' ) ;
109130
110- expect ( ( ) =>
111- ReactDOM . render ( < FunctionComponentWithChildContext /> , container ) ,
112- ) . toErrorDev (
131+ await expect ( async ( ) => {
132+ const root = ReactDOMClient . createRoot ( container ) ;
133+ await act ( ( ) => {
134+ root . render ( < FunctionComponentWithChildContext /> ) ;
135+ } ) ;
136+ } ) . toErrorDev (
113137 'FunctionComponentWithChildContext: Function ' +
114138 'components do not support getDerivedStateFromProps.' ,
115139 ) ;
116140 } ) ;
117141
118- it ( 'should warn for childContextTypes on a function component' , ( ) => {
142+ it ( 'should warn for childContextTypes on a function component' , async ( ) => {
119143 function FunctionComponentWithChildContext ( props ) {
120144 return < div > { props . name } </ div > ;
121145 }
@@ -126,12 +150,12 @@ describe('ReactFunctionComponent', () => {
126150
127151 const container = document . createElement ( 'div' ) ;
128152
129- expect ( ( ) =>
130- ReactDOM . render (
131- < FunctionComponentWithChildContext name = "A" /> ,
132- container ,
133- ) ,
134- ) . toErrorDev (
153+ await expect ( async ( ) => {
154+ const root = ReactDOMClient . createRoot ( container ) ;
155+ await act ( ( ) => {
156+ root . render ( < FunctionComponentWithChildContext name = "A" /> ) ;
157+ } ) ;
158+ } ) . toErrorDev (
135159 'FunctionComponentWithChildContext(...): childContextTypes cannot ' +
136160 'be defined on a function component.' ,
137161 ) ;
@@ -378,7 +402,7 @@ describe('ReactFunctionComponent', () => {
378402 } ) ;
379403
380404 // @gate !disableLegacyContext
381- it ( 'should receive context' , ( ) => {
405+ it ( 'should receive context' , async ( ) => {
382406 class Parent extends React . Component {
383407 static childContextTypes = {
384408 lang : PropTypes . string ,
@@ -399,7 +423,11 @@ describe('ReactFunctionComponent', () => {
399423 Child . contextTypes = { lang : PropTypes . string } ;
400424
401425 const el = document . createElement ( 'div' ) ;
402- ReactDOM . render ( < Parent /> , el ) ;
426+
427+ const root = ReactDOMClient . createRoot ( el ) ;
428+ await act ( ( ) => {
429+ root . render ( < Parent /> ) ;
430+ } ) ;
403431 expect ( el . textContent ) . toBe ( 'en' ) ;
404432 } ) ;
405433
0 commit comments