Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shallow rendering, setState after setProps calls componentWillReceiveProps #1779

Merged
merged 1 commit into from Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,86 @@ describe('shallow', () => {
});
});

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 = shallow(<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 = shallow(<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
3 changes: 2 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import flat from 'array.prototype.flat';
import isEqual from 'lodash.isequal';
import cheerio from 'cheerio';

import {
Expand Down Expand Up @@ -374,7 +375,7 @@ class ShallowWrapper {
}
}
// If it doesn't need to rerender, update only its props.
} else if (props) {
} else if (!isEqual(props, instance.props)) {
instance.props = (Object.freeze || Object)({ ...instance.props, ...props });
}
this.update();
Expand Down