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

This commit is contained in:
Toru Kobayashi
2018-05-08 09:31:33 +09:00
committed by Brandon Dail
parent 25dda90c1e
commit 0bf24cc83e
2 changed files with 33 additions and 0 deletions

View File

@@ -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,

View File

@@ -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']);
});
});