@samitouri / QOS-React-2 / commits / e17086e5df

Add componentWillReceiveProps setState test

Ben Alpert committed May 19, 2014 at 15:58 UTC e17086e5df626d915be122f9c319cee2e184e9eb
1 file changed +25
src/core/__tests__/ReactUpdates-test.js
+25
@@ -723,4 +723,29 @@ describe('ReactUpdates', function() {
723 expect(a.state.x).toBe(1);
724 expect(a.getDOMNode().textContent).toBe('A1');
725 });
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 + });
751 });