@@ -673,10 +673,38 @@ describe('ReactComponentLifeCycle', () => {
673
673
674
674
const container = document . createElement ( 'div' ) ;
675
675
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 .' ,
677
677
) ;
678
678
} ) ;
679
679
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
+
680
708
it ( 'should not invoke new unsafe lifecycles (cWM/cWRP/cWU) if static gDSFP is present' , ( ) => {
681
709
class Component extends React . Component {
682
710
state = { } ;
@@ -698,9 +726,10 @@ describe('ReactComponentLifeCycle', () => {
698
726
}
699
727
700
728
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 .' ,
703
731
) ;
732
+ ReactDOM . render ( < Component value = { 2 } /> , container ) ;
704
733
} ) ;
705
734
706
735
it ( 'should warn about deprecated lifecycles (cWM/cWRP/cWU) if new static gDSFP is present' , ( ) => {
@@ -720,7 +749,7 @@ describe('ReactComponentLifeCycle', () => {
720
749
}
721
750
722
751
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' +
724
753
'AllLegacyLifecycles uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
725
754
' componentWillMount\n' +
726
755
' UNSAFE_componentWillReceiveProps\n' +
@@ -741,7 +770,7 @@ describe('ReactComponentLifeCycle', () => {
741
770
}
742
771
743
772
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' +
745
774
'WillMount uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
746
775
' UNSAFE_componentWillMount\n\n' +
747
776
'The above lifecycles should be removed. Learn more about this warning here:\n' +
@@ -761,7 +790,7 @@ describe('ReactComponentLifeCycle', () => {
761
790
}
762
791
763
792
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' +
765
794
'WillMountAndUpdate uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
766
795
' componentWillMount\n' +
767
796
' UNSAFE_componentWillUpdate\n\n' +
@@ -781,14 +810,96 @@ describe('ReactComponentLifeCycle', () => {
781
810
}
782
811
783
812
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' +
785
814
'WillReceiveProps uses getDerivedStateFromProps() but also contains the following legacy lifecycles:\n' +
786
815
' componentWillReceiveProps\n\n' +
787
816
'The above lifecycles should be removed. Learn more about this warning here:\n' +
788
817
'https://fb.me/react-async-component-lifecycle-hooks' ,
789
818
) ;
790
819
} ) ;
791
820
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
+
792
903
it ( 'calls effects on module-pattern component' , function ( ) {
793
904
const log = [ ] ;
794
905
@@ -1037,9 +1148,6 @@ describe('ReactComponentLifeCycle', () => {
1037
1148
1038
1149
ReactDOM . render ( < div /> , div ) ;
1039
1150
expect ( log ) . toEqual ( [ ] ) ;
1040
-
1041
- // De-duped
1042
- ReactDOM . render ( < MyComponent /> , div ) ;
1043
1151
} ) ;
1044
1152
1045
1153
it ( 'should warn if getSnapshotBeforeUpdate returns undefined' , ( ) => {
0 commit comments