Better error for invalid element when shallow rendering

Fixes #4721, fixes #4730.
This commit is contained in:
Ben Alpert committed 2015-08-28 18:21:30 -07:00
1 parent 72d5b8f116
commit f4d7feb8be
2 files changed
+37

No files matched your search

+16
View File
@@ -396,6 +396,22 @@ assign(
);
ReactShallowRenderer.prototype.render = function(element, context) {
invariant(
ReactElement.isValidElement(element),
'ReactShallowRenderer render(): Invalid component element.%s',
typeof element === 'function' ?
' Instead of passing a component class, make sure to instantiate ' +
'it by passing it to React.createElement.' :
''
);
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
);
if (!context) {
context = emptyObject;
}
+21
View File
@@ -53,6 +53,27 @@ describe('ReactTestUtils', function() {
]);
});
it('should throw for invalid elements', function() {
var SomeComponent = React.createClass({
render: function() {
return <div />;
},
});
var shallowRenderer = ReactTestUtils.createRenderer();
expect(() => shallowRenderer.render(SomeComponent)).toThrow(
'Invariant Violation: ReactShallowRenderer render(): Invalid component ' +
'element. Instead of passing a component class, make sure to ' +
'instantiate it by passing it to React.createElement.'
);
expect(() => shallowRenderer.render(<div />)).toThrow(
'Invariant Violation: ReactShallowRenderer render(): Shallow rendering ' +
'works only with custom components, not primitives (div). Instead of ' +
'calling `.render(el)` and inspecting the rendered output, look at ' +
'`el.props` directly instead.'
);
});
it('should have shallow unmounting', function() {
var componentWillUnmount = mocks.getMockFunction();