Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shallow renderer: support multiple setState invocation #11167

Merged
merged 1 commit into from Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/renderers/testing/ReactShallowRendererEntry.js
Expand Up @@ -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,
};

Expand Down
46 changes: 46 additions & 0 deletions src/renderers/testing/__tests__/ReactShallowRenderer-test.js
Expand Up @@ -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};
Expand Down