Skip to content

Commit

Permalink
Add a failed test that setState in cDM
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and ljharb committed Aug 17, 2018
1 parent e590784 commit b412ec8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5169,5 +5169,32 @@ describeWithDOM('mount', () => {
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 1);
});

it('should call `componentDidUpdate` when component’s `setState` is called', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
this.update = () => this.setState({ foo: 'update' });
}

componentDidMount() {
this.update();
}

componentDidUpdate() {}

render() {
return <div>{this.state.foo}</div>;
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');

const wrapper = mount(<Foo />);
expect(spy).to.have.property('callCount', 1);
expect(wrapper.state('foo')).to.equal('update');
});
});
});
27 changes: 27 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4967,6 +4967,33 @@ describe('shallow', () => {
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 1);
});

it('should call `componentDidUpdate` when component’s `setState` is called', () => {
class Foo extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'init',
};
this.update = () => this.setState({ foo: 'update' });
}

componentDidMount() {
this.update();
}

componentDidUpdate() {}

render() {
return <div>{this.state.foo}</div>;
}
}
const spy = sinon.spy(Foo.prototype, 'componentDidUpdate');

const wrapper = shallow(<Foo />);
expect(spy).to.have.property('callCount', 1);
expect(wrapper.state('foo')).to.equal('update');
});
});

describeIf(is('>= 16'), 'support getSnapshotBeforeUpdate', () => {
Expand Down

0 comments on commit b412ec8

Please sign in to comment.