Skip to content

Commit

Permalink
[Fix] mount: setState: invoke callback with the proper receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 16, 2018
1 parent 8feee5a commit ec3beef
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,23 @@ class ReactWrapper {
* @param {Function} cb - callback function
* @returns {ReactWrapper}
*/
setState(state, callback = noop) {
setState(state, callback = undefined) {
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') {
if (arguments.length > 1 && typeof callback !== 'function') {
throw new TypeError('ReactWrapper::setState() expects a function as its second argument');
}
this.instance().setState(state, () => {
this.update();
callback();
if (callback) {
const adapter = getAdapter(this[OPTIONS]);
const instance = this.instance();
adapter.invokeSetStateCallback(instance, callback);
}
});
return this;
}
Expand Down

0 comments on commit ec3beef

Please sign in to comment.