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

props not merged when shallow rendering in lifecycleExperimental #1088

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
96 changes: 96 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,50 @@ describe('shallow', () => {
expect(wrapper.first('div').text()).to.equal('yolo');
});

it('should call componentWillReceiveProps, shouldComponentUpdate and componentWillUpdate with merged newProps', () => {
const spy = sinon.spy();

class Foo extends React.Component {
componentWillReceiveProps(nextProps) {
spy('componentWillReceiveProps', this.props, nextProps);
}
shouldComponentUpdate(nextProps) {
spy('shouldComponentUpdate', this.props, nextProps);
return true;
}
componentWillUpdate(nextProps) {
spy('componentWillUpdate', this.props, nextProps);
}
render() {
return (
<div />
);
}
}

const wrapper = shallow(<Foo a="a" b="b" />);

wrapper.setProps({ b: 'c', d: 'e' });

expect(spy.args).to.deep.equal([
[
'componentWillReceiveProps',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'shouldComponentUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'componentWillUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
]);
});

describeIf(!REACT013, 'stateless function components', () => {
it('should set props for a component multiple times', () => {
const Foo = props => (
Expand Down Expand Up @@ -3094,6 +3138,58 @@ describe('shallow', () => {
);
});

it('should componentWillReceiveProps, shouldComponentUpdate, componentWillUpdate and componentDidUpdate with merged props', () => {
const spy = sinon.spy();

class Foo extends React.Component {
componentWillReceiveProps(nextProps) {
spy('componentWillReceiveProps', this.props, nextProps);
}
shouldComponentUpdate(nextProps) {
spy('shouldComponentUpdate', this.props, nextProps);
return true;
}
componentWillUpdate(nextProps) {
spy('componentWillUpdate', this.props, nextProps);
}
componentDidUpdate(prevProps) {
spy('componentDidUpdate', prevProps, this.props);
}
render() {
return (
<div />
);
}
}

const wrapper = shallow(<Foo a="a" b="b" />, { lifecycleExperimental: true });

wrapper.setProps({ b: 'c', d: 'e' });

expect(spy.args).to.deep.equal([
[
'componentWillReceiveProps',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'shouldComponentUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'componentWillUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'componentDidUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
]);
});

it('should cancel rendering when Component returns false in shouldComponentUpdate', () => {
const spy = sinon.spy();

Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class ShallowWrapper {
const state = instance.state;
const prevProps = instance.props || this[UNRENDERED].props;
const prevContext = instance.context || this[OPTIONS].context;
const nextProps = props || prevProps;
const nextProps = { ...prevProps, ...props };
const nextContext = context || prevContext;
if (context) {
this[OPTIONS] = { ...this[OPTIONS], context: nextContext };
Expand Down