Skip to content

Commit 5b2d442

Browse files
committed
separate tests for component.setState and wrapper.setState
1 parent 02c6868 commit 5b2d442

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,7 +4764,7 @@ describe('shallow', () => {
47644764
});
47654765

47664766
context('component instance', () => {
4767-
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
4767+
it('should call `componentDidUpdate` when wrapper’s `setState` is called', () => {
47684768
const spy = sinon.spy();
47694769
class Foo extends React.Component {
47704770
constructor(props) {
@@ -4792,9 +4792,36 @@ describe('shallow', () => {
47924792
wrapper.setState({ foo: 'wrapper setState update' });
47934793
expect(wrapper.state('foo')).to.equal('wrapper setState update');
47944794
expect(spy).to.have.property('callCount', 1);
4795+
});
4796+
4797+
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
4798+
const spy = sinon.spy();
4799+
class Foo extends React.Component {
4800+
constructor(props) {
4801+
super(props);
4802+
this.state = {
4803+
foo: 'init',
4804+
};
4805+
}
4806+
4807+
componentDidUpdate() {
4808+
spy();
4809+
}
4810+
4811+
onChange() {
4812+
// enzyme can't handle the update because `this` is a ReactComponent instance,
4813+
// not a ShallowWrapper instance.
4814+
this.setState({ foo: 'onChange update' });
4815+
}
4816+
4817+
render() {
4818+
return <div>{this.state.foo}</div>;
4819+
}
4820+
}
4821+
const wrapper = shallow(<Foo />);
47954822
wrapper.instance().onChange();
47964823
expect(wrapper.state('foo')).to.equal('onChange update');
4797-
expect(spy).to.have.property('callCount', 2);
4824+
expect(spy).to.have.property('callCount', 1);
47984825
});
47994826

48004827
it('should call `componentDidUpdate` when component’s `setState` is called through a bound method', () => {

0 commit comments

Comments
 (0)