Skip to content

Commit

Permalink
[Tests] shallow: add componentDidUpdate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and ljharb committed Jul 5, 2018
1 parent 2345755 commit fac4670
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -3351,8 +3351,8 @@ describe('shallow', () => {
}
}
const result = shallow(<Foo />, { lifecycleExperimental: true });
expect(result.state('count')).to.equal(2);
expect(spy.callCount).to.equal(2);
expect(result.state()).to.have.property('count', 2);
expect(spy).to.have.property('callCount', 2);
});
});

Expand Down Expand Up @@ -3382,19 +3382,17 @@ describe('shallow', () => {
}
render() {
spy('render');
return <div>{this.state.foo}</div>;
const { foo } = this.state;
return <div>{foo}</div>;
}
}
Foo.contextTypes = {
foo: PropTypes.string,
};

const wrapper = shallow(
<Foo foo="bar" />,
{
context: { foo: 'context' },
},
);
const wrapper = shallow(<Foo foo="bar" />, {
context: { foo: 'context' },
});
wrapper.setProps({ foo: 'baz' });
wrapper.setProps({ foo: 'bax' });
expect(spy.args).to.deep.equal([
Expand Down Expand Up @@ -3976,6 +3974,40 @@ describe('shallow', () => {
});
});

context('component instance', () => {
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.setState({ foo: 'wrapper setState update' });
expect(wrapper.state('foo')).to.equal('wrapper setState update');
expect(spy).to.have.property('callCount', 1);

wrapper.instance().onChange();
expect(wrapper.state('foo')).to.equal('onChange update');
expect(spy).to.have.property('callCount', 2);
});
});

describeIf(REACT16, 'support getSnapshotBeforeUpdate', () => {
it('should call getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate', () => {
const spy = sinon.spy();
Expand Down

0 comments on commit fac4670

Please sign in to comment.