Skip to content

Commit

Permalink
[Fix] shallow/mount: restore fallback when adapter lacks `invokeS…
Browse files Browse the repository at this point in the history
…etStateCallback`
  • Loading branch information
ljharb committed Aug 16, 2018
1 parent ec3beef commit 093b2ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ class ReactWrapper {
if (callback) {
const adapter = getAdapter(this[OPTIONS]);
const instance = this.instance();
adapter.invokeSetStateCallback(instance, callback);
if (adapter.invokeSetStateCallback) {
adapter.invokeSetStateCallback(instance, callback);
} else {
callback.call(instance);
}
}
});
return this;
Expand Down
9 changes: 8 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,9 @@ class ShallowWrapper {
if (this.instance() === null || this[RENDERER].getNode().nodeType === 'function') {
throw new Error('ShallowWrapper::setState() can only be called on class components');
}
if (arguments.length > 1 && typeof callback !== 'function') {
throw new TypeError('ReactWrapper::setState() expects a function as its second argument');
}
this.single('setState', () => {
withSetStateAllowed(() => {
const adapter = getAdapter(this[OPTIONS]);
Expand Down Expand Up @@ -481,7 +484,11 @@ class ShallowWrapper {
this.update();
// call the setState callback
if (callback) {
adapter.invokeSetStateCallback(instance, callback);
if (adapter.invokeSetStateCallback) {
adapter.invokeSetStateCallback(instance, callback);
} else {
callback.call(instance);
}
}
});
});
Expand Down

0 comments on commit 093b2ed

Please sign in to comment.