Skip to content

Commit

Permalink
Merge pull request #1123 from andrewiggins/add-gDSFP-test
Browse files Browse the repository at this point in the history
Specify that gDSFP is not called if neither props nor state have changed
  • Loading branch information
marvinhagemeister authored May 27, 2018
2 parents 534eb8f + fbeae66 commit fd62f6a
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions test/browser/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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 <Child parentRenders={this.state.parentRenders} ref={child => childRef = child} />;
}
}

class Child extends Component {
render() {
logs.push('child render');
return this.props.parentRenders;
}
}

render(<Parent />, 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)
});
Expand Down

0 comments on commit fd62f6a

Please sign in to comment.