From fbeae66b7abe19b22d4cd93dd8d19b36560ee8d3 Mon Sep 17 00:00:00 2001 From: Andre Wiggins Date: Sat, 26 May 2018 15:22:01 -0700 Subject: [PATCH] Spec gDSFP is not called if neither props nor state have changed --- test/browser/lifecycle.js | 45 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/test/browser/lifecycle.js b/test/browser/lifecycle.js index 3c637e4e62..b7ed745752 100644 --- a/test/browser/lifecycle.js +++ b/test/browser/lifecycle.js @@ -192,8 +192,6 @@ describe('Lifecycle methods', () => { expect(Foo.getDerivedStateFromProps).to.have.been.called; }); - // TODO: Consider if componentWillUpdate should still be called - // Likely, componentWillUpdate should not be called only if getSnapshotBeforeUpdate is implemented it('should NOT invoke deprecated lifecycles (cWM/cWRP) if new static gDSFP is present', () => { class Foo extends Component { static getDerivedStateFromProps() {} @@ -214,6 +212,49 @@ describe('Lifecycle methods', () => { expect(Foo.prototype.componentWillReceiveProps).to.not.have.been.called; }); + it('is not called if neither state nor props have changed', () => { + let logs = []; + let childRef; + + class Parent extends Component { + constructor(props) { + super(props); + this.state = { parentRenders: 0 }; + } + + static getDerivedStateFromProps(props, prevState) { + logs.push('parent getDerivedStateFromProps'); + return prevState.parentRenders + 1; + } + + render() { + logs.push('parent render'); + return childRef = child} />; + } + } + + class Child extends Component { + render() { + logs.push('child render'); + return this.props.parentRenders; + } + } + + render(, scratch); + expect(logs).to.deep.equal([ + 'parent getDerivedStateFromProps', + 'parent render', + 'child render' + ]); + + logs = []; + childRef.setState({}); + rerender(); + expect(logs).to.deep.equal([ + 'child render' + ]); + }); + // TODO: Investigate this test: // [should not override state with stale values if prevState is spread within getDerivedStateFromProps](https://github.com/facebook/react/blob/25dda90c1ecb0c662ab06e2c80c1ee31e0ae9d36/packages/react-dom/src/__tests__/ReactComponentLifeCycle-test.js#L1035) });