shallow render: fix setState in componentWillMount
Scott Feeney committed
Nov 28, 2015 at 16:42 UTC
9c21e2f3c447776bb38a71b12b60a1c3218edf97
2 files changed
+22
-3
src/test/ReactTestUtils.js
+7
-3
@@ -427,13 +427,17 @@ ReactShallowRenderer.prototype.render = function(element, context) {
427
if (!context) {
428
context = emptyObject;
429
}
430
- var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
431
- this._render(element, transaction, context);
432
- ReactUpdates.ReactReconcileTransaction.release(transaction);
430
+ ReactUpdates.batchedUpdates(_batchedRender, this, element, context);
431
432
return this.getRenderOutput();
433
};
434
435
+function _batchedRender(renderer, element, context) {
436
+ var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
437
+ renderer._render(element, transaction, context);
438
+ ReactUpdates.ReactReconcileTransaction.release(transaction);
439
+}
440
+
441
ReactShallowRenderer.prototype.getRenderOutput = function() {
442
return (
443
(this._instance && this._instance._renderedComponent &&
src/test/__tests__/ReactTestUtils-test.js
+15
@@ -217,6 +217,21 @@ describe('ReactTestUtils', function() {
217
expect(result.props.className).toEqual('clicked');
218
});
219
220
+ it('can setState in componentWillMount when shallow rendering', function() {
221
+ var SimpleComponent = React.createClass({
222
+ componentWillMount() {
223
+ this.setState({groovy: 'doovy'});
224
+ },
225
+ render() {
226
+ return <div>{this.state.groovy}</div>;
227
+ },
228
+ });
229
+
230
+ var shallowRenderer = ReactTestUtils.createRenderer();
231
+ var result = shallowRenderer.render(<SimpleComponent />);
232
+ expect(result).toEqual(<div>doovy</div>);
233
+ });
234
+
235
it('can pass context when shallowly rendering', function() {
236
var SimpleComponent = React.createClass({
237
contextTypes: {