Skip to content

Commit

Permalink
[Tests] setProps: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 21, 2020
1 parent 497e436 commit acf5f4d
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions packages/enzyme-test-suite/test/shared/methods/setProps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,12 @@ export default function describeSetProps({
spy('componentWillReceiveProps', this.props, nextProps);
}

componentDidUpdate(prevProps) {
spy('componentDidUpdate', prevProps, this.props);
componentDidUpdate(prevProps, prevState) {
spy('componentDidUpdate', prevProps, this.props, prevState, this.state);
}

render() {
spy('render', this.props, this.state);
const { count } = this.state;
return (<div>{count}</div>);
}
Expand All @@ -622,48 +623,43 @@ export default function describeSetProps({
this.setState(({ count }) => ({ count: count + 1 }));
}

componentDidUpdate(prevProps) {
spy('componentDidUpdate', prevProps, this.props);
componentDidUpdate(prevProps, prevState) {
spy('componentDidUpdate', prevProps, this.props, prevState, this.state);
}

render() {
spy('render', this.props, this.state);
const { count } = this.state;
return (<div>{count}</div>);
}
}

const oldProps = { a: 'old a', b: 'old b' };
const newProps = { a: 'old a', b: 'new b', d: 'new d' };
const oldState = { count: 0 };
const newState = { count: 1 };

it('calls componentDidUpdate when componentWillReceiveProps without setting state', () => {
const wrapper = Wrap(<WithoutSetStateInCWRP a="old a" b="old b" />);
wrapper.setProps({ b: 'new b', d: 'new d' });
expect(spy.args).to.deep.equal([
[
'componentWillReceiveProps',
{ a: 'old a', b: 'old b' },
{ a: 'old a', b: 'new b', d: 'new d' },
],
[
'componentDidUpdate',
{ a: 'old a', b: 'old b' },
{ a: 'old a', b: 'new b', d: 'new d' },
],
['render', oldProps, oldState],
['componentWillReceiveProps', oldProps, newProps],
['render', newProps, oldState],
['componentDidUpdate', oldProps, newProps, oldState, oldState],
]);
});

// TODO: assertion output is incorrect after calling setState inside of componentWillReceiveProps
itIf(!isShallow, 'calls componentDidUpdate when componentWillReceiveProps sets state', () => {
const wrapper = Wrap(<WithSetStateInCWRP a="old a" b="old b" />);
wrapper.setProps({ b: 'new b', d: 'new d' });

expect(spy.args).to.deep.equal([
[
'componentWillReceiveProps',
{ a: 'old a', b: 'old b' },
{ a: 'old a', b: 'new b', d: 'new d' },
],
[
'componentDidUpdate',
{ a: 'old a', b: 'old b' },
{ a: 'old a', b: 'new b', d: 'new d' },
],
['render', oldProps, oldState],
['componentWillReceiveProps', oldProps, newProps],
['render', newProps, newState],
['componentDidUpdate', oldProps, newProps, oldState, newState],
]);
});
});
Expand Down

0 comments on commit acf5f4d

Please sign in to comment.