mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Merge pull request #5561 from graue/setstate-cwm
shallow render: fix setState in componentWillMount
(cherry picked from commit 8557cc0d67)
This commit is contained in:
@@ -423,11 +423,15 @@ ReactShallowRenderer.prototype.render = function(element, context) {
|
||||
if (!context) {
|
||||
context = emptyObject;
|
||||
}
|
||||
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(false);
|
||||
this._render(element, transaction, context);
|
||||
ReactUpdates.ReactReconcileTransaction.release(transaction);
|
||||
ReactUpdates.batchedUpdates(_batchedRender, this, element, context);
|
||||
};
|
||||
|
||||
function _batchedRender(renderer, element, context) {
|
||||
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(false);
|
||||
renderer._render(element, transaction, context);
|
||||
ReactUpdates.ReactReconcileTransaction.release(transaction);
|
||||
}
|
||||
|
||||
ReactShallowRenderer.prototype.unmount = function() {
|
||||
if (this._instance) {
|
||||
this._instance.unmountComponent();
|
||||
|
||||
@@ -201,6 +201,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: {
|
||||
|
||||
Reference in New Issue
Block a user