Skip to content

Commit d288879

Browse files
committed
Merge pull request #4993 from jsdf/shallow-render-lifecycle
Run all component lifecycle methods when shallow rendering
2 parents 1728902 + 107e301 commit d288879

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/test/ReactTestUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ ReactShallowRenderer.prototype.render = function(element, context) {
433433
context = emptyObject;
434434
}
435435
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(false);
436-
this._render(element, transaction, context);
436+
transaction.perform(this._render, this, element, transaction, context);
437437
ReactUpdates.ReactReconcileTransaction.release(transaction);
438438
};
439439

src/test/__tests__/ReactTestUtils-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,46 @@ describe('ReactTestUtils', function() {
8787
expect(componentWillUnmount).toBeCalled();
8888
});
8989

90+
it('should run all lifecycle methods', function() {
91+
var componentWillMount = mocks.getMockFunction();
92+
var componentDidMount = mocks.getMockFunction();
93+
var componentWillReceiveProps = mocks.getMockFunction();
94+
var shouldComponentUpdate = mocks.getMockFunction();
95+
var componentWillUpdate = mocks.getMockFunction();
96+
var componentDidUpdate = mocks.getMockFunction();
97+
var componentWillUnmount = mocks.getMockFunction();
98+
99+
var SomeComponent = React.createClass({
100+
render: function() {
101+
return <SomeComponent onChange={() => this.setState({a: 1})} />;
102+
},
103+
componentWillMount,
104+
componentDidMount,
105+
componentWillReceiveProps,
106+
shouldComponentUpdate() {
107+
shouldComponentUpdate();
108+
return true;
109+
},
110+
componentWillUpdate,
111+
componentDidUpdate,
112+
componentWillUnmount,
113+
});
114+
115+
var shallowRenderer = ReactTestUtils.createRenderer();
116+
shallowRenderer.render(<SomeComponent />);
117+
shallowRenderer.getRenderOutput().props.onChange();
118+
shallowRenderer.render(<SomeComponent />);
119+
shallowRenderer.unmount();
120+
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();
128+
});
129+
90130
it('can shallow render to null', function() {
91131
var SomeComponent = React.createClass({
92132
render: function() {

0 commit comments

Comments
 (0)