Skip to content

Commit e17086e

Browse files
committed
Add componentWillReceiveProps setState test
1 parent 808a54f commit e17086e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/core/__tests__/ReactUpdates-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,29 @@ describe('ReactUpdates', function() {
723723
expect(a.state.x).toBe(1);
724724
expect(a.getDOMNode().textContent).toBe('A1');
725725
});
726+
727+
it('calls componentWillReceiveProps setState callback properly', function() {
728+
var callbackCount = 0;
729+
var A = React.createClass({
730+
getInitialState: function() {
731+
return {x: this.props.x};
732+
},
733+
componentWillReceiveProps: function(nextProps) {
734+
var newX = nextProps.x;
735+
this.setState({x: newX}, function() {
736+
// State should have updated by the time this callback gets called
737+
expect(this.state.x).toBe(newX);
738+
callbackCount++;
739+
});
740+
},
741+
render: function() {
742+
return <div>{this.state.x}</div>;
743+
}
744+
});
745+
746+
var container = document.createElement('div');
747+
React.renderComponent(<A x={1} />, container);
748+
React.renderComponent(<A x={2} />, container);
749+
expect(callbackCount).toBe(1);
750+
});
726751
});

0 commit comments

Comments
 (0)