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

setState returning null and undefined is a no-op on the ShallowRenderer #12756

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ class Updater {
partialState = partialState(currentState, publicInstance.props);
}

// Null and undefined are treated as no-ops.
if (partialState === null || partialState === undefined) {
return;
}

this._renderer._newState = {
...currentState,
...partialState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1305,4 +1305,32 @@ describe('ReactShallowRenderer', () => {
'UNSAFE_componentWillUpdate',
]);
});

it('should stop the upade when setState returns null or undefined', () => {
const log = [];
let instance;
class Component extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
render() {
log.push('render');
instance = this;
return null;
}
}
const shallowRenderer = createRenderer();
shallowRenderer.render(<Component />);
log.length = 0;
instance.setState(() => null);
instance.setState(() => undefined);
instance.setState(null);
instance.setState(undefined);
expect(log).toEqual([]);
instance.setState(state => ({count: state.count + 1}));
expect(log).toEqual(['render']);
});
});