1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @emails react-core
    
  8.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. let React;
    
  13. let ReactDOM;
    
  14. 
    
  15. describe('ReactCompositeComponentNestedState-state', () => {
    
  16.   beforeEach(() => {
    
  17.     React = require('react');
    
  18.     ReactDOM = require('react-dom');
    
  19.   });
    
  20. 
    
  21.   it('should provide up to date values for props', () => {
    
  22.     class ParentComponent extends React.Component {
    
  23.       state = {color: 'blue'};
    
  24. 
    
  25.       handleColor = color => {
    
  26.         this.props.logger('parent-handleColor', this.state.color);
    
  27.         this.setState({color: color}, function () {
    
  28.           this.props.logger('parent-after-setState', this.state.color);
    
  29.         });
    
  30.       };
    
  31. 
    
  32.       render() {
    
  33.         this.props.logger('parent-render', this.state.color);
    
  34.         return (
    
  35.           <ChildComponent
    
  36.             logger={this.props.logger}
    
  37.             color={this.state.color}
    
  38.             onSelectColor={this.handleColor}
    
  39.           />
    
  40.         );
    
  41.       }
    
  42.     }
    
  43. 
    
  44.     class ChildComponent extends React.Component {
    
  45.       constructor(props) {
    
  46.         super(props);
    
  47.         props.logger('getInitialState', props.color);
    
  48.         this.state = {hue: 'dark ' + props.color};
    
  49.       }
    
  50. 
    
  51.       handleHue = (shade, color) => {
    
  52.         this.props.logger('handleHue', this.state.hue, this.props.color);
    
  53.         this.props.onSelectColor(color);
    
  54.         this.setState(
    
  55.           function (state, props) {
    
  56.             this.props.logger(
    
  57.               'setState-this',
    
  58.               this.state.hue,
    
  59.               this.props.color,
    
  60.             );
    
  61.             this.props.logger('setState-args', state.hue, props.color);
    
  62.             return {hue: shade + ' ' + props.color};
    
  63.           },
    
  64.           function () {
    
  65.             this.props.logger(
    
  66.               'after-setState',
    
  67.               this.state.hue,
    
  68.               this.props.color,
    
  69.             );
    
  70.           },
    
  71.         );
    
  72.       };
    
  73. 
    
  74.       render() {
    
  75.         this.props.logger('render', this.state.hue, this.props.color);
    
  76.         return (
    
  77.           <div>
    
  78.             <button onClick={this.handleHue.bind(this, 'dark', 'blue')}>
    
  79.               Dark Blue
    
  80.             </button>
    
  81.             <button onClick={this.handleHue.bind(this, 'light', 'blue')}>
    
  82.               Light Blue
    
  83.             </button>
    
  84.             <button onClick={this.handleHue.bind(this, 'dark', 'green')}>
    
  85.               Dark Green
    
  86.             </button>
    
  87.             <button onClick={this.handleHue.bind(this, 'light', 'green')}>
    
  88.               Light Green
    
  89.             </button>
    
  90.           </div>
    
  91.         );
    
  92.       }
    
  93.     }
    
  94. 
    
  95.     const container = document.createElement('div');
    
  96.     document.body.appendChild(container);
    
  97. 
    
  98.     const logger = jest.fn();
    
  99. 
    
  100.     void ReactDOM.render(<ParentComponent logger={logger} />, container);
    
  101. 
    
  102.     // click "light green"
    
  103.     container.childNodes[0].childNodes[3].click();
    
  104. 
    
  105.     expect(logger.mock.calls).toEqual([
    
  106.       ['parent-render', 'blue'],
    
  107.       ['getInitialState', 'blue'],
    
  108.       ['render', 'dark blue', 'blue'],
    
  109.       ['handleHue', 'dark blue', 'blue'],
    
  110.       ['parent-handleColor', 'blue'],
    
  111.       ['parent-render', 'green'],
    
  112.       ['setState-this', 'dark blue', 'blue'],
    
  113.       ['setState-args', 'dark blue', 'green'],
    
  114.       ['render', 'light green', 'green'],
    
  115.       ['after-setState', 'light green', 'green'],
    
  116.       ['parent-after-setState', 'green'],
    
  117.     ]);
    
  118.   });
    
  119. });