Skip to content

Commit

Permalink
separate tests for component.setState and wrapper.setState
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 committed Aug 10, 2018
1 parent 02c6868 commit 5b2d442
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4764,7 +4764,7 @@ describe('shallow', () => {
});

context('component instance', () => {
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
it('should call `componentDidUpdate` when wrapper’s `setState` is called', () => {
const spy = sinon.spy();
class Foo extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -4792,9 +4792,36 @@ describe('shallow', () => {
wrapper.setState({ foo: 'wrapper setState update' });
expect(wrapper.state('foo')).to.equal('wrapper setState update');
expect(spy).to.have.property('callCount', 1);
});

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

componentDidUpdate() {
spy();
}

onChange() {
// enzyme can't handle the update because `this` is a ReactComponent instance,
// not a ShallowWrapper instance.
this.setState({ foo: 'onChange update' });
}

render() {
return <div>{this.state.foo}</div>;
}
}
const wrapper = shallow(<Foo />);
wrapper.instance().onChange();
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 2);
expect(spy).to.have.property('callCount', 1);
});

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

0 comments on commit 5b2d442

Please sign in to comment.