From 159e28d4016c9ee535e0c29d7d68c5b46417d529 Mon Sep 17 00:00:00 2001 From: Justin Bay Date: Thu, 4 Jan 2018 20:20:43 -0500 Subject: [PATCH] align shallow renderer with other renderers in defaulting state to null on mount --- .../react-test-renderer/src/ReactShallowRenderer.js | 2 +- .../src/__tests__/ReactShallowRenderer-test.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/react-test-renderer/src/ReactShallowRenderer.js b/packages/react-test-renderer/src/ReactShallowRenderer.js index ef50aafa28d1e..6c55709bca6b0 100644 --- a/packages/react-test-renderer/src/ReactShallowRenderer.js +++ b/packages/react-test-renderer/src/ReactShallowRenderer.js @@ -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') { diff --git a/packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js b/packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js index a2e572e7bd32a..fc76810aa1f27 100644 --- a/packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js +++ b/packages/react-test-renderer/src/__tests__/ReactShallowRenderer-test.js @@ -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 ; + } + } + + const shallowRenderer = createRenderer(); + shallowRenderer.render(); + + expect(shallowRenderer.getMountedInstance().state).toBeNull(); + }); });