13
13
'use strict' ;
14
14
15
15
let React ;
16
- let ReactTestUtils ;
16
+ let ReactDOMClient ;
17
+ let act ;
17
18
18
19
describe ( 'ReactChildReconciler' , ( ) => {
19
20
beforeEach ( ( ) => {
20
21
jest . resetModules ( ) ;
21
22
22
23
React = require ( 'react' ) ;
23
- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
24
+ ReactDOMClient = require ( 'react-dom/client' ) ;
25
+ act = require ( 'internal-test-utils' ) . act ;
24
26
} ) ;
25
27
26
28
function createIterable ( array ) {
@@ -55,37 +57,47 @@ describe('ReactChildReconciler', () => {
55
57
return fn ;
56
58
}
57
59
58
- it ( 'does not treat functions as iterables' , ( ) => {
59
- let node ;
60
+ it ( 'does not treat functions as iterables' , async ( ) => {
60
61
const iterableFunction = makeIterableFunction ( 'foo' ) ;
61
62
62
- expect ( ( ) => {
63
- node = ReactTestUtils . renderIntoDocument (
64
- < div >
65
- < h1 > { iterableFunction } </ h1 >
66
- </ div > ,
67
- ) ;
63
+ const container = document . createElement ( 'div' ) ;
64
+ const root = ReactDOMClient . createRoot ( container ) ;
65
+ await expect ( async ( ) => {
66
+ await act ( ( ) => {
67
+ root . render (
68
+ < div >
69
+ < h1 > { iterableFunction } </ h1 >
70
+ </ div > ,
71
+ ) ;
72
+ } ) ;
68
73
} ) . toErrorDev ( 'Functions are not valid as a React child' ) ;
74
+ const node = container . firstChild ;
69
75
70
76
expect ( node . innerHTML ) . toContain ( '' ) ; // h1
71
77
} ) ;
72
78
73
- it ( 'warns for duplicated array keys' , ( ) => {
79
+ it ( 'warns for duplicated array keys' , async ( ) => {
74
80
class Component extends React . Component {
75
81
render ( ) {
76
82
return < div > { [ < div key = "1" /> , < div key = "1" /> ] } </ div > ;
77
83
}
78
84
}
79
85
80
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Component /> ) ) . toErrorDev (
86
+ const container = document . createElement ( 'div' ) ;
87
+ const root = ReactDOMClient . createRoot ( container ) ;
88
+ await expect ( async ( ) => {
89
+ await act ( ( ) => {
90
+ root . render ( < Component /> ) ;
91
+ } ) ;
92
+ } ) . toErrorDev (
81
93
'Keys should be unique so that components maintain their identity ' +
82
94
'across updates. Non-unique keys may cause children to be ' +
83
95
'duplicated and/or omitted — the behavior is unsupported and ' +
84
96
'could change in a future version.' ,
85
97
) ;
86
98
} ) ;
87
99
88
- it ( 'warns for duplicated array keys with component stack info' , ( ) => {
100
+ it ( 'warns for duplicated array keys with component stack info' , async ( ) => {
89
101
class Component extends React . Component {
90
102
render ( ) {
91
103
return < div > { [ < div key = "1" /> , < div key = "1" /> ] } </ div > ;
@@ -104,7 +116,13 @@ describe('ReactChildReconciler', () => {
104
116
}
105
117
}
106
118
107
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < GrandParent /> ) ) . toErrorDev (
119
+ const container = document . createElement ( 'div' ) ;
120
+ const root = ReactDOMClient . createRoot ( container ) ;
121
+ await expect ( async ( ) => {
122
+ await act ( ( ) => {
123
+ root . render ( < GrandParent /> ) ;
124
+ } ) ;
125
+ } ) . toErrorDev (
108
126
'Encountered two children with the same key, `1`. ' +
109
127
'Keys should be unique so that components maintain their identity ' +
110
128
'across updates. Non-unique keys may cause children to be ' +
@@ -117,22 +135,28 @@ describe('ReactChildReconciler', () => {
117
135
) ;
118
136
} ) ;
119
137
120
- it ( 'warns for duplicated iterable keys' , ( ) => {
138
+ it ( 'warns for duplicated iterable keys' , async ( ) => {
121
139
class Component extends React . Component {
122
140
render ( ) {
123
141
return < div > { createIterable ( [ < div key = "1" /> , < div key = "1" /> ] ) } </ div > ;
124
142
}
125
143
}
126
144
127
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < Component /> ) ) . toErrorDev (
145
+ const container = document . createElement ( 'div' ) ;
146
+ const root = ReactDOMClient . createRoot ( container ) ;
147
+ await expect ( async ( ) => {
148
+ await act ( ( ) => {
149
+ root . render ( < Component /> ) ;
150
+ } ) ;
151
+ } ) . toErrorDev (
128
152
'Keys should be unique so that components maintain their identity ' +
129
153
'across updates. Non-unique keys may cause children to be ' +
130
154
'duplicated and/or omitted — the behavior is unsupported and ' +
131
155
'could change in a future version.' ,
132
156
) ;
133
157
} ) ;
134
158
135
- it ( 'warns for duplicated iterable keys with component stack info' , ( ) => {
159
+ it ( 'warns for duplicated iterable keys with component stack info' , async ( ) => {
136
160
class Component extends React . Component {
137
161
render ( ) {
138
162
return < div > { createIterable ( [ < div key = "1" /> , < div key = "1" /> ] ) } </ div > ;
@@ -151,7 +175,13 @@ describe('ReactChildReconciler', () => {
151
175
}
152
176
}
153
177
154
- expect ( ( ) => ReactTestUtils . renderIntoDocument ( < GrandParent /> ) ) . toErrorDev (
178
+ const container = document . createElement ( 'div' ) ;
179
+ const root = ReactDOMClient . createRoot ( container ) ;
180
+ await expect ( async ( ) => {
181
+ await act ( ( ) => {
182
+ root . render ( < GrandParent /> ) ;
183
+ } ) ;
184
+ } ) . toErrorDev (
155
185
'Encountered two children with the same key, `1`. ' +
156
186
'Keys should be unique so that components maintain their identity ' +
157
187
'across updates. Non-unique keys may cause children to be ' +
0 commit comments