| 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 ReactDOMClient; |
| 14 | let act; |
| 15 | |
| 16 | describe('ReactCompositeComponentNestedState-state', () => { |
| 17 | beforeEach(() => { |
| 18 | React = require('react'); |
| 19 | ReactDOMClient = require('react-dom/client'); |
| 20 | act = require('internal-test-utils').act; |
| 21 | }); |
| 22 | |
| 23 | it('should provide up to date values for props', async () => { |
| 24 | class ParentComponent extends React.Component { |
| 25 | state = {color: 'blue'}; |
| 26 | |
| 27 | handleColor = color => { |
| 28 | this.props.logger('parent-handleColor', this.state.color); |
| 29 | this.setState({color: color}, function () { |
| 30 | this.props.logger('parent-after-setState', this.state.color); |
| 31 | }); |
| 32 | }; |
| 33 | |
| 34 | render() { |
| 35 | this.props.logger('parent-render', this.state.color); |
| 36 | return ( |
| 37 | <ChildComponent |
| 38 | logger={this.props.logger} |
| 39 | color={this.state.color} |
| 40 | onSelectColor={this.handleColor} |
| 41 | /> |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | class ChildComponent extends React.Component { |
| 47 | constructor(props) { |
| 48 | super(props); |
| 49 | props.logger('getInitialState', props.color); |
| 50 | this.state = {hue: 'dark ' + props.color}; |
| 51 | } |
| 52 | |
| 53 | handleHue = (shade, color) => { |
| 54 | this.props.logger('handleHue', this.state.hue, this.props.color); |
| 55 | this.props.onSelectColor(color); |
| 56 | this.setState( |
| 57 | function (state, props) { |
| 58 | this.props.logger( |
| 59 | 'setState-this', |
| 60 | this.state.hue, |
| 61 | this.props.color, |
| 62 | ); |
| 63 | this.props.logger('setState-args', state.hue, props.color); |
| 64 | return {hue: shade + ' ' + props.color}; |
| 65 | }, |
| 66 | function () { |
| 67 | this.props.logger( |
| 68 | 'after-setState', |
| 69 | this.state.hue, |
| 70 | this.props.color, |
| 71 | ); |
| 72 | }, |
| 73 | ); |
| 74 | }; |
| 75 | |
| 76 | render() { |
| 77 | this.props.logger('render', this.state.hue, this.props.color); |
| 78 | return ( |
| 79 | <div> |
| 80 | <button onClick={this.handleHue.bind(this, 'dark', 'blue')}> |
| 81 | Dark Blue |
| 82 | </button> |
| 83 | <button onClick={this.handleHue.bind(this, 'light', 'blue')}> |
| 84 | Light Blue |
| 85 | </button> |
| 86 | <button onClick={this.handleHue.bind(this, 'dark', 'green')}> |
| 87 | Dark Green |
| 88 | </button> |
| 89 | <button onClick={this.handleHue.bind(this, 'light', 'green')}> |
| 90 | Light Green |
| 91 | </button> |
| 92 | </div> |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | const container = document.createElement('div'); |
| 98 | document.body.appendChild(container); |
| 99 | |
| 100 | const logger = jest.fn(); |
| 101 | const root = ReactDOMClient.createRoot(container); |
| 102 | |
| 103 | await act(async () => { |
| 104 | root.render(<ParentComponent logger={logger} />); |
| 105 | }); |
| 106 | |
| 107 | await act(async () => { |
| 108 | // click "light green" |
| 109 | container.childNodes[0].childNodes[3].click(); |
| 110 | }); |
| 111 | |
| 112 | expect(logger.mock.calls).toEqual([ |
| 113 | ['parent-render', 'blue'], |
| 114 | ['getInitialState', 'blue'], |
| 115 | ['render', 'dark blue', 'blue'], |
| 116 | ['handleHue', 'dark blue', 'blue'], |
| 117 | ['parent-handleColor', 'blue'], |
| 118 | ['parent-render', 'green'], |
| 119 | ['setState-this', 'dark blue', 'blue'], |
| 120 | ['setState-args', 'dark blue', 'green'], |
| 121 | ['render', 'light green', 'green'], |
| 122 | ['after-setState', 'light green', 'green'], |
| 123 | ['parent-after-setState', 'green'], |
| 124 | ]); |
| 125 | }); |
| 126 | }); |