@@ -113,17 +113,29 @@ describe('ReactComponentLifeCycle', () => {
113113 }
114114
115115 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 ;
119131 expect ( firstInstance ) . not . toBe ( secondInstance ) ;
120132 } ) ;
121133
122134 /**
123135 * If a state update triggers rerendering that in turn fires an onDOMReady,
124136 * that second onDOMReady should not fail.
125137 */
126- it ( 'it should fire onDOMReady when already in onDOMReady' , ( ) => {
138+ it ( 'it should fire onDOMReady when already in onDOMReady' , async ( ) => {
127139 const _testJournal = [ ] ;
128140
129141 class Child extends React . Component {
@@ -161,7 +173,13 @@ describe('ReactComponentLifeCycle', () => {
161173 }
162174 }
163175
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+
165183 expect ( _testJournal ) . toEqual ( [
166184 'SwitcherParent:getInitialState' ,
167185 'SwitcherParent:onDOMReady' ,
@@ -205,7 +223,7 @@ describe('ReactComponentLifeCycle', () => {
205223 } ) . not . toThrow ( ) ;
206224 } ) ;
207225
208- it ( "warns if setting 'this.state = props'" , ( ) => {
226+ it ( "warns if setting 'this.state = props'" , async ( ) => {
209227 class StatefulComponent extends React . Component {
210228 constructor ( props , context ) {
211229 super ( props , context ) ;
@@ -216,16 +234,20 @@ describe('ReactComponentLifeCycle', () => {
216234 }
217235 }
218236
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+ } ) ;
221243 } ) . toErrorDev (
222244 'StatefulComponent: It is not recommended to assign props directly to state ' +
223245 "because updates to props won't be reflected in state. " +
224246 'In most cases, it is better to use props directly.' ,
225247 ) ;
226248 } ) ;
227249
228- it ( 'should not allow update state inside of getInitialState' , ( ) => {
250+ it ( 'should not allow update state inside of getInitialState' , async ( ) => {
229251 class StatefulComponent extends React . Component {
230252 constructor ( props , context ) {
231253 super ( props , context ) ;
@@ -239,20 +261,27 @@ describe('ReactComponentLifeCycle', () => {
239261 }
240262 }
241263
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+ } ) ;
244270 } ) . toErrorDev (
245271 "Warning: Can't call setState on a component that is not yet mounted. " +
246272 'This is a no-op, but it might indicate a bug in your application. ' +
247273 'Instead, assign to `this.state` directly or define a `state = {};` ' +
248274 'class property with the desired state in the StatefulComponent component.' ,
249275 ) ;
250276
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+ } ) ;
253282 } ) ;
254283
255- it ( 'should correctly determine if a component is mounted' , ( ) => {
284+ it ( 'should correctly determine if a component is mounted' , async ( ) => {
256285 class Component extends React . Component {
257286 _isMounted ( ) {
258287 // No longer a public API, but we can test that it works internally by
@@ -271,15 +300,20 @@ describe('ReactComponentLifeCycle', () => {
271300 }
272301 }
273302
274- const element = < Component /> ;
303+ let instance ;
304+ const element = < Component ref = { current => ( instance = current ) } /> ;
275305
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+ } ) ;
279312 } ) . toErrorDev ( 'Component is accessing isMounted inside its render()' ) ;
313+ expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
280314 } ) ;
281315
282- it ( 'should correctly determine if a null component is mounted' , ( ) => {
316+ it ( 'should correctly determine if a null component is mounted' , async ( ) => {
283317 class Component extends React . Component {
284318 _isMounted ( ) {
285319 // No longer a public API, but we can test that it works internally by
@@ -298,12 +332,17 @@ describe('ReactComponentLifeCycle', () => {
298332 }
299333 }
300334
301- const element = < Component /> ;
335+ let instance ;
336+ const element = < Component ref = { current => ( instance = current ) } /> ;
302337
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+ } ) ;
306344 } ) . toErrorDev ( 'Component is accessing isMounted inside its render()' ) ;
345+ expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
307346 } ) ;
308347
309348 it ( 'isMounted should return false when unmounted' , async ( ) => {
@@ -331,7 +370,7 @@ describe('ReactComponentLifeCycle', () => {
331370 expect ( instance . updater . isMounted ( instance ) ) . toBe ( false ) ;
332371 } ) ;
333372
334- it ( 'warns if findDOMNode is used inside render' , ( ) => {
373+ it ( 'warns if legacy findDOMNode is used inside render' , async ( ) => {
335374 class Component extends React . Component {
336375 state = { isMounted : false } ;
337376 componentDidMount ( ) {
@@ -345,8 +384,12 @@ describe('ReactComponentLifeCycle', () => {
345384 }
346385 }
347386
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+ } ) ;
350393 } ) . toErrorDev ( 'Component is accessing findDOMNode inside its render()' ) ;
351394 } ) ;
352395
0 commit comments