|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 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 | +var React; |
| 13 | +var ReactFeatureFlags; |
| 14 | +var ReactTestRenderer; |
| 15 | + |
| 16 | +describe('ReactAsyncClassComponent', () => { |
| 17 | + describe('debugRenderPhaseSideEffects', () => { |
| 18 | + beforeEach(() => { |
| 19 | + jest.resetModules(); |
| 20 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 21 | + ReactFeatureFlags.debugRenderPhaseSideEffects = true; |
| 22 | + React = require('react'); |
| 23 | + ReactTestRenderer = require('react-test-renderer'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should invoke precommit lifecycle methods twice', () => { |
| 27 | + let log = []; |
| 28 | + let shouldComponentUpdate = false; |
| 29 | + class ClassComponent extends React.Component { |
| 30 | + state = {}; |
| 31 | + componentDidMount() { |
| 32 | + log.push('componentDidMount'); |
| 33 | + } |
| 34 | + componentDidUpdate() { |
| 35 | + log.push('componentDidUpdate'); |
| 36 | + } |
| 37 | + componentWillMount() { |
| 38 | + log.push('componentWillMount'); |
| 39 | + } |
| 40 | + componentWillReceiveProps() { |
| 41 | + log.push('componentWillReceiveProps'); |
| 42 | + } |
| 43 | + componentWillUnmount() { |
| 44 | + log.push('componentWillUnmount'); |
| 45 | + } |
| 46 | + componentWillUpdate() { |
| 47 | + log.push('componentWillUpdate'); |
| 48 | + } |
| 49 | + shouldComponentUpdate() { |
| 50 | + log.push('shouldComponentUpdate'); |
| 51 | + return shouldComponentUpdate; |
| 52 | + } |
| 53 | + render() { |
| 54 | + log.push('render'); |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const component = ReactTestRenderer.create(<ClassComponent />); |
| 60 | + expect(log).toEqual([ |
| 61 | + 'componentWillMount', |
| 62 | + 'componentWillMount', |
| 63 | + 'render', |
| 64 | + 'render', |
| 65 | + 'componentDidMount', |
| 66 | + ]); |
| 67 | + |
| 68 | + log = []; |
| 69 | + shouldComponentUpdate = true; |
| 70 | + |
| 71 | + component.update(<ClassComponent />); |
| 72 | + expect(log).toEqual([ |
| 73 | + 'componentWillReceiveProps', |
| 74 | + 'componentWillReceiveProps', |
| 75 | + 'shouldComponentUpdate', |
| 76 | + 'shouldComponentUpdate', |
| 77 | + 'componentWillUpdate', |
| 78 | + 'componentWillUpdate', |
| 79 | + 'render', |
| 80 | + 'render', |
| 81 | + 'componentDidUpdate', |
| 82 | + ]); |
| 83 | + |
| 84 | + log = []; |
| 85 | + shouldComponentUpdate = false; |
| 86 | + |
| 87 | + component.update(<ClassComponent />); |
| 88 | + expect(log).toEqual([ |
| 89 | + 'componentWillReceiveProps', |
| 90 | + 'componentWillReceiveProps', |
| 91 | + 'shouldComponentUpdate', |
| 92 | + 'shouldComponentUpdate', |
| 93 | + ]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should invoke setState callbacks twice', () => { |
| 97 | + class ClassComponent extends React.Component { |
| 98 | + state = { |
| 99 | + count: 1, |
| 100 | + }; |
| 101 | + render() { |
| 102 | + return null; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + let setStateCount = 0; |
| 107 | + |
| 108 | + const rendered = ReactTestRenderer.create(<ClassComponent />); |
| 109 | + const instance = rendered.getInstance(); |
| 110 | + instance.setState(state => { |
| 111 | + setStateCount++; |
| 112 | + return { |
| 113 | + count: state.count + 1, |
| 114 | + }; |
| 115 | + }); |
| 116 | + |
| 117 | + // Callback should be invoked twice |
| 118 | + expect(setStateCount).toBe(2); |
| 119 | + // But each time `state` should be the previous value |
| 120 | + expect(instance.state.count).toBe(2); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments