Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support all lifecycle methods to ShallowRender #7912

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/test/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,12 @@ Object.assign(
return new NoopInternalComponent(element);
},
_replaceNodeWithMarkup: function() {},
_renderValidatedComponent:
ReactCompositeComponent
._renderValidatedComponentWithoutOwnerOrContext,
}
);

function _batchedRender(renderer, element, context) {
var transaction = ReactUpdates.ReactReconcileTransaction.getPooled(true);
renderer._render(element, transaction, context);
transaction.perform(renderer._render, renderer, element, transaction, context);
ReactUpdates.ReactReconcileTransaction.release(transaction);
}

Expand Down Expand Up @@ -109,11 +106,39 @@ class ReactShallowRenderer {
return this.getRenderOutput();
}
getRenderOutput() {
return (
(this._instance && this._instance._renderedComponent &&
this._instance._renderedComponent._renderedOutput)
|| null
const renderedElement = (
this._instance && this._instance._renderedComponent &&
this._instance._renderedComponent._renderedOutput
);
return renderedElement ? this._getShallowElement(renderedElement) : null;
}
_getShallowElement(element) {
if (typeof element !== 'object') {
return element;
}
return Object.keys(element).reduce((current, key) => {
if (key === 'props') {
return Object.assign(current, {
props: Object.keys(element.props).reduce((props, propKey) => {
if (propKey === 'children') {
return Object.assign(props, {
[propKey]: Array.isArray(element.props.children)
? element.props.children.map(c => this._getShallowElement(c))
: this._getShallowElement(element.props.children),
});
} else {
return this._getShallowElementProps(props, propKey, element.props[propKey]);
}
}, {}),
});
}
return this._getShallowElementProps(current, key, element[key]);
}, {});
}
_getShallowElementProps(element, prop, value) {
return Object.assign(element, {
[prop]: prop === '_owner' ? null : value,
});
}
unmount() {
if (this._instance) {
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 @@ -118,6 +118,46 @@ describe('ReactTestUtils', () => {
expect(result).toBe(null);
});

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

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 with a ref', () => {
class SomeComponent extends React.Component {
render() {
Expand Down