Skip to content

Commit

Permalink
Add nextState, etc. test for shouldComponentUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Jul 6, 2018
1 parent fc3e4af commit 4c5b361
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion test/browser/lifecycle.js
Expand Up @@ -1060,7 +1060,6 @@ describe('Lifecycle methods', () => {
});


// TODO - look at
describe('shouldComponentUpdate', () => {
let setState;

Expand Down Expand Up @@ -1102,6 +1101,75 @@ describe('Lifecycle methods', () => {
expect(ShouldNot.prototype.shouldComponentUpdate).to.have.been.calledOnce;
expect(ShouldNot.prototype.render).to.have.been.calledOnce;
});

it('should be passed next props and state', () => {
let currentPropsLog = [];
let currentStatesLog = [];

let nextPropsLog = [];
let nextStatesLog = [];

class Foo extends Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
}
static getDerivedStateFromProps(props, state) {
return {
value: state.value + 1,
};
}
shouldComponentUpdate(nextProps, nextState) {
nextPropsLog.push(nextProps);
nextStatesLog.push(nextState);

currentPropsLog.push(this.props);
currentStatesLog.push(this.state);

return true;
}
componentDidMount() {
this.setState({
value: this.state.value + 1
});
}
render() {
return <div>{this.state.value}</div>
}
}

let element = render(<Foo foo="foo" />, scratch);
expect(element.textContent).to.be.equal('1');
expect(nextStatesLog).to.have.length(0);
expect(currentStatesLog).to.have.length(0);

element = render(<Foo foo="bar" />, scratch, scratch.firstChild);
expect(element.textContent).to.be.equal('3');

expect(currentPropsLog).to.deep.equal([{
foo: "foo",
children: []
}]);

// this.state in shouldComponentUpdate should be
// the state before getDerivedStateFromProps is called...
expect(currentStatesLog).to.deep.equal([{
value: 1
}]);

// ...and nextState in shouldComponentUpdate should be
// the updated state after getDerivedStateFromProps is called...
expect(nextStatesLog).to.deep.equal([{
value: 3
}]);

expect(nextPropsLog).to.deep.equal([{
foo: "bar",
children: []
}]);
});
});


Expand Down

0 comments on commit 4c5b361

Please sign in to comment.