Skip to content

Commit

Permalink
Fix to call a setState callback after finishing the the render
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and ljharb committed Dec 26, 2017
1 parent c8f828a commit 04076f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1378,8 +1378,10 @@ describe('shallow', () => {
}
const wrapper = shallow(<Foo />);
expect(wrapper.state()).to.eql({ id: 'foo' });
wrapper.setState({ id: 'bar' }, () => {
wrapper.setState({ id: 'bar' }, function callback() {
expect(wrapper.state()).to.eql({ id: 'bar' });
expect(this.state).to.eql({ id: 'bar' });
expect(wrapper.find('div').prop('className')).to.eql('bar');
});
});

Expand Down
8 changes: 7 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ class ShallowWrapper {
return shouldRender;
};
}
instance.setState(state, callback);
// We don't pass the setState callback here
// to guarantee to call the callback after finishing the render
instance.setState(state);
if (
shouldRender &&
!this[OPTIONS].disableLifecycleMethods &&
Expand All @@ -400,6 +402,10 @@ class ShallowWrapper {
}
}
this.update();
// call the setState callback
if (callback) {
callback.call(instance);
}
});
});
return this;
Expand Down

0 comments on commit 04076f6

Please sign in to comment.