11
11
12
12
let PropTypes ;
13
13
let React ;
14
- let ReactDOM ;
14
+ let ReactDOMClient ;
15
15
let ReactTestUtils ;
16
+ let act ;
16
17
17
18
function FunctionComponent ( props ) {
18
19
return < div > { props . name } </ div > ;
@@ -23,44 +24,58 @@ describe('ReactFunctionComponent', () => {
23
24
jest . resetModules ( ) ;
24
25
PropTypes = require ( 'prop-types' ) ;
25
26
React = require ( 'react' ) ;
26
- ReactDOM = require ( 'react-dom' ) ;
27
+ ReactDOMClient = require ( 'react-dom/client' ) ;
28
+ act = require ( 'internal-test-utils' ) . act ;
27
29
ReactTestUtils = require ( 'react-dom/test-utils' ) ;
28
30
} ) ;
29
31
30
- it ( 'should render stateless component' , ( ) => {
32
+ it ( 'should render stateless component' , async ( ) => {
31
33
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
+ } ) ;
33
39
34
40
expect ( el . textContent ) . toBe ( 'A' ) ;
35
41
} ) ;
36
42
37
- it ( 'should update stateless component' , ( ) => {
43
+ it ( 'should update stateless component' , async ( ) => {
38
44
class Parent extends React . Component {
39
45
render ( ) {
40
46
return < FunctionComponent { ...this . props } /> ;
41
47
}
42
48
}
43
49
44
50
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
+ } ) ;
46
56
expect ( el . textContent ) . toBe ( 'A' ) ;
47
57
48
- ReactDOM . render ( < Parent name = "B" /> , el ) ;
58
+ await act ( ( ) => {
59
+ root . render ( < Parent name = "B" /> ) ;
60
+ } ) ;
49
61
expect ( el . textContent ) . toBe ( 'B' ) ;
50
62
} ) ;
51
63
52
- it ( 'should unmount stateless component' , ( ) => {
64
+ it ( 'should unmount stateless component' , async ( ) => {
53
65
const container = document . createElement ( 'div' ) ;
54
66
55
- ReactDOM . render ( < FunctionComponent name = "A" /> , container ) ;
67
+ const root = ReactDOMClient . createRoot ( container ) ;
68
+ await act ( ( ) => {
69
+ root . render ( < FunctionComponent name = "A" /> ) ;
70
+ } ) ;
56
71
expect ( container . textContent ) . toBe ( 'A' ) ;
57
72
58
- ReactDOM . unmountComponentAtNode ( container ) ;
73
+ root . unmount ( ) ;
59
74
expect ( container . textContent ) . toBe ( '' ) ;
60
75
} ) ;
61
76
62
77
// @gate !disableLegacyContext
63
- it ( 'should pass context thru stateless component' , ( ) => {
78
+ it ( 'should pass context thru stateless component' , async ( ) => {
64
79
class Child extends React . Component {
65
80
static contextTypes = {
66
81
test : PropTypes . string . isRequired ,
@@ -90,32 +105,41 @@ describe('ReactFunctionComponent', () => {
90
105
}
91
106
92
107
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
+ } ) ;
94
113
95
114
expect ( el . textContent ) . toBe ( 'test' ) ;
96
115
97
- ReactDOM . render ( < GrandParent test = "mest" /> , el ) ;
116
+ await act ( ( ) => {
117
+ root . render ( < GrandParent test = "mest" /> ) ;
118
+ } ) ;
98
119
99
120
expect ( el . textContent ) . toBe ( 'mest' ) ;
100
121
} ) ;
101
122
102
- it ( 'should warn for getDerivedStateFromProps on a function component' , ( ) => {
123
+ it ( 'should warn for getDerivedStateFromProps on a function component' , async ( ) => {
103
124
function FunctionComponentWithChildContext ( ) {
104
125
return null ;
105
126
}
106
127
FunctionComponentWithChildContext . getDerivedStateFromProps = function ( ) { } ;
107
128
108
129
const container = document . createElement ( 'div' ) ;
109
130
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 (
113
137
'FunctionComponentWithChildContext: Function ' +
114
138
'components do not support getDerivedStateFromProps.' ,
115
139
) ;
116
140
} ) ;
117
141
118
- it ( 'should warn for childContextTypes on a function component' , ( ) => {
142
+ it ( 'should warn for childContextTypes on a function component' , async ( ) => {
119
143
function FunctionComponentWithChildContext ( props ) {
120
144
return < div > { props . name } </ div > ;
121
145
}
@@ -126,12 +150,12 @@ describe('ReactFunctionComponent', () => {
126
150
127
151
const container = document . createElement ( 'div' ) ;
128
152
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 (
135
159
'FunctionComponentWithChildContext(...): childContextTypes cannot ' +
136
160
'be defined on a function component.' ,
137
161
) ;
@@ -378,7 +402,7 @@ describe('ReactFunctionComponent', () => {
378
402
} ) ;
379
403
380
404
// @gate !disableLegacyContext
381
- it ( 'should receive context' , ( ) => {
405
+ it ( 'should receive context' , async ( ) => {
382
406
class Parent extends React . Component {
383
407
static childContextTypes = {
384
408
lang : PropTypes . string ,
@@ -399,7 +423,11 @@ describe('ReactFunctionComponent', () => {
399
423
Child . contextTypes = { lang : PropTypes . string } ;
400
424
401
425
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
+ } ) ;
403
431
expect ( el . textContent ) . toBe ( 'en' ) ;
404
432
} ) ;
405
433
0 commit comments