Skip to content

Commit ffa88dd

Browse files
committed
[Tests] add mount test case to match shallow case in #1779
1 parent 1c70681 commit ffa88dd

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,6 +2460,87 @@ describeWithDOM('mount', () => {
24602460
});
24612461
});
24622462

2463+
describe('should not call componentWillReceiveProps after setState is called', () => {
2464+
it('should not call componentWillReceiveProps upon rerender', () => {
2465+
class A extends React.Component {
2466+
constructor(props) {
2467+
super(props);
2468+
2469+
this.state = { a: 0 };
2470+
}
2471+
2472+
componentWillReceiveProps() {
2473+
this.setState({ a: 1 });
2474+
}
2475+
2476+
render() {
2477+
return (<div>{this.state.a}</div>);
2478+
}
2479+
}
2480+
const spy = sinon.spy(A.prototype, 'componentWillReceiveProps');
2481+
2482+
const wrapper = mount(<A />, { disableLifecycleMethods: true });
2483+
2484+
wrapper.setState({ a: 2 });
2485+
expect(wrapper.state('a')).to.equal(2);
2486+
2487+
expect(spy).to.have.property('callCount', 0);
2488+
wrapper.setProps({});
2489+
expect(spy).to.have.property('callCount', 1);
2490+
expect(wrapper.state('a')).to.equal(1);
2491+
2492+
return new Promise((resolve) => {
2493+
wrapper.setState({ a: 3 }, resolve);
2494+
}).then(() => {
2495+
expect(spy).to.have.property('callCount', 1);
2496+
expect(wrapper.state('a')).to.equal(3);
2497+
});
2498+
});
2499+
2500+
it('should not call componentWillReceiveProps with multiple keys in props', () => {
2501+
class B extends React.Component {
2502+
constructor(props) {
2503+
super(props);
2504+
this.state = { a: 0, b: 1 };
2505+
}
2506+
2507+
componentWillReceiveProps() {
2508+
this.setState({ b: 0, a: 1 });
2509+
}
2510+
2511+
render() {
2512+
return (
2513+
<div>
2514+
{this.state.a + this.state.b}
2515+
</div>
2516+
);
2517+
}
2518+
}
2519+
const spy = sinon.spy(B.prototype, 'componentWillReceiveProps');
2520+
2521+
const wrapper = mount(<B />, { disableLifecycleMethods: true });
2522+
2523+
wrapper.setState({ a: 2 });
2524+
expect(wrapper.state('a')).to.equal(2);
2525+
expect(wrapper.state('b')).to.equal(1);
2526+
2527+
expect(spy).to.have.property('callCount', 0);
2528+
wrapper.setProps({});
2529+
expect(spy).to.have.property('callCount', 1);
2530+
expect(wrapper.state('a')).to.equal(1);
2531+
2532+
return Promise.all([
2533+
new Promise((resolve) => { wrapper.setState({ b: 5 }, resolve); }),
2534+
new Promise((resolve) => { wrapper.setState({ a: 10 }, resolve); }),
2535+
]).then(() => {
2536+
expect(spy).to.have.property('callCount', 1);
2537+
expect(wrapper.state('b')).to.equal(5);
2538+
expect(wrapper.state('a')).to.equal(10);
2539+
});
2540+
});
2541+
});
2542+
2543+
24632544
describeIf(is('> 0.13'), 'stateless function components', () => {
24642545
it('should throw when trying to access state', () => {
24652546
const Foo = () => (

0 commit comments

Comments
 (0)