@@ -717,4 +717,52 @@ describe('ReactIncrementalUpdates', () => {
717
717
] ) ;
718
718
expect ( root ) . toMatchRenderedOutput ( 'ABCD' ) ;
719
719
} ) ;
720
+
721
+ it ( "base state of update queue is initialized to its fiber's memoized state" , async ( ) => {
722
+ // This test is very weird because it tests an implementation detail but
723
+ // is tested in terms of public APIs. When it was originally written, the
724
+ // test failed because the update queue was initialized to the state of
725
+ // the alternate fiber.
726
+ let app ;
727
+ class App extends React . Component {
728
+ state = { prevProp : 'A' , count : 0 } ;
729
+ static getDerivedStateFromProps ( props , state ) {
730
+ // Add 100 whenever the label prop changes. The prev label is stored
731
+ // in state. If the state is dropped incorrectly, we'll fail to detect
732
+ // prop changes.
733
+ if ( props . prop !== state . prevProp ) {
734
+ return {
735
+ prevProp : props . prop ,
736
+ count : state . count + 100 ,
737
+ } ;
738
+ }
739
+ return null ;
740
+ }
741
+ render ( ) {
742
+ app = this ;
743
+ return this . state . count ;
744
+ }
745
+ }
746
+
747
+ const root = ReactNoop . createRoot ( ) ;
748
+ await ReactNoop . act ( async ( ) => {
749
+ root . render ( < App prop = "A" /> ) ;
750
+ } ) ;
751
+ expect ( root ) . toMatchRenderedOutput ( '0' ) ;
752
+
753
+ // Changing the prop causes the count to increase by 100
754
+ await ReactNoop . act ( async ( ) => {
755
+ root . render ( < App prop = "B" /> ) ;
756
+ } ) ;
757
+ expect ( root ) . toMatchRenderedOutput ( '100' ) ;
758
+
759
+ // Now increment the count by 1 with a state update. And, in the same
760
+ // batch, change the prop back to its original value.
761
+ await ReactNoop . act ( async ( ) => {
762
+ root . render ( < App prop = "A" /> ) ;
763
+ app . setState ( state => ( { count : state . count + 1 } ) ) ;
764
+ } ) ;
765
+ // There were two total prop changes, plus an increment.
766
+ expect ( root ) . toMatchRenderedOutput ( '201' ) ;
767
+ } ) ;
720
768
} ) ;
0 commit comments