Skip to content

Commit

Permalink
[Fix] shallow/mount: throw an explicit error when state is null/u…
Browse files Browse the repository at this point in the history
…ndefined
  • Loading branch information
ljharb committed Oct 2, 2018
1 parent e18bb65 commit 9ea33d7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describeWithDOM('mount', () => {
</div>
</Foo>
`.trim());
expect(() => wrapper.state('key')).to.throw('ReactWrapper::state("key") requires that `state` not be `null` or `undefined`');
});

describeIf(is('>= 16.3'), 'uses the isValidElementType from the Adapter to validate the prop type of Component', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ describe('shallow', () => {
null
</div>
`.trim());
expect(() => wrapper.state('key')).to.throw('ShallowWrapper::state("key") requires that `state` not be `null` or `undefined`');
});
});

Expand Down
3 changes: 3 additions & 0 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ class ReactWrapper {
}
const _state = this.single('state', () => this.instance().state);
if (typeof name !== 'undefined') {
if (_state == null) {
throw new TypeError(`ReactWrapper::state("${name}") requires that \`state\` not be \`null\` or \`undefined\``);
}
return _state[name];
}
return _state;
Expand Down
3 changes: 3 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,9 @@ class ShallowWrapper {
}
const _state = this.single('state', () => this.instance().state);
if (typeof name !== 'undefined') {
if (_state == null) {
throw new TypeError(`ShallowWrapper::state("${name}") requires that \`state\` not be \`null\` or \`undefined\``);
}
return _state[name];
}
return _state;
Expand Down

0 comments on commit 9ea33d7

Please sign in to comment.