9
9
10
10
'use strict' ;
11
11
12
+ let act ;
12
13
let PropTypes ;
13
14
let React ;
14
- let ReactDOM ;
15
- let ReactTestUtils ;
15
+ let ReactDOMClient ;
16
16
17
17
describe ( 'ReactElementClone' , ( ) => {
18
18
let ComponentClass ;
19
19
20
20
beforeEach ( ( ) => {
21
+ act = require ( 'internal-test-utils' ) . act ;
22
+
21
23
PropTypes = require ( 'prop-types' ) ;
22
24
React = require ( 'react' ) ;
23
- ReactDOM = require ( 'react-dom' ) ;
24
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
25
+ ReactDOMClient = require ( 'react-dom/client' ) ;
25
26
26
27
// NOTE: We're explicitly not using JSX here. This is intended to test
27
28
// classic JS without JSX.
@@ -32,10 +33,15 @@ describe('ReactElementClone', () => {
32
33
} ;
33
34
} ) ;
34
35
35
- it ( 'should clone a DOM component with new props' , ( ) => {
36
+ it ( 'should clone a DOM component with new props' , async ( ) => {
37
+ let div ;
36
38
class Grandparent extends React . Component {
37
39
render ( ) {
38
- return < Parent child = { < div className = "child" /> } /> ;
40
+ return (
41
+ < Parent
42
+ child = { < div ref = { node => ( div = node ) } className = "child" /> }
43
+ />
44
+ ) ;
39
45
}
40
46
}
41
47
class Parent extends React . Component {
@@ -47,14 +53,21 @@ describe('ReactElementClone', () => {
47
53
) ;
48
54
}
49
55
}
50
- const component = ReactTestUtils . renderIntoDocument ( < Grandparent /> ) ;
51
- expect ( ReactDOM . findDOMNode ( component ) . childNodes [ 0 ] . className ) . toBe ( 'xyz' ) ;
56
+
57
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
58
+ await act ( ( ) => {
59
+ root . render ( < Grandparent /> ) ;
60
+ } ) ;
61
+ expect ( div . className ) . toBe ( 'xyz' ) ;
52
62
} ) ;
53
63
54
- it ( 'should clone a composite component with new props' , ( ) => {
64
+ it ( 'should clone a composite component with new props' , async ( ) => {
65
+ let div ;
55
66
class Child extends React . Component {
56
67
render ( ) {
57
- return < div className = { this . props . className } /> ;
68
+ return (
69
+ < div ref = { node => ( div = node ) } className = { this . props . className } />
70
+ ) ;
58
71
}
59
72
}
60
73
class Grandparent extends React . Component {
@@ -71,19 +84,27 @@ describe('ReactElementClone', () => {
71
84
) ;
72
85
}
73
86
}
74
- const component = ReactTestUtils . renderIntoDocument ( < Grandparent /> ) ;
75
- expect ( ReactDOM . findDOMNode ( component ) . childNodes [ 0 ] . className ) . toBe ( 'xyz' ) ;
87
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
88
+ await act ( ( ) => {
89
+ root . render ( < Grandparent /> ) ;
90
+ } ) ;
91
+ expect ( div . className ) . toBe ( 'xyz' ) ;
76
92
} ) ;
77
93
78
94
it ( 'does not fail if config has no prototype' , ( ) => {
79
95
const config = Object . create ( null , { foo : { value : 1 , enumerable : true } } ) ;
80
96
React . cloneElement ( < div /> , config ) ;
81
97
} ) ;
82
98
83
- it ( 'should keep the original ref if it is not overridden' , ( ) => {
99
+ it ( 'should keep the original ref if it is not overridden' , async ( ) => {
100
+ let component ;
84
101
class Grandparent extends React . Component {
85
102
yoloRef = React . createRef ( ) ;
86
103
104
+ componentDidMount ( ) {
105
+ component = this ;
106
+ }
107
+
87
108
render ( ) {
88
109
return < Parent child = { < div ref = { this . yoloRef } /> } /> ;
89
110
}
@@ -97,7 +118,11 @@ describe('ReactElementClone', () => {
97
118
}
98
119
}
99
120
100
- const component = ReactTestUtils . renderIntoDocument ( < Grandparent /> ) ;
121
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
122
+ await act ( ( ) => {
123
+ root . render ( < Grandparent /> ) ;
124
+ } ) ;
125
+
101
126
expect ( component . yoloRef . current . tagName ) . toBe ( 'DIV' ) ;
102
127
} ) ;
103
128
@@ -111,30 +136,32 @@ describe('ReactElementClone', () => {
111
136
expect ( clone . key ) . toBe ( 'xyz' ) ;
112
137
} ) ;
113
138
114
- it ( 'should transfer children' , ( ) => {
139
+ it ( 'should transfer children' , async ( ) => {
115
140
class Component extends React . Component {
116
141
render ( ) {
117
142
expect ( this . props . children ) . toBe ( 'xyz' ) ;
118
143
return < div /> ;
119
144
}
120
145
}
121
146
122
- ReactTestUtils . renderIntoDocument (
123
- React . cloneElement ( < Component /> , { children : 'xyz' } ) ,
124
- ) ;
147
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
148
+ await act ( ( ) => {
149
+ root . render ( React . cloneElement ( < Component /> , { children : 'xyz' } ) ) ;
150
+ } ) ;
125
151
} ) ;
126
152
127
- it ( 'should shallow clone children' , ( ) => {
153
+ it ( 'should shallow clone children' , async ( ) => {
128
154
class Component extends React . Component {
129
155
render ( ) {
130
156
expect ( this . props . children ) . toBe ( 'xyz' ) ;
131
157
return < div /> ;
132
158
}
133
159
}
134
160
135
- ReactTestUtils . renderIntoDocument (
136
- React . cloneElement ( < Component > xyz</ Component > , { } ) ,
137
- ) ;
161
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
162
+ await act ( ( ) => {
163
+ root . render ( React . cloneElement ( < Component > xyz</ Component > , { } ) ) ;
164
+ } ) ;
138
165
} ) ;
139
166
140
167
it ( 'should accept children as rest arguments' , ( ) => {
@@ -174,7 +201,8 @@ describe('ReactElementClone', () => {
174
201
expect ( element2 . props . children ) . toBe ( undefined ) ;
175
202
} ) ;
176
203
177
- it ( 'should support keys and refs' , ( ) => {
204
+ it ( 'should support keys and refs' , async ( ) => {
205
+ let component ;
178
206
class Parent extends React . Component {
179
207
xyzRef = React . createRef ( ) ;
180
208
@@ -192,6 +220,10 @@ describe('ReactElementClone', () => {
192
220
class Grandparent extends React . Component {
193
221
parentRef = React . createRef ( ) ;
194
222
223
+ componentDidMount ( ) {
224
+ component = this ;
225
+ }
226
+
195
227
render ( ) {
196
228
return (
197
229
< Parent ref = { this . parentRef } >
@@ -201,11 +233,13 @@ describe('ReactElementClone', () => {
201
233
}
202
234
}
203
235
204
- const component = ReactTestUtils . renderIntoDocument ( < Grandparent /> ) ;
236
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
237
+ await act ( ( ) => root . render ( < Grandparent /> ) ) ;
205
238
expect ( component . parentRef . current . xyzRef . current . tagName ) . toBe ( 'SPAN' ) ;
206
239
} ) ;
207
240
208
- it ( 'should steal the ref if a new ref is specified' , ( ) => {
241
+ it ( 'should steal the ref if a new ref is specified' , async ( ) => {
242
+ let component ;
209
243
class Parent extends React . Component {
210
244
xyzRef = React . createRef ( ) ;
211
245
@@ -221,6 +255,10 @@ describe('ReactElementClone', () => {
221
255
parentRef = React . createRef ( ) ;
222
256
childRef = React . createRef ( ) ;
223
257
258
+ componentDidMount ( ) {
259
+ component = this ;
260
+ }
261
+
224
262
render ( ) {
225
263
return (
226
264
< Parent ref = { this . parentRef } >
@@ -230,21 +268,25 @@ describe('ReactElementClone', () => {
230
268
}
231
269
}
232
270
233
- const component = ReactTestUtils . renderIntoDocument ( < Grandparent /> ) ;
271
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
272
+ await act ( ( ) => root . render ( < Grandparent /> ) ) ;
234
273
expect ( component . childRef ) . toEqual ( { current : null } ) ;
235
274
expect ( component . parentRef . current . xyzRef . current . tagName ) . toBe ( 'SPAN' ) ;
236
275
} ) ;
237
276
238
- it ( 'should overwrite props' , ( ) => {
277
+ it ( 'should overwrite props' , async ( ) => {
239
278
class Component extends React . Component {
240
279
render ( ) {
241
280
expect ( this . props . myprop ) . toBe ( 'xyz' ) ;
242
281
return < div /> ;
243
282
}
244
283
}
245
284
246
- ReactTestUtils . renderIntoDocument (
247
- React . cloneElement ( < Component myprop = "abc" /> , { myprop : 'xyz' } ) ,
285
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
286
+ await act ( ( ) =>
287
+ root . render (
288
+ React . cloneElement ( < Component myprop = "abc" /> , { myprop : 'xyz' } ) ,
289
+ ) ,
248
290
) ;
249
291
} ) ;
250
292
@@ -287,7 +329,7 @@ describe('ReactElementClone', () => {
287
329
React . cloneElement ( < div /> , null , [ { } , { } ] ) ;
288
330
} ) ;
289
331
290
- it ( 'should check declared prop types after clone' , ( ) => {
332
+ it ( 'should check declared prop types after clone' , async ( ) => {
291
333
class Component extends React . Component {
292
334
static propTypes = {
293
335
color : PropTypes . string . isRequired ,
@@ -308,8 +350,10 @@ describe('ReactElementClone', () => {
308
350
} ) ;
309
351
}
310
352
}
311
- expect ( ( ) =>
312
- ReactTestUtils . renderIntoDocument ( React . createElement ( GrandParent ) ) ,
353
+ const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
354
+ await expect (
355
+ async ( ) =>
356
+ await act ( ( ) => root . render ( React . createElement ( GrandParent ) ) ) ,
313
357
) . toErrorDev (
314
358
'Warning: Failed prop type: ' +
315
359
'Invalid prop `color` of type `number` supplied to `Component`, ' +
0 commit comments