1212let act ;
1313
1414let React ;
15- let ReactDOM ;
1615let ReactDOMClient ;
1716let assertConsoleErrorDev ;
1817let assertConsoleWarnDev ;
@@ -63,24 +62,6 @@ const POST_WILL_UNMOUNT_STATE = {
6362 hasWillUnmountCompleted : true ,
6463} ;
6564
66- /**
67- * Every React component is in one of these life cycles.
68- */
69- type ComponentLifeCycle =
70- /**
71- * Mounted components have a DOM node representation and are capable of
72- * receiving new props.
73- */
74- | 'MOUNTED'
75- /**
76- * Unmounted components are inactive and cannot receive new props.
77- */
78- | 'UNMOUNTED' ;
79-
80- function getLifeCycleState ( instance ) : ComponentLifeCycle {
81- return instance . updater . isMounted ( instance ) ? 'MOUNTED' : 'UNMOUNTED' ;
82- }
83-
8465/**
8566 * TODO: We should make any setState calls fail in
8667 * `getInitialState` and `componentWillMount`. They will usually fail
@@ -99,7 +80,6 @@ describe('ReactComponentLifeCycle', () => {
9980 } = require ( 'internal-test-utils' ) ) ;
10081
10182 React = require ( 'react' ) ;
102- ReactDOM = require ( 'react-dom' ) ;
10383 ReactDOMClient = require ( 'react-dom/client' ) ;
10484 } ) ;
10585
@@ -290,137 +270,6 @@ describe('ReactComponentLifeCycle', () => {
290270 } ) ;
291271 } ) ;
292272
293- it ( 'should correctly determine if a component is mounted' , async ( ) => {
294- class Component extends React . Component {
295- _isMounted ( ) {
296- // No longer a public API, but we can test that it works internally by
297- // reaching into the updater.
298- return this . updater . isMounted ( this ) ;
299- }
300- UNSAFE_componentWillMount ( ) {
301- expect ( this . _isMounted ( ) ) . toBeFalsy ( ) ;
302- }
303- componentDidMount ( ) {
304- expect ( this . _isMounted ( ) ) . toBeTruthy ( ) ;
305- }
306- render ( ) {
307- expect ( this . _isMounted ( ) ) . toBeFalsy ( ) ;
308- return < div /> ;
309- }
310- }
311-
312- let instance ;
313- const element = < Component ref = { current => ( instance = current ) } /> ;
314-
315- const container = document . createElement ( 'div' ) ;
316- const root = ReactDOMClient . createRoot ( container ) ;
317- await act ( ( ) => {
318- root . render ( element ) ;
319- } ) ;
320- assertConsoleErrorDev ( [
321- 'Component is accessing isMounted inside its render() function. ' +
322- 'render() should be a pure function of props and state. ' +
323- 'It should never access something that requires stale data ' +
324- 'from the previous render, such as refs. ' +
325- 'Move this logic to componentDidMount and componentDidUpdate instead.\n' +
326- ' in Component (at **)' ,
327- ] ) ;
328- expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
329- } ) ;
330-
331- it ( 'should correctly determine if a null component is mounted' , async ( ) => {
332- class Component extends React . Component {
333- _isMounted ( ) {
334- // No longer a public API, but we can test that it works internally by
335- // reaching into the updater.
336- return this . updater . isMounted ( this ) ;
337- }
338- UNSAFE_componentWillMount ( ) {
339- expect ( this . _isMounted ( ) ) . toBeFalsy ( ) ;
340- }
341- componentDidMount ( ) {
342- expect ( this . _isMounted ( ) ) . toBeTruthy ( ) ;
343- }
344- render ( ) {
345- expect ( this . _isMounted ( ) ) . toBeFalsy ( ) ;
346- return null ;
347- }
348- }
349-
350- let instance ;
351- const element = < Component ref = { current => ( instance = current ) } /> ;
352-
353- const container = document . createElement ( 'div' ) ;
354- const root = ReactDOMClient . createRoot ( container ) ;
355- await act ( ( ) => {
356- root . render ( element ) ;
357- } ) ;
358- assertConsoleErrorDev ( [
359- 'Component is accessing isMounted inside its render() function. ' +
360- 'render() should be a pure function of props and state. ' +
361- 'It should never access something that requires stale data ' +
362- 'from the previous render, such as refs. ' +
363- 'Move this logic to componentDidMount and componentDidUpdate instead.\n' +
364- ' in Component (at **)' ,
365- ] ) ;
366- expect ( instance . _isMounted ( ) ) . toBeTruthy ( ) ;
367- } ) ;
368-
369- it ( 'isMounted should return false when unmounted' , async ( ) => {
370- class Component extends React . Component {
371- render ( ) {
372- return < div /> ;
373- }
374- }
375-
376- const root = ReactDOMClient . createRoot ( document . createElement ( 'div' ) ) ;
377- const instanceRef = React . createRef ( ) ;
378- await act ( ( ) => {
379- root . render ( < Component ref = { instanceRef } /> ) ;
380- } ) ;
381- const instance = instanceRef . current ;
382-
383- // No longer a public API, but we can test that it works internally by
384- // reaching into the updater.
385- expect ( instance . updater . isMounted ( instance ) ) . toBe ( true ) ;
386-
387- await act ( ( ) => {
388- root . unmount ( ) ;
389- } ) ;
390-
391- expect ( instance . updater . isMounted ( instance ) ) . toBe ( false ) ;
392- } ) ;
393-
394- // @gate www && classic
395- it ( 'warns if legacy findDOMNode is used inside render' , async ( ) => {
396- class Component extends React . Component {
397- state = { isMounted : false } ;
398- componentDidMount ( ) {
399- this . setState ( { isMounted : true } ) ;
400- }
401- render ( ) {
402- if ( this . state . isMounted ) {
403- expect ( ReactDOM . findDOMNode ( this ) . tagName ) . toBe ( 'DIV' ) ;
404- }
405- return < div /> ;
406- }
407- }
408-
409- const container = document . createElement ( 'div' ) ;
410- const root = ReactDOMClient . createRoot ( container ) ;
411- await act ( ( ) => {
412- root . render ( < Component /> ) ;
413- } ) ;
414- assertConsoleErrorDev ( [
415- 'Component is accessing findDOMNode inside its render(). ' +
416- 'render() should be a pure function of props and state. ' +
417- 'It should never access something that requires stale data ' +
418- 'from the previous render, such as refs. ' +
419- 'Move this logic to componentDidMount and componentDidUpdate instead.\n' +
420- ' in Component (at **)' ,
421- ] ) ;
422- } ) ;
423-
424273 it ( 'should carry through each of the phases of setup' , async ( ) => {
425274 class LifeCycleComponent extends React . Component {
426275 constructor ( props , context ) {
@@ -433,31 +282,25 @@ describe('ReactComponentLifeCycle', () => {
433282 hasWillUnmountCompleted : false ,
434283 } ;
435284 this . _testJournal . returnedFromGetInitialState = clone ( initState ) ;
436- this . _testJournal . lifeCycleAtStartOfGetInitialState =
437- getLifeCycleState ( this ) ;
438285 this . state = initState ;
439286 }
440287
441288 UNSAFE_componentWillMount ( ) {
442289 this . _testJournal . stateAtStartOfWillMount = clone ( this . state ) ;
443- this . _testJournal . lifeCycleAtStartOfWillMount = getLifeCycleState ( this ) ;
444290 this . state . hasWillMountCompleted = true ;
445291 }
446292
447293 componentDidMount ( ) {
448294 this . _testJournal . stateAtStartOfDidMount = clone ( this . state ) ;
449- this . _testJournal . lifeCycleAtStartOfDidMount = getLifeCycleState ( this ) ;
450295 this . setState ( { hasDidMountCompleted : true } ) ;
451296 }
452297
453298 render ( ) {
454299 const isInitialRender = ! this . state . hasRenderCompleted ;
455300 if ( isInitialRender ) {
456301 this . _testJournal . stateInInitialRender = clone ( this . state ) ;
457- this . _testJournal . lifeCycleInInitialRender = getLifeCycleState ( this ) ;
458302 } else {
459303 this . _testJournal . stateInLaterRender = clone ( this . state ) ;
460- this . _testJournal . lifeCycleInLaterRender = getLifeCycleState ( this ) ;
461304 }
462305 // you would *NEVER* do anything like this in real code!
463306 this . state . hasRenderCompleted = true ;
@@ -466,8 +309,6 @@ describe('ReactComponentLifeCycle', () => {
466309
467310 componentWillUnmount ( ) {
468311 this . _testJournal . stateAtStartOfWillUnmount = clone ( this . state ) ;
469- this . _testJournal . lifeCycleAtStartOfWillUnmount =
470- getLifeCycleState ( this ) ;
471312 this . state . hasWillUnmountCompleted = true ;
472313 }
473314 }
@@ -480,52 +321,33 @@ describe('ReactComponentLifeCycle', () => {
480321 await act ( ( ) => {
481322 root . render ( < LifeCycleComponent ref = { instanceRef } /> ) ;
482323 } ) ;
483- assertConsoleErrorDev ( [
484- 'LifeCycleComponent is accessing isMounted inside its render() function. ' +
485- 'render() should be a pure function of props and state. ' +
486- 'It should never access something that requires stale data ' +
487- 'from the previous render, such as refs. ' +
488- 'Move this logic to componentDidMount and componentDidUpdate instead.\n' +
489- ' in LifeCycleComponent (at **)' ,
490- ] ) ;
491324 const instance = instanceRef . current ;
492325
493326 // getInitialState
494327 expect ( instance . _testJournal . returnedFromGetInitialState ) . toEqual (
495328 GET_INIT_STATE_RETURN_VAL ,
496329 ) ;
497- expect ( instance . _testJournal . lifeCycleAtStartOfGetInitialState ) . toBe (
498- 'UNMOUNTED' ,
499- ) ;
500330
501331 // componentWillMount
502332 expect ( instance . _testJournal . stateAtStartOfWillMount ) . toEqual (
503333 instance . _testJournal . returnedFromGetInitialState ,
504334 ) ;
505- expect ( instance . _testJournal . lifeCycleAtStartOfWillMount ) . toBe ( 'UNMOUNTED' ) ;
506335
507336 // componentDidMount
508337 expect ( instance . _testJournal . stateAtStartOfDidMount ) . toEqual (
509338 DID_MOUNT_STATE ,
510339 ) ;
511- expect ( instance . _testJournal . lifeCycleAtStartOfDidMount ) . toBe ( 'MOUNTED' ) ;
512340
513341 // initial render
514342 expect ( instance . _testJournal . stateInInitialRender ) . toEqual (
515343 INIT_RENDER_STATE ,
516344 ) ;
517- expect ( instance . _testJournal . lifeCycleInInitialRender ) . toBe ( 'UNMOUNTED' ) ;
518-
519- expect ( getLifeCycleState ( instance ) ) . toBe ( 'MOUNTED' ) ;
520345
521346 // Now *update the component*
522347 instance . forceUpdate ( ) ;
523348
524349 // render 2nd time
525350 expect ( instance . _testJournal . stateInLaterRender ) . toEqual ( NEXT_RENDER_STATE ) ;
526- expect ( instance . _testJournal . lifeCycleInLaterRender ) . toBe ( 'MOUNTED' ) ;
527-
528- expect ( getLifeCycleState ( instance ) ) . toBe ( 'MOUNTED' ) ;
529351
530352 await act ( ( ) => {
531353 root . unmount ( ) ;
@@ -535,10 +357,8 @@ describe('ReactComponentLifeCycle', () => {
535357 WILL_UNMOUNT_STATE ,
536358 ) ;
537359 // componentWillUnmount called right before unmount.
538- expect ( instance . _testJournal . lifeCycleAtStartOfWillUnmount ) . toBe ( 'MOUNTED' ) ;
539360
540361 // But the current lifecycle of the component is unmounted.
541- expect ( getLifeCycleState ( instance ) ) . toBe ( 'UNMOUNTED' ) ;
542362 expect ( instance . state ) . toEqual ( POST_WILL_UNMOUNT_STATE ) ;
543363 } ) ;
544364
0 commit comments