Skip to content
Merged
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
19 changes: 11 additions & 8 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,22 @@ class ReactShallowRenderer {
'it by passing it to React.createElement.'
: '',
);
// Show a special message for host elements since it's a common case.
invariant(
typeof element.type !== 'string',
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, not primitives (%s). Instead of calling `.render(el)` and ' +
'inspecting the rendered output, look at `el.props` directly instead.',
element.type,
);
invariant(
typeof element.type === 'function',
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `%s`.',
Array.isArray(element.type)
? 'array'
: element.type === null ? 'null' : typeof element.type,
);

if (this._rendering) {
return;
Expand Down Expand Up @@ -144,12 +153,8 @@ class ReactShallowRenderer {
) {
this._instance.componentWillReceiveProps(props, context);
}

// Read state after cWRP in case it calls setState
// Fallback to previous instance state to support rendering React.cloneElement()
// TODO: the cloneElement() use case is broken and should be removed
// https://github.com/facebook/react/issues/11441
const state = this._newState || this._instance.state || emptyObject;
const state = this._newState || oldState;

let shouldUpdate = true;
if (this._forcedUpdate) {
Expand All @@ -161,9 +166,7 @@ class ReactShallowRenderer {
state,
context,
);
} else if (type && type.prototype && type.prototype.isPureReactComponent) {
// TODO: we can remove the type existence check when we fix this:
// https://github.com/facebook/react/issues/11441
} else if (type.prototype && type.prototype.isPureReactComponent) {
shouldUpdate =
!shallowEqual(oldProps, props) || !shallowEqual(oldState, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,12 +844,44 @@ describe('ReactShallowRenderer', () => {
}

const shallowRenderer = createRenderer();
let result = shallowRenderer.render(<SimpleComponent foo="foo" />);
const el = <SimpleComponent foo="foo" />;
let result = shallowRenderer.render(el);
expect(result).toEqual(<div>foo:bar</div>);

const instance = shallowRenderer.getMountedInstance();
const cloned = React.cloneElement(instance, {foo: 'baz'});
const cloned = React.cloneElement(el, {foo: 'baz'});
result = shallowRenderer.render(cloned);
expect(result).toEqual(<div>baz:bar</div>);
});

it('throws usefully when rendering badly-typed elements', () => {
spyOn(console, 'error');
const shallowRenderer = createRenderer();

var Undef = undefined;
expect(() => shallowRenderer.render(<Undef />)).toThrowError(
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `undefined`.',
);

var Null = null;
expect(() => shallowRenderer.render(<Null />)).toThrowError(
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `null`.',
);

var Arr = [];
expect(() => shallowRenderer.render(<Arr />)).toThrowError(
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `array`.',
);

var Obj = {};
expect(() => shallowRenderer.render(<Obj />)).toThrowError(
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `object`.',
);

// One warning for each element creation
expectDev(console.error.calls.count()).toBe(4);
});
});