mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Merge pull request #4993 from jsdf/shallow-render-lifecycle
Run all component lifecycle methods when shallow rendering
This commit is contained in:
commit
d2888798f0
2 files changed
+41
-1
No files matched your search
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user