@samitouri / QOS-React-2 / commits / 9419976e51

Revert #4993 with an added test for refs

We were shallow-rendering a component that used refs at FB so this can't go in as-is. It's a little unclear what we _should_ do though, since there is nothing to hold a ref to (since we're shallowly rendering) and we generally promise that child refs are resolved before a parent's componentDidMount. Also, changing shallow rendering to use the original `_renderValidatedComponent` (instead of `_renderValidatedComponentWithoutOwnerOrContext`) breaks tests because now the `_owner` field doesn't match up for `toEqual` (non-null in `getRenderOutput` but null if constructed in a test).

Ben Alpert committed Nov 4, 2015 at 16:33 UTC 9419976e519bb7bda297dd4b57054a5942ddf8e8
2 files changed +9 -37
src/test/ReactTestUtils.js
+1 -1
@@ -431,7 +431,7 @@ ReactShallowRenderer.prototype.render = function(element, context) {
431 context = emptyObject;
432 }
433 var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
434 - transaction.perform(this._render, this, element, transaction, context);
434 + this._render(element, transaction, context);
435 ReactUpdates.ReactReconcileTransaction.release(transaction);
436 };
437
src/test/__tests__/ReactTestUtils-test.js
+8 -36
@@ -87,59 +87,31 @@ describe('ReactTestUtils', function() {
87 expect(componentWillUnmount).toBeCalled();
88 });
89
90 - it('should run all lifecycle methods', function() {
91 - var componentWillMount = jest.genMockFn();
92 - var componentDidMount = jest.genMockFn();
93 - var componentWillReceiveProps = jest.genMockFn();
94 - var shouldComponentUpdate = jest.genMockFn();
95 - var componentWillUpdate = jest.genMockFn();
96 - var componentDidUpdate = jest.genMockFn();
97 - var componentWillUnmount = jest.genMockFn();
98 -
90 + it('can shallow render to null', function() {
91 var SomeComponent = React.createClass({
92 render: function() {
101 - return <SomeComponent onChange={() => this.setState({a: 1})} />;
102 - },
103 - componentWillMount,
104 - componentDidMount,
105 - componentWillReceiveProps,
106 - shouldComponentUpdate() {
107 - shouldComponentUpdate();
108 - return true;
93 + return null;
94 },
110 - componentWillUpdate,
111 - componentDidUpdate,
112 - componentWillUnmount,
95 });
96
97 var shallowRenderer = ReactTestUtils.createRenderer();
98 shallowRenderer.render(<SomeComponent />);
117 - shallowRenderer.getRenderOutput().props.onChange();
118 - shallowRenderer.render(<SomeComponent />);
119 - shallowRenderer.unmount();
99
121 - expect(componentWillMount).toBeCalled();
122 - expect(componentDidMount).toBeCalled();
123 - expect(componentWillReceiveProps).toBeCalled();
124 - expect(shouldComponentUpdate).toBeCalled();
125 - expect(componentWillUpdate).toBeCalled();
126 - expect(componentDidUpdate).toBeCalled();
127 - expect(componentWillUnmount).toBeCalled();
100 + var result = shallowRenderer.getRenderOutput();
101 +
102 + expect(result).toBe(null);
103 });
104
130 - it('can shallow render to null', function() {
105 + it('can shallow render with a ref', function() {
106 var SomeComponent = React.createClass({
107 render: function() {
133 - return null;
108 + return <div ref="hello" />;
109 },
110 });
111
112 var shallowRenderer = ReactTestUtils.createRenderer();
113 + // Shouldn't crash.
114 shallowRenderer.render(<SomeComponent />);
139 -
140 - var result = shallowRenderer.getRenderOutput();
141 -
142 - expect(result).toBe(null);
115 });
116
117 it('lets you update shallowly rendered components', function() {