Skip to content

Commit

Permalink
[Fix] mount: state and setState should throw an explicit error …
Browse files Browse the repository at this point in the history
…message when called on an SFC
  • Loading branch information
ljharb committed Aug 16, 2018
1 parent 12b150f commit 8feee5a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/enzyme/src/ReactWrapper.js
Expand Up @@ -302,6 +302,9 @@ class ReactWrapper {
if (this[ROOT] !== this) {
throw new Error('ReactWrapper::setState() can only be called on the root');
}
if (this.instance() === null || this[RENDERER].getNode().nodeType === 'function') {
throw new Error('ReactWrapper::setState() can only be called on class components');
}
if (typeof callback !== 'function') {
throw new TypeError('ReactWrapper::setState() expects a function as its second argument');
}
Expand Down Expand Up @@ -622,6 +625,9 @@ class ReactWrapper {
if (this[ROOT] !== this) {
throw new Error('ReactWrapper::state() can only be called on the root');
}
if (this.instance() === null || this[RENDERER].getNode().nodeType === 'function') {
throw new Error('ReactWrapper::state() can only be called on class components');
}
const _state = this.single('state', () => this.instance().state);
if (typeof name !== 'undefined') {
return _state[name];
Expand Down

0 comments on commit 8feee5a

Please sign in to comment.