Skip to content

Commit

Permalink
shallow render: fix setState in componentWillMount
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Feeney committed Dec 2, 2015
1 parent 34fbcf2 commit 9c21e2f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,17 @@ ReactShallowRenderer.prototype.render = function(element, context) {
if (!context) {
context = emptyObject;
}
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
this._render(element, transaction, context);
ReactUpdates.ReactReconcileTransaction.release(transaction);
ReactUpdates.batchedUpdates(_batchedRender, this, element, context);

return this.getRenderOutput();
};

function _batchedRender(renderer, element, context) {
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
renderer._render(element, transaction, context);
ReactUpdates.ReactReconcileTransaction.release(transaction);
}

ReactShallowRenderer.prototype.getRenderOutput = function() {
return (
(this._instance && this._instance._renderedComponent &&
Expand Down
15 changes: 15 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ describe('ReactTestUtils', function() {
expect(result.props.className).toEqual('clicked');
});

it('can setState in componentWillMount when shallow rendering', function() {
var SimpleComponent = React.createClass({
componentWillMount() {
this.setState({groovy: 'doovy'});
},
render() {
return <div>{this.state.groovy}</div>;
},
});

var shallowRenderer = ReactTestUtils.createRenderer();
var result = shallowRenderer.render(<SimpleComponent />);
expect(result).toEqual(<div>doovy</div>);
});

it('can pass context when shallowly rendering', function() {
var SimpleComponent = React.createClass({
contextTypes: {
Expand Down

0 comments on commit 9c21e2f

Please sign in to comment.