From d8b83cda66321fa4a59e3779f2939a02ea798c95 Mon Sep 17 00:00:00 2001 From: Toru Kobayashi Date: Fri, 3 Nov 2017 22:09:34 +0900 Subject: [PATCH] Fix to call componentDidUpdate on setState of React v16 --- .../test/ShallowWrapper-spec.jsx | 22 ++++++++++++------- packages/enzyme/src/ShallowWrapper.js | 6 ++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 3f15cb720..3939cbbf3 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -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 { @@ -1042,6 +1042,9 @@ describe('shallow', () => { componentWillUpdate(nextProps) { spy('componentWillUpdate', this.props, nextProps); } + componentDidUpdate(prevProps) { + spy('componentDidUpdate', prevProps, this.props); + } render() { return (
@@ -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' }, + ], ]); }); @@ -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([ @@ -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); }); diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 7e0604a43..a98db3006 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -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. * @@ -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' @@ -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'