Skip to content

Commit

Permalink
Merge pull request #4993 from jsdf/shallow-render-lifecycle
Browse files Browse the repository at this point in the history
Run all component lifecycle methods when shallow rendering
  • Loading branch information
sophiebits committed Nov 3, 2015
2 parents 1728902 + 107e301 commit d288879
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/test/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ ReactShallowRenderer.prototype.render = function(element, context) {
context = emptyObject;
}
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(false);
this._render(element, transaction, context);
transaction.perform(this._render, this, element, transaction, context);
ReactUpdates.ReactReconcileTransaction.release(transaction);
};

Expand Down
40 changes: 40 additions & 0 deletions src/test/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ describe('ReactTestUtils', function() {
expect(componentWillUnmount).toBeCalled();
});

it('should run all lifecycle methods', function() {
var componentWillMount = mocks.getMockFunction();
var componentDidMount = mocks.getMockFunction();
var componentWillReceiveProps = mocks.getMockFunction();
var shouldComponentUpdate = mocks.getMockFunction();
var componentWillUpdate = mocks.getMockFunction();
var componentDidUpdate = mocks.getMockFunction();
var componentWillUnmount = mocks.getMockFunction();

var SomeComponent = React.createClass({
render: function() {
return <SomeComponent onChange={() => this.setState({a: 1})} />;
},
componentWillMount,
componentDidMount,
componentWillReceiveProps,
shouldComponentUpdate() {
shouldComponentUpdate();
return true;
},
componentWillUpdate,
componentDidUpdate,
componentWillUnmount,
});

var shallowRenderer = ReactTestUtils.createRenderer();
shallowRenderer.render(<SomeComponent />);
shallowRenderer.getRenderOutput().props.onChange();
shallowRenderer.render(<SomeComponent />);
shallowRenderer.unmount();

expect(componentWillMount).toBeCalled();
expect(componentDidMount).toBeCalled();
expect(componentWillReceiveProps).toBeCalled();
expect(shouldComponentUpdate).toBeCalled();
expect(componentWillUpdate).toBeCalled();
expect(componentDidUpdate).toBeCalled();
expect(componentWillUnmount).toBeCalled();
});

it('can shallow render to null', function() {
var SomeComponent = React.createClass({
render: function() {
Expand Down

0 comments on commit d288879

Please sign in to comment.