Skip to content

Commit

Permalink
[Tests] add mount test case to match shallow case in #1779
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 21, 2018
1 parent 1c70681 commit ffa88dd
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,87 @@ describeWithDOM('mount', () => {
});
});

describe('should not call componentWillReceiveProps after setState is called', () => {
it('should not call componentWillReceiveProps upon rerender', () => {
class A extends React.Component {
constructor(props) {
super(props);

this.state = { a: 0 };
}

componentWillReceiveProps() {
this.setState({ a: 1 });
}

render() {
return (<div>{this.state.a}</div>);
}
}
const spy = sinon.spy(A.prototype, 'componentWillReceiveProps');

const wrapper = mount(<A />, { disableLifecycleMethods: true });

wrapper.setState({ a: 2 });
expect(wrapper.state('a')).to.equal(2);

expect(spy).to.have.property('callCount', 0);
wrapper.setProps({});
expect(spy).to.have.property('callCount', 1);
expect(wrapper.state('a')).to.equal(1);

return new Promise((resolve) => {
wrapper.setState({ a: 3 }, resolve);
}).then(() => {
expect(spy).to.have.property('callCount', 1);
expect(wrapper.state('a')).to.equal(3);
});
});

it('should not call componentWillReceiveProps with multiple keys in props', () => {
class B extends React.Component {
constructor(props) {
super(props);
this.state = { a: 0, b: 1 };
}

componentWillReceiveProps() {
this.setState({ b: 0, a: 1 });
}

render() {
return (
<div>
{this.state.a + this.state.b}
</div>
);
}
}
const spy = sinon.spy(B.prototype, 'componentWillReceiveProps');

const wrapper = mount(<B />, { disableLifecycleMethods: true });

wrapper.setState({ a: 2 });
expect(wrapper.state('a')).to.equal(2);
expect(wrapper.state('b')).to.equal(1);

expect(spy).to.have.property('callCount', 0);
wrapper.setProps({});
expect(spy).to.have.property('callCount', 1);
expect(wrapper.state('a')).to.equal(1);

return Promise.all([
new Promise((resolve) => { wrapper.setState({ b: 5 }, resolve); }),
new Promise((resolve) => { wrapper.setState({ a: 10 }, resolve); }),
]).then(() => {
expect(spy).to.have.property('callCount', 1);
expect(wrapper.state('b')).to.equal(5);
expect(wrapper.state('a')).to.equal(10);
});
});
});


describeIf(is('> 0.13'), 'stateless function components', () => {
it('should throw when trying to access state', () => {
const Foo = () => (
Expand Down

0 comments on commit ffa88dd

Please sign in to comment.