Skip to content

Commit

Permalink
fixed problems with not merged props in lifecycleExperimental mode
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsHassler committed Aug 23, 2017
1 parent da91407 commit d9e50bb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
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

0 comments on commit d9e50bb

Please sign in to comment.