11
11
12
12
// TODO: All these warnings should become static errors using Flow instead
13
13
// of dynamic errors when using JSX with Flow.
14
+ let act ;
14
15
let React ;
15
- let ReactTestUtils ;
16
+ let ReactDOMClient ;
16
17
17
18
describe ( 'ReactJSXElementValidator' , ( ) => {
18
19
let Component ;
@@ -21,8 +22,9 @@ describe('ReactJSXElementValidator', () => {
21
22
beforeEach ( ( ) => {
22
23
jest . resetModules ( ) ;
23
24
25
+ act = require ( 'internal-test-utils' ) . act ;
24
26
React = require ( 'react' ) ;
25
- ReactTestUtils = require ( 'react-dom/test-utils ' ) ;
27
+ ReactDOMClient = require ( 'react-dom/client ' ) ;
26
28
27
29
Component = class extends React . Component {
28
30
render ( ) {
@@ -38,15 +40,18 @@ describe('ReactJSXElementValidator', () => {
38
40
RequiredPropComponent . displayName = 'RequiredPropComponent' ;
39
41
} ) ;
40
42
41
- it ( 'warns for keys for arrays of elements in children position' , ( ) => {
42
- expect ( ( ) =>
43
- ReactTestUtils . renderIntoDocument (
44
- < Component > { [ < Component /> , < Component /> ] } </ Component > ,
45
- ) ,
46
- ) . toErrorDev ( 'Each child in a list should have a unique "key" prop.' ) ;
43
+ it ( 'warns for keys for arrays of elements in children position' , async ( ) => {
44
+ await expect ( async ( ) => {
45
+ const container = document . createElement ( 'div' ) ;
46
+ const root = ReactDOMClient . createRoot ( container ) ;
47
+
48
+ await act ( ( ) => {
49
+ root . render ( < Component > { [ < Component /> , < Component /> ] } </ Component > ) ;
50
+ } ) ;
51
+ } ) . toErrorDev ( 'Each child in a list should have a unique "key" prop.' ) ;
47
52
} ) ;
48
53
49
- it ( 'warns for keys for arrays of elements with owner info' , ( ) => {
54
+ it ( 'warns for keys for arrays of elements with owner info' , async ( ) => {
50
55
class InnerComponent extends React . Component {
51
56
render ( ) {
52
57
return < Component > { this . props . childSet } </ Component > ;
@@ -59,16 +64,20 @@ describe('ReactJSXElementValidator', () => {
59
64
}
60
65
}
61
66
62
- expect ( ( ) =>
63
- ReactTestUtils . renderIntoDocument ( < ComponentWrapper /> ) ,
64
- ) . toErrorDev (
67
+ await expect ( async ( ) => {
68
+ const container = document . createElement ( 'div' ) ;
69
+ const root = ReactDOMClient . createRoot ( container ) ;
70
+ await act ( ( ) => {
71
+ root . render ( < ComponentWrapper /> ) ;
72
+ } ) ;
73
+ } ) . toErrorDev ( [
65
74
'Each child in a list should have a unique "key" prop.' +
66
75
'\n\nCheck the render method of `InnerComponent`. ' +
67
76
'It was passed a child from ComponentWrapper. ' ,
68
- ) ;
77
+ ] ) ;
69
78
} ) ;
70
79
71
- it ( 'warns for keys for iterables of elements in rest args' , ( ) => {
80
+ it ( 'warns for keys for iterables of elements in rest args' , async ( ) => {
72
81
const iterable = {
73
82
'@@iterator' : function ( ) {
74
83
let i = 0 ;
@@ -81,18 +90,30 @@ describe('ReactJSXElementValidator', () => {
81
90
} ,
82
91
} ;
83
92
84
- expect ( ( ) =>
85
- ReactTestUtils . renderIntoDocument ( < Component > { iterable } </ Component > ) ,
86
- ) . toErrorDev ( 'Each child in a list should have a unique "key" prop.' ) ;
93
+ await expect ( async ( ) => {
94
+ const container = document . createElement ( 'div' ) ;
95
+ const root = ReactDOMClient . createRoot ( container ) ;
96
+
97
+ await act ( ( ) => {
98
+ root . render ( < Component > { iterable } </ Component > ) ;
99
+ } ) ;
100
+ } ) . toErrorDev ( 'Each child in a list should have a unique "key" prop.' ) ;
87
101
} ) ;
88
102
89
- it ( 'does not warn for arrays of elements with keys' , ( ) => {
90
- ReactTestUtils . renderIntoDocument (
91
- < Component > { [ < Component key = "#1" /> , < Component key = "#2" /> ] } </ Component > ,
92
- ) ;
103
+ it ( 'does not warn for arrays of elements with keys' , async ( ) => {
104
+ const container = document . createElement ( 'div' ) ;
105
+ const root = ReactDOMClient . createRoot ( container ) ;
106
+
107
+ await act ( ( ) => {
108
+ root . render (
109
+ < Component >
110
+ { [ < Component key = "#1" /> , < Component key = "#2" /> ] }
111
+ </ Component > ,
112
+ ) ;
113
+ } ) ;
93
114
} ) ;
94
115
95
- it ( 'does not warn for iterable elements with keys' , ( ) => {
116
+ it ( 'does not warn for iterable elements with keys' , async ( ) => {
96
117
const iterable = {
97
118
'@@iterator' : function ( ) {
98
119
let i = 0 ;
@@ -108,10 +129,15 @@ describe('ReactJSXElementValidator', () => {
108
129
} ,
109
130
} ;
110
131
111
- ReactTestUtils . renderIntoDocument ( < Component > { iterable } </ Component > ) ;
132
+ const container = document . createElement ( 'div' ) ;
133
+ const root = ReactDOMClient . createRoot ( container ) ;
134
+
135
+ await act ( ( ) => {
136
+ root . render ( < Component > { iterable } </ Component > ) ;
137
+ } ) ;
112
138
} ) ;
113
139
114
- it ( 'does not warn for numeric keys in entry iterable as a child' , ( ) => {
140
+ it ( 'does not warn for numeric keys in entry iterable as a child' , async ( ) => {
115
141
const iterable = {
116
142
'@@iterator' : function ( ) {
117
143
let i = 0 ;
@@ -125,23 +151,31 @@ describe('ReactJSXElementValidator', () => {
125
151
} ;
126
152
iterable . entries = iterable [ '@@iterator' ] ;
127
153
128
- ReactTestUtils . renderIntoDocument ( < Component > { iterable } </ Component > ) ;
154
+ const container = document . createElement ( 'div' ) ;
155
+ const root = ReactDOMClient . createRoot ( container ) ;
156
+ await act ( ( ) => {
157
+ root . render ( < Component > { iterable } </ Component > ) ;
158
+ } ) ;
129
159
} ) ;
130
160
131
- it ( 'does not warn when the element is directly as children' , ( ) => {
132
- ReactTestUtils . renderIntoDocument (
133
- < Component >
134
- < Component />
135
- < Component />
136
- </ Component > ,
137
- ) ;
161
+ it ( 'does not warn when the element is directly as children' , async ( ) => {
162
+ const container = document . createElement ( 'div' ) ;
163
+ const root = ReactDOMClient . createRoot ( container ) ;
164
+ await act ( ( ) => {
165
+ root . render (
166
+ < Component >
167
+ < Component />
168
+ < Component />
169
+ </ Component > ,
170
+ ) ;
171
+ } ) ;
138
172
} ) ;
139
173
140
174
it ( 'does not warn when the child array contains non-elements' , ( ) => {
141
175
void ( < Component > { [ { } , { } ] } </ Component > ) ;
142
176
} ) ;
143
177
144
- it ( 'should give context for errors in nested components.' , ( ) => {
178
+ it ( 'should give context for errors in nested components.' , async ( ) => {
145
179
class MyComp extends React . Component {
146
180
render ( ) {
147
181
return [ < div /> ] ;
@@ -152,7 +186,14 @@ describe('ReactJSXElementValidator', () => {
152
186
return < MyComp /> ;
153
187
}
154
188
}
155
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < ParentComp /> ) ) . toErrorDev (
189
+ await expect ( async ( ) => {
190
+ const container = document . createElement ( 'div' ) ;
191
+ const root = ReactDOMClient . createRoot ( container ) ;
192
+
193
+ await act ( ( ) => {
194
+ root . render ( < ParentComp /> ) ;
195
+ } ) ;
196
+ } ) . toErrorDev (
156
197
'Each child in a list should have a unique "key" prop. ' +
157
198
'See https://reactjs.org/link/warning-keys for more information.\n' +
158
199
' in MyComp (at **)\n' +
@@ -189,20 +230,26 @@ describe('ReactJSXElementValidator', () => {
189
230
void ( < Div /> ) ;
190
231
} ) ;
191
232
192
- it ( 'warns for fragments with illegal attributes' , ( ) => {
233
+ it ( 'warns for fragments with illegal attributes' , async ( ) => {
193
234
class Foo extends React . Component {
194
235
render ( ) {
195
236
return < React . Fragment a = { 1 } > hello</ React . Fragment > ;
196
237
}
197
238
}
198
239
199
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Foo /> ) ) . toErrorDev (
240
+ await expect ( async ( ) => {
241
+ const container = document . createElement ( 'div' ) ;
242
+ const root = ReactDOMClient . createRoot ( container ) ;
243
+ await act ( ( ) => {
244
+ root . render ( < Foo /> ) ;
245
+ } ) ;
246
+ } ) . toErrorDev (
200
247
'Invalid prop `a` supplied to `React.Fragment`. React.Fragment ' +
201
248
'can only have `key` and `children` props.' ,
202
249
) ;
203
250
} ) ;
204
251
205
- it ( 'warns for fragments with refs' , ( ) => {
252
+ it ( 'warns for fragments with refs' , async ( ) => {
206
253
class Foo extends React . Component {
207
254
render ( ) {
208
255
return (
@@ -217,35 +264,51 @@ describe('ReactJSXElementValidator', () => {
217
264
}
218
265
219
266
if ( gate ( flags => flags . enableRefAsProp ) ) {
220
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Foo /> ) ) . toErrorDev (
221
- 'Invalid prop `ref` supplied to `React.Fragment`.' ,
222
- ) ;
267
+ await expect ( async ( ) => {
268
+ const container = document . createElement ( 'div' ) ;
269
+ const root = ReactDOMClient . createRoot ( container ) ;
270
+ await act ( ( ) => {
271
+ root . render ( < Foo /> ) ;
272
+ } ) ;
273
+ } ) . toErrorDev ( 'Invalid prop `ref` supplied to `React.Fragment`.' ) ;
223
274
} else {
224
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Foo /> ) ) . toErrorDev (
225
- 'Invalid attribute `ref` supplied to `React.Fragment`.' ,
226
- ) ;
275
+ await expect ( async ( ) => {
276
+ const container = document . createElement ( 'div' ) ;
277
+ const root = ReactDOMClient . createRoot ( container ) ;
278
+ await act ( ( ) => {
279
+ root . render ( < Foo /> ) ;
280
+ } ) ;
281
+ } ) . toErrorDev ( 'Invalid attribute `ref` supplied to `React.Fragment`.' ) ;
227
282
}
228
283
} ) ;
229
284
230
- it ( 'does not warn for fragments of multiple elements without keys' , ( ) => {
231
- ReactTestUtils . renderIntoDocument (
232
- < >
233
- < span > 1</ span >
234
- < span > 2</ span >
235
- </ > ,
236
- ) ;
237
- } ) ;
238
-
239
- it ( 'warns for fragments of multiple elements with same key' , ( ) => {
240
- expect ( ( ) =>
241
- ReactTestUtils . renderIntoDocument (
285
+ it ( 'does not warn for fragments of multiple elements without keys' , async ( ) => {
286
+ const container = document . createElement ( 'div' ) ;
287
+ const root = ReactDOMClient . createRoot ( container ) ;
288
+ await act ( ( ) => {
289
+ root . render (
242
290
< >
243
- < span key = "a" > 1</ span >
244
- < span key = "a" > 2</ span >
245
- < span key = "b" > 3</ span >
291
+ < span > 1</ span >
292
+ < span > 2</ span >
246
293
</ > ,
247
- ) ,
248
- ) . toErrorDev ( 'Encountered two children with the same key, `a`.' , {
294
+ ) ;
295
+ } ) ;
296
+ } ) ;
297
+
298
+ it ( 'warns for fragments of multiple elements with same key' , async ( ) => {
299
+ await expect ( async ( ) => {
300
+ const container = document . createElement ( 'div' ) ;
301
+ const root = ReactDOMClient . createRoot ( container ) ;
302
+ await act ( ( ) => {
303
+ root . render (
304
+ < >
305
+ < span key = "a" > 1</ span >
306
+ < span key = "a" > 2</ span >
307
+ < span key = "b" > 3</ span >
308
+ </ > ,
309
+ ) ;
310
+ } ) ;
311
+ } ) . toErrorDev ( 'Encountered two children with the same key, `a`.' , {
249
312
withoutStack : true ,
250
313
} ) ;
251
314
} ) ;
0 commit comments