@@ -673,10 +673,38 @@ describe('ReactComponentLifeCycle', () => {
673673
674674 const container = document . createElement ( 'div' ) ;
675675 expect ( ( ) => ReactDOM . render ( < Component /> , container ) ) . toWarnDev (
676- 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API .' ,
676+ 'Unsafe legacy lifecycles will not be called for components using new component APIs .' ,
677677 ) ;
678678 } ) ;
679679
680+ it ( 'should not invoke deprecated lifecycles (cWM/cWRP/cWU) if new getSnapshotBeforeUpdate is present' , ( ) => {
681+ class Component extends React . Component {
682+ state = { } ;
683+ getSnapshotBeforeUpdate ( ) {
684+ return null ;
685+ }
686+ componentWillMount ( ) {
687+ throw Error ( 'unexpected' ) ;
688+ }
689+ componentWillReceiveProps ( ) {
690+ throw Error ( 'unexpected' ) ;
691+ }
692+ componentWillUpdate ( ) {
693+ throw Error ( 'unexpected' ) ;
694+ }
695+ componentDidUpdate ( ) { }
696+ render ( ) {
697+ return null ;
698+ }
699+ }
700+
701+ const container = document . createElement ( 'div' ) ;
702+ expect ( ( ) => ReactDOM . render ( < Component value = { 1 } /> , container ) ) . toWarnDev (
703+ 'Unsafe legacy lifecycles will not be called for components using new component APIs.' ,
704+ ) ;
705+ ReactDOM . render ( < Component value = { 2 } /> , container ) ;
706+ } ) ;
707+
680708 it ( 'should not invoke new unsafe lifecycles (cWM/cWRP/cWU) if static gDSFP is present' , ( ) => {
681709 class Component extends React . Component {
682710 state = { } ;
@@ -698,9 +726,10 @@ describe('ReactComponentLifeCycle', () => {
698726 }
699727
700728 const container = document . createElement ( 'div' ) ;
701- expect ( ( ) => ReactDOM . render ( < Component /> , container ) ) . toWarnDev (
702- 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API .' ,
729+ expect ( ( ) => ReactDOM . render ( < Component value = { 1 } /> , container ) ) . toWarnDev (
730+ 'Unsafe legacy lifecycles will not be called for components using new component APIs .' ,
703731 ) ;
732+ ReactDOM . render ( < Component value = { 2 } /> , container ) ;
704733 } ) ;
705734
706735 it ( 'should warn about deprecated lifecycles (cWM/cWRP/cWU) if new static gDSFP is present' , ( ) => {
@@ -720,7 +749,7 @@ describe('ReactComponentLifeCycle', () => {
720749 }
721750
722751 expect ( ( ) => ReactDOM . render ( < AllLegacyLifecycles /> , container ) ) . toWarnDev (
723- 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API .\n\n' +
752+ 'Unsafe legacy lifecycles will not be called for components using new component APIs .\n\n' +
724753 'AllLegacyLifecycles uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
725754 ' componentWillMount\n' +
726755 ' UNSAFE_componentWillReceiveProps\n' +
@@ -741,7 +770,7 @@ describe('ReactComponentLifeCycle', () => {
741770 }
742771
743772 expect ( ( ) => ReactDOM . render ( < WillMount /> , container ) ) . toWarnDev (
744- 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API .\n\n' +
773+ 'Unsafe legacy lifecycles will not be called for components using new component APIs .\n\n' +
745774 'WillMount uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
746775 ' UNSAFE_componentWillMount\n\n' +
747776 'The above lifecycles should be removed. Learn more about this warning here:\n' +
@@ -761,7 +790,7 @@ describe('ReactComponentLifeCycle', () => {
761790 }
762791
763792 expect ( ( ) => ReactDOM . render ( < WillMountAndUpdate /> , container ) ) . toWarnDev (
764- 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API .\n\n' +
793+ 'Unsafe legacy lifecycles will not be called for components using new component APIs .\n\n' +
765794 'WillMountAndUpdate uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
766795 ' componentWillMount\n' +
767796 ' UNSAFE_componentWillUpdate\n\n' +
@@ -781,14 +810,96 @@ describe('ReactComponentLifeCycle', () => {
781810 }
782811
783812 expect ( ( ) => ReactDOM . render ( < WillReceiveProps /> , container ) ) . toWarnDev (
784- 'Unsafe legacy lifecycles will not be called for components using the new getDerivedStateFromProps() API .\n\n' +
813+ 'Unsafe legacy lifecycles will not be called for components using new component APIs .\n\n' +
785814 'WillReceiveProps uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
786815 ' componentWillReceiveProps\n\n' +
787816 'The above lifecycles should be removed. Learn more about this warning here:\n' +
788817 'https://fb.me/react-async-component-lifecycle-hooks' ,
789818 ) ;
790819 } ) ;
791820
821+ it ( 'should warn about deprecated lifecycles (cWM/cWRP/cWU) if new getSnapshotBeforeUpdate is present' , ( ) => {
822+ const container = document . createElement ( 'div' ) ;
823+
824+ class AllLegacyLifecycles extends React . Component {
825+ state = { } ;
826+ getSnapshotBeforeUpdate ( ) { }
827+ componentWillMount ( ) { }
828+ UNSAFE_componentWillReceiveProps ( ) { }
829+ componentWillUpdate ( ) { }
830+ componentDidUpdate ( ) { }
831+ render ( ) {
832+ return null ;
833+ }
834+ }
835+
836+ expect ( ( ) => ReactDOM . render ( < AllLegacyLifecycles /> , container ) ) . toWarnDev (
837+ 'Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n' +
838+ 'AllLegacyLifecycles uses getSnapshotBeforeUpdate() but also contains the following legacy lifecycles:\n' +
839+ ' componentWillMount\n' +
840+ ' UNSAFE_componentWillReceiveProps\n' +
841+ ' componentWillUpdate\n\n' +
842+ 'The above lifecycles should be removed. Learn more about this warning here:\n' +
843+ 'https://fb.me/react-async-component-lifecycle-hooks' ,
844+ ) ;
845+
846+ class WillMount extends React . Component {
847+ state = { } ;
848+ getSnapshotBeforeUpdate ( ) { }
849+ UNSAFE_componentWillMount ( ) { }
850+ componentDidUpdate ( ) { }
851+ render ( ) {
852+ return null ;
853+ }
854+ }
855+
856+ expect ( ( ) => ReactDOM . render ( < WillMount /> , container ) ) . toWarnDev (
857+ 'Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n' +
858+ 'WillMount uses getSnapshotBeforeUpdate() but also contains the following legacy lifecycles:\n' +
859+ ' UNSAFE_componentWillMount\n\n' +
860+ 'The above lifecycles should be removed. Learn more about this warning here:\n' +
861+ 'https://fb.me/react-async-component-lifecycle-hooks' ,
862+ ) ;
863+
864+ class WillMountAndUpdate extends React . Component {
865+ state = { } ;
866+ getSnapshotBeforeUpdate ( ) { }
867+ componentWillMount ( ) { }
868+ UNSAFE_componentWillUpdate ( ) { }
869+ componentDidUpdate ( ) { }
870+ render ( ) {
871+ return null ;
872+ }
873+ }
874+
875+ expect ( ( ) => ReactDOM . render ( < WillMountAndUpdate /> , container ) ) . toWarnDev (
876+ 'Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n' +
877+ 'WillMountAndUpdate uses getSnapshotBeforeUpdate() but also contains the following legacy lifecycles:\n' +
878+ ' componentWillMount\n' +
879+ ' UNSAFE_componentWillUpdate\n\n' +
880+ 'The above lifecycles should be removed. Learn more about this warning here:\n' +
881+ 'https://fb.me/react-async-component-lifecycle-hooks' ,
882+ ) ;
883+
884+ class WillReceiveProps extends React . Component {
885+ state = { } ;
886+ getSnapshotBeforeUpdate ( ) { }
887+ componentWillReceiveProps ( ) { }
888+ componentDidUpdate ( ) { }
889+ render ( ) {
890+ return null ;
891+ }
892+ }
893+
894+ expect ( ( ) => ReactDOM . render ( < WillReceiveProps /> , container ) ) . toWarnDev (
895+ 'Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n' +
896+ 'WillReceiveProps uses getSnapshotBeforeUpdate() but also contains the following legacy lifecycles:\n' +
897+ ' componentWillReceiveProps\n\n' +
898+ 'The above lifecycles should be removed. Learn more about this warning here:\n' +
899+ 'https://fb.me/react-async-component-lifecycle-hooks' ,
900+ ) ;
901+ } ) ;
902+
792903 it ( 'calls effects on module-pattern component' , function ( ) {
793904 const log = [ ] ;
794905
@@ -1037,9 +1148,6 @@ describe('ReactComponentLifeCycle', () => {
10371148
10381149 ReactDOM . render ( < div /> , div ) ;
10391150 expect ( log ) . toEqual ( [ ] ) ;
1040-
1041- // De-duped
1042- ReactDOM . render ( < MyComponent /> , div ) ;
10431151 } ) ;
10441152
10451153 it ( 'should warn if getSnapshotBeforeUpdate returns undefined' , ( ) => {
0 commit comments