Skip to content

Commit fac4670

Browse files
koba04ljharb
authored andcommitted
[Tests] shallow: add componentDidUpdate tests
Per #1452 (comment)
1 parent 2345755 commit fac4670

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,8 +3351,8 @@ describe('shallow', () => {
33513351
}
33523352
}
33533353
const result = shallow(<Foo />, { lifecycleExperimental: true });
3354-
expect(result.state('count')).to.equal(2);
3355-
expect(spy.callCount).to.equal(2);
3354+
expect(result.state()).to.have.property('count', 2);
3355+
expect(spy).to.have.property('callCount', 2);
33563356
});
33573357
});
33583358

@@ -3382,19 +3382,17 @@ describe('shallow', () => {
33823382
}
33833383
render() {
33843384
spy('render');
3385-
return <div>{this.state.foo}</div>;
3385+
const { foo } = this.state;
3386+
return <div>{foo}</div>;
33863387
}
33873388
}
33883389
Foo.contextTypes = {
33893390
foo: PropTypes.string,
33903391
};
33913392

3392-
const wrapper = shallow(
3393-
<Foo foo="bar" />,
3394-
{
3395-
context: { foo: 'context' },
3396-
},
3397-
);
3393+
const wrapper = shallow(<Foo foo="bar" />, {
3394+
context: { foo: 'context' },
3395+
});
33983396
wrapper.setProps({ foo: 'baz' });
33993397
wrapper.setProps({ foo: 'bax' });
34003398
expect(spy.args).to.deep.equal([
@@ -3976,6 +3974,40 @@ describe('shallow', () => {
39763974
});
39773975
});
39783976

3977+
context('component instance', () => {
3978+
it('should call `componentDidUpdate` when component’s `setState` is called', () => {
3979+
const spy = sinon.spy();
3980+
class Foo extends React.Component {
3981+
constructor(props) {
3982+
super(props);
3983+
this.state = {
3984+
foo: 'init',
3985+
};
3986+
}
3987+
componentDidUpdate() {
3988+
spy();
3989+
}
3990+
onChange() {
3991+
// enzyme can't handle the update because `this` is a ReactComponent instance,
3992+
// not a ShallowWrapper instance.
3993+
this.setState({ foo: 'onChange update' });
3994+
}
3995+
render() {
3996+
return <div>{this.state.foo}</div>;
3997+
}
3998+
}
3999+
const wrapper = shallow(<Foo />);
4000+
4001+
wrapper.setState({ foo: 'wrapper setState update' });
4002+
expect(wrapper.state('foo')).to.equal('wrapper setState update');
4003+
expect(spy).to.have.property('callCount', 1);
4004+
4005+
wrapper.instance().onChange();
4006+
expect(wrapper.state('foo')).to.equal('onChange update');
4007+
expect(spy).to.have.property('callCount', 2);
4008+
});
4009+
});
4010+
39794011
describeIf(REACT16, 'support getSnapshotBeforeUpdate', () => {
39804012
it('should call getSnapshotBeforeUpdate and pass snapshot to componentDidUpdate', () => {
39814013
const spy = sinon.spy();

0 commit comments

Comments
 (0)