Skip to content

Commit

Permalink
Fix to call componentDidUpdate on setState of React v16
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and ljharb committed Oct 13, 2017
1 parent c66768a commit 4ab4804
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
22 changes: 14 additions & 8 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ describe('shallow', () => {
expect(wrapper.first('div').text()).to.equal('yolo');
});

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

class Foo extends React.Component {
Expand All @@ -1042,6 +1042,9 @@ describe('shallow', () => {
componentWillUpdate(nextProps) {
spy('componentWillUpdate', this.props, nextProps);
}
componentDidUpdate(prevProps) {
spy('componentDidUpdate', prevProps, this.props);
}
render() {
return (
<div />
Expand Down Expand Up @@ -1069,6 +1072,11 @@ describe('shallow', () => {
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
[
'componentDidUpdate',
{ a: 'a', b: 'b' },
{ a: 'a', b: 'c', d: 'e' },
],
]);
});

Expand Down Expand Up @@ -3098,7 +3106,7 @@ describe('shallow', () => {
]);
});

// componentDidUpdate does not seem to get called in react 16 beta.
// componentDidUpdate is not called in react 16
itIf(REACT16, 'calls expected methods for setState', () => {
wrapper.setState({ bar: 'bar' });
expect(spy.args).to.deep.equal([
Expand Down Expand Up @@ -3519,15 +3527,13 @@ describe('shallow', () => {
[
'render',
],
];
if (!REACT16) {
expected.push([
[
'componentDidUpdate',
{ foo: 'props' }, { foo: 'props' },
{ foo: 'bar' }, { foo: 'baz' },
{ foo: 'context' },
]);
}
REACT16 ? undefined : { foo: 'context' },
],
];
expect(spy.args).to.deep.equal(expected);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class ShallowWrapper {

/**
* A method is for re-render with new props and context.
* This calls componentDidUpdate method if lifecycleExperimental is enabled.
* This calls componentDidUpdate method if disableLifecycleMethods is not enabled.
*
* NOTE: can only be called on a wrapper instance that is also the root instance.
*
Expand Down Expand Up @@ -373,7 +373,7 @@ class ShallowWrapper {
// so we replace shouldComponentUpdate to know the result and restore it later.
let originalShouldComponentUpdate;
if (
this[OPTIONS].lifecycleExperimental &&
!this[OPTIONS].disableLifecycleMethods &&
adapter.options.enableComponentDidUpdateOnSetState &&
instance &&
typeof instance.shouldComponentUpdate === 'function'
Expand All @@ -388,7 +388,7 @@ class ShallowWrapper {
instance.setState(state, callback);
if (
shouldRender &&
this[OPTIONS].lifecycleExperimental &&
!this[OPTIONS].disableLifecycleMethods &&
adapter.options.enableComponentDidUpdateOnSetState &&
instance &&
typeof instance.componentDidUpdate === 'function'
Expand Down

0 comments on commit 4ab4804

Please sign in to comment.