@@ -113,17 +113,29 @@ describe('ReactComponentLifeCycle', () => {
113
113
}
114
114
115
115
const element = < StatefulComponent /> ;
116
- const firstInstance = ReactDOM . render ( element , container ) ;
117
- ReactDOM . unmountComponentAtNode ( container ) ;
118
- const secondInstance = ReactDOM . render ( element , container ) ;
116
+ let root = ReactDOMClient . createRoot ( container ) ;
117
+ await act ( ( ) => {
118
+ root . render ( element ) ;
119
+ } ) ;
120
+
121
+ const firstInstance = container . firstChild ;
122
+ await act ( ( ) => {
123
+ root . unmount ( ) ;
124
+ } ) ;
125
+ root = ReactDOMClient . createRoot ( container ) ;
126
+ await act ( ( ) => {
127
+ root . render ( element ) ;
128
+ } ) ;
129
+
130
+ const secondInstance = container . firstChild ;
119
131
expect ( firstInstance ) . not . toBe ( secondInstance ) ;
120
132
} ) ;
121
133
122
134
/**
123
135
* If a state update triggers rerendering that in turn fires an onDOMReady,
124
136
* that second onDOMReady should not fail.
125
137
*/
126
- it ( 'it should fire onDOMReady when already in onDOMReady' , ( ) => {
138
+ it ( 'it should fire onDOMReady when already in onDOMReady' , async ( ) => {
127
139
const _testJournal = [ ] ;
128
140
129
141
class Child extends React . Component {
@@ -161,7 +173,13 @@ describe('ReactComponentLifeCycle', () => {
161
173
}
162
174
}
163
175
164
- ReactTestUtils . renderIntoDocument ( < SwitcherParent /> ) ;
176
+ const container = document . createElement ( 'div' ) ;
177
+ const root = ReactDOMClient . createRoot ( container ) ;
178
+
179
+ await act ( ( ) => {
180
+ root . render ( < SwitcherParent /> ) ;
181
+ } ) ;
182
+
165
183
expect ( _testJournal ) . toEqual ( [
166
184
'SwitcherParent:getInitialState' ,
167
185
'SwitcherParent:onDOMReady' ,
@@ -205,7 +223,7 @@ describe('ReactComponentLifeCycle', () => {
205
223
} ) . not . toThrow ( ) ;
206
224
} ) ;
207
225
208
- it ( "warns if setting 'this.state = props'" , ( ) => {
226
+ it ( "warns if setting 'this.state = props'" , async ( ) => {
209
227
class StatefulComponent extends React . Component {
210
228
constructor ( props , context ) {
211
229
super ( props , context ) ;
@@ -216,16 +234,20 @@ describe('ReactComponentLifeCycle', () => {
216
234
}
217
235
}
218
236
219
- expect ( ( ) => {
220
- ReactTestUtils . renderIntoDocument ( < StatefulComponent /> ) ;
237
+ const container = document . createElement ( 'div' ) ;
238
+ const root = ReactDOMClient . createRoot ( container ) ;
239
+ await expect ( async ( ) => {
240
+ await act ( ( ) => {
241
+ root . render ( < StatefulComponent /> ) ;
242
+ } ) ;
221
243
} ) . toErrorDev (
222
244
'StatefulComponent: It is not recommended to assign props directly to state ' +
223
245
"because updates to props won't be reflected in state. " +
224
246
'In most cases, it is better to use props directly.' ,
225
247
) ;
226
248
} ) ;
227
249
228
- it ( 'should not allow update state inside of getInitialState' , ( ) => {
250
+ it ( 'should not allow update state inside of getInitialState' , async ( ) => {
229
251
class StatefulComponent extends React . Component {
230
252
constructor ( props , context ) {
231
253
super ( props , context ) ;
@@ -239,20 +261,27 @@ describe('ReactComponentLifeCycle', () => {
239
261
}
240
262
}
241
263
242
- expect ( ( ) => {
243
- ReactTestUtils . renderIntoDocument ( < StatefulComponent /> ) ;
264
+ let container = document . createElement ( 'div' ) ;
265
+ let root = ReactDOMClient . createRoot ( container ) ;
266
+ await expect ( async ( ) => {
267
+ await act ( ( ) => {
268
+ root . render ( < StatefulComponent /> ) ;
269
+ } ) ;
244
270
} ) . toErrorDev (
245
271
"Warning: Can't call setState on a component that is not yet mounted. " +
246
272
'This is a no-op, but it might indicate a bug in your application. ' +
247
273
'Instead, assign to `this.state` directly or define a `state = {};` ' +
248
274
'class property with the desired state in the StatefulComponent component.' ,
249
275
) ;
250
276
251
- // Check deduplication; (no extra warnings should be logged).
252
- ReactTestUtils . renderIntoDocument ( < StatefulComponent /> ) ;
277
+ container = document . createElement ( 'div' ) ;
278
+ root = ReactDOMClient . createRoot ( container ) ;
279
+ await act ( ( ) => {
280
+ root . render ( < StatefulComponent /> ) ;
281
+ } ) ;
253
282
} ) ;
254
283
255
- it ( 'should correctly determine if a component is mounted' , ( ) => {
284
+ it ( 'should correctly determine if a component is mounted' , async ( ) => {
256
285
class Component extends React . Component {
257
286
_isMounted ( ) {
258
287
// No longer a public API, but we can test that it works internally by
@@ -271,15 +300,20 @@ describe('ReactComponentLifeCycle', () => {
271
300
}
272
301
}
273
302
274
- const element = < Component /> ;
303
+ let instance ;
304
+ const element = < Component ref = { current => ( instance = current ) } /> ;
275
305
276
- expect ( ( ) => {
277
- const instance = ReactTestUtils . renderIntoDocument ( element ) ;
278
- expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
306
+ const container = document . createElement ( 'div' ) ;
307
+ const root = ReactDOMClient . createRoot ( container ) ;
308
+ await expect ( async ( ) => {
309
+ await act ( ( ) => {
310
+ root . render ( element ) ;
311
+ } ) ;
279
312
} ) . toErrorDev ( 'Component is accessing isMounted inside its render()' ) ;
313
+ expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
280
314
} ) ;
281
315
282
- it ( 'should correctly determine if a null component is mounted' , ( ) => {
316
+ it ( 'should correctly determine if a null component is mounted' , async ( ) => {
283
317
class Component extends React . Component {
284
318
_isMounted ( ) {
285
319
// No longer a public API, but we can test that it works internally by
@@ -298,12 +332,17 @@ describe('ReactComponentLifeCycle', () => {
298
332
}
299
333
}
300
334
301
- const element = < Component /> ;
335
+ let instance ;
336
+ const element = < Component ref = { current => ( instance = current ) } /> ;
302
337
303
- expect ( ( ) => {
304
- const instance = ReactTestUtils . renderIntoDocument ( element ) ;
305
- expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
338
+ const container = document . createElement ( 'div' ) ;
339
+ const root = ReactDOMClient . createRoot ( container ) ;
340
+ await expect ( async ( ) => {
341
+ await act ( ( ) => {
342
+ root . render ( element ) ;
343
+ } ) ;
306
344
} ) . toErrorDev ( 'Component is accessing isMounted inside its render()' ) ;
345
+ expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
307
346
} ) ;
308
347
309
348
it ( 'isMounted should return false when unmounted' , async ( ) => {
@@ -331,7 +370,7 @@ describe('ReactComponentLifeCycle', () => {
331
370
expect ( instance . updater . isMounted ( instance ) ) . toBe ( false ) ;
332
371
} ) ;
333
372
334
- it ( 'warns if findDOMNode is used inside render' , ( ) => {
373
+ it ( 'warns if legacy findDOMNode is used inside render' , async ( ) => {
335
374
class Component extends React . Component {
336
375
state = { isMounted : false } ;
337
376
componentDidMount ( ) {
@@ -345,8 +384,12 @@ describe('ReactComponentLifeCycle', () => {
345
384
}
346
385
}
347
386
348
- expect ( ( ) => {
349
- ReactTestUtils . renderIntoDocument ( < Component /> ) ;
387
+ const container = document . createElement ( 'div' ) ;
388
+ const root = ReactDOMClient . createRoot ( container ) ;
389
+ await expect ( async ( ) => {
390
+ await act ( ( ) => {
391
+ root . render ( < Component /> ) ;
392
+ } ) ;
350
393
} ) . toErrorDev ( 'Component is accessing findDOMNode inside its render()' ) ;
351
394
} ) ;
352
395
0 commit comments