Shallow renderer: support multiple setState invocation (#11167)

This commit is contained in:
Filipp Riabchun
2017-10-10 12:33:57 +01:00
committed by Dan Abramov
parent 0f43405ca1
commit 0720262bba
2 changed files with 50 additions and 2 deletions
@@ -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,
};
@@ -450,6 +450,52 @@ describe('ReactShallowRenderer', () => {
expect(result).toEqual(<div>doovy</div>);
});
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 <div>{`${groovy}${separator}${doovy}`}</div>;
}
}
const shallowRenderer = createRenderer();
const result = shallowRenderer.render(<SimpleComponent />);
expect(result).toEqual(<div>doovy-groovy</div>);
});
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 <div>{`${groovy}${separator}${doovy}`}</div>;
}
}
const shallowRenderer = createRenderer();
const result = shallowRenderer.render(<SimpleComponent />);
expect(result).toEqual(<div>doovy-doovy</div>);
});
it('can setState in componentWillReceiveProps when shallow rendering', () => {
class SimpleComponent extends React.Component {
state = {count: 0};