diff --git a/src/renderers/testing/ReactShallowRendererEntry.js b/src/renderers/testing/ReactShallowRendererEntry.js index 654cf4297312d..4920da5319125 100644 --- a/src/renderers/testing/ReactShallowRendererEntry.js +++ b/src/renderers/testing/ReactShallowRendererEntry.js @@ -205,12 +205,14 @@ class Updater { } enqueueSetState(publicInstance, partialState, callback, callerName) { + const currentState = this._renderer._newState || publicInstance.state; + if (typeof partialState === 'function') { - partialState = partialState(publicInstance.state, publicInstance.props); + partialState = partialState(currentState, publicInstance.props); } this._renderer._newState = { - ...publicInstance.state, + ...currentState, ...partialState, }; diff --git a/src/renderers/testing/__tests__/ReactShallowRenderer-test.js b/src/renderers/testing/__tests__/ReactShallowRenderer-test.js index 270e8a10d8206..90055e863ee53 100644 --- a/src/renderers/testing/__tests__/ReactShallowRenderer-test.js +++ b/src/renderers/testing/__tests__/ReactShallowRenderer-test.js @@ -450,6 +450,52 @@ describe('ReactShallowRenderer', () => { expect(result).toEqual(
doovy
); }); + it('can setState in componentWillMount repeatedly when shallow rendering', () => { + class SimpleComponent extends React.Component { + state = { + separator: '-', + }; + + componentWillMount() { + this.setState({groovy: 'doovy'}); + this.setState({doovy: 'groovy'}); + } + + render() { + const {groovy, doovy, separator} = this.state; + + return
{`${groovy}${separator}${doovy}`}
; + } + } + + const shallowRenderer = createRenderer(); + const result = shallowRenderer.render(); + expect(result).toEqual(
doovy-groovy
); + }); + + it('can setState in componentWillMount with an updater function repeatedly when shallow rendering', () => { + class SimpleComponent extends React.Component { + state = { + separator: '-', + }; + + componentWillMount() { + this.setState(state => ({groovy: 'doovy'})); + this.setState(state => ({doovy: state.groovy})); + } + + render() { + const {groovy, doovy, separator} = this.state; + + return
{`${groovy}${separator}${doovy}`}
; + } + } + + const shallowRenderer = createRenderer(); + const result = shallowRenderer.render(); + expect(result).toEqual(
doovy-doovy
); + }); + it('can setState in componentWillReceiveProps when shallow rendering', () => { class SimpleComponent extends React.Component { state = {count: 0};