Perform transaction around shallow render to run lifecycle methods
James Friend committed
Sep 28, 2015 at 22:48 UTC
107e3014ef9fc1855424ddd8870c06903c07ca9f
2 files changed
+41
-1
src/test/ReactTestUtils.js
+1
-1
@@ -411,7 +411,7 @@ ReactShallowRenderer.prototype.render = function(element, context) {
411
context = emptyObject;
412
}
413
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(false);
414
- this._render(element, transaction, context);
414
+ transaction.perform(this._render, this, element, transaction, context);
415
ReactUpdates.ReactReconcileTransaction.release(transaction);
416
};
417
src/test/__tests__/ReactTestUtils-test.js
+40
@@ -91,6 +91,46 @@ describe('ReactTestUtils', function() {
91
expect(componentWillUnmount).toBeCalled();
92
});
93
94
+ it('should run all lifecycle methods', function() {
95
+ var componentWillMount = mocks.getMockFunction();
96
+ var componentDidMount = mocks.getMockFunction();
97
+ var componentWillReceiveProps = mocks.getMockFunction();
98
+ var shouldComponentUpdate = mocks.getMockFunction();
99
+ var componentWillUpdate = mocks.getMockFunction();
100
+ var componentDidUpdate = mocks.getMockFunction();
101
+ var componentWillUnmount = mocks.getMockFunction();
102
+
103
+ var SomeComponent = React.createClass({
104
+ render: function() {
105
+ return <SomeComponent onChange={() => this.setState({a: 1})} />;
106
+ },
107
+ componentWillMount,
108
+ componentDidMount,
109
+ componentWillReceiveProps,
110
+ shouldComponentUpdate() {
111
+ shouldComponentUpdate();
112
+ return true;
113
+ },
114
+ componentWillUpdate,
115
+ componentDidUpdate,
116
+ componentWillUnmount,
117
+ });
118
+
119
+ var shallowRenderer = ReactTestUtils.createRenderer();
120
+ shallowRenderer.render(<SomeComponent />);
121
+ shallowRenderer.getRenderOutput().props.onChange();
122
+ shallowRenderer.render(<SomeComponent />);
123
+ shallowRenderer.unmount();
124
+
125
+ expect(componentWillMount).toBeCalled();
126
+ expect(componentDidMount).toBeCalled();
127
+ expect(componentWillReceiveProps).toBeCalled();
128
+ expect(shouldComponentUpdate).toBeCalled();
129
+ expect(componentWillUpdate).toBeCalled();
130
+ expect(componentDidUpdate).toBeCalled();
131
+ expect(componentWillUnmount).toBeCalled();
132
+ });
133
+
134
it('can shallow render to null', function() {
135
var SomeComponent = React.createClass({
136
render: function() {