align shallow renderer with other renderers in defaulting state to null on mount (#11965)

This commit is contained in:
jwbay 2018-01-05 13:44:44 -05:00 committed by Dan Abramov
parent 808f31af5c
commit 39be83565c
2 changed files with 14 additions and 1 deletions

View File

@ -125,7 +125,7 @@ class ReactShallowRenderer {
_mountClassComponent(props, context) {
this._instance.context = context;
this._instance.props = props;
this._instance.state = this._instance.state || emptyObject;
this._instance.state = this._instance.state || null;
this._instance.updater = this._updater;
if (typeof this._instance.componentWillMount === 'function') {

View File

@ -947,4 +947,17 @@ describe('ReactShallowRenderer', () => {
renderAndVerifyWarningAndError([], 'array');
renderAndVerifyWarningAndError({}, 'object');
});
it('should have initial state of null if not defined', () => {
class SomeComponent extends React.Component {
render() {
return <span />;
}
}
const shallowRenderer = createRenderer();
shallowRenderer.render(<SomeComponent />);
expect(shallowRenderer.getMountedInstance().state).toBeNull();
});
});