Skip to content

Commit

Permalink
[Fix] shallow/mount: improve error message when wrapping invalid …
Browse files Browse the repository at this point in the history
…elements
  • Loading branch information
jgzuke authored and ljharb committed Aug 16, 2018
1 parent 9ef2cc8 commit 997f95a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,48 @@ describeWithDOM('mount', () => {
expect(spy).to.have.property('callCount', 1);
});

describe('wrapping invalid elements', () => {
itIf(is('>= 16'), 'should throw when mounting Portals', () => {
const portal = createPortal(
<div />,
{ nodeType: 1 },
);

expect(() => mount(portal)).to.throw(
Error,
'ReactWrapper can only wrap valid elements',
);
});

it('should throw when mounting plain text', () => {
expect(() => mount('Foo')).to.throw(
Error,
'ReactWrapper can only wrap valid elements',
);
});

it('should throw when mounting multiple elements', () => {
expect(() => mount([<div />])).to.throw(
TypeError,
'ReactWrapper can only wrap valid elements',
);
});
});

it('should mount built in components', () => {
expect(() => mount(<div />)).not.to.throw();
});

it('should mount composite components', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}

expect(() => mount(<Foo />)).not.to.throw();
});

describeIf(is('>= 16.3'), 'uses the isValidElementType from the Adapter to validate the prop type of Component', () => {
const Foo = () => null;
const Bar = () => null;
Expand Down
43 changes: 43 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './_helpers/setupAdapters';
import {
createClass,
createContext,
createPortal,
createRef,
Fragment,
forwardRef,
Expand Down Expand Up @@ -77,6 +78,48 @@ describe('shallow', () => {
expect(wrapper.children().type()).to.equal('div');
expect(wrapper.children().props().bam).to.equal(undefined);
});

describe('wrapping invalid elements', () => {
itIf(is('>= 16'), 'should throw when shallow rendering Portals', () => {
const portal = createPortal(
<div />,
{ nodeType: 1 },
);

expect(() => shallow(portal)).to.throw(
Error,
'ShallowWrapper can only wrap valid elements',
);
});

it('should throw when shallow rendering plain text', () => {
expect(() => shallow('Foo')).to.throw(
Error,
'ShallowWrapper can only wrap valid elements',
);
});

it('should throw when shallow rendering multiple elements', () => {
expect(() => shallow([<div />])).to.throw(
TypeError,
'ShallowWrapper can only wrap valid elements',
);
});
});

it('should shallow render built in components', () => {
expect(() => shallow(<div />)).not.to.throw();
});

it('should shallow render composite components', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}

expect(() => shallow(<Foo />)).not.to.throw();
});
});

describe('context', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ class ReactWrapper {
const options = makeOptions(passedOptions);

if (!root) {
const adapter = getAdapter(options);
if (!adapter.isValidElement(nodes)) {
throw new TypeError('ReactWrapper can only wrap valid elements');
}

privateSet(this, UNRENDERED, nodes);
const renderer = getAdapter(options).createRenderer({ mode: 'mount', ...options });
const renderer = adapter.createRenderer({ mode: 'mount', ...options });
privateSet(this, RENDERER, renderer);
renderer.render(nodes, options.context);
privateSet(this, ROOT, this);
Expand Down
4 changes: 4 additions & 0 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class ShallowWrapper {

// mounting a ShallowRender component
if (!root) {
if (!adapter.isValidElement(nodes)) {
throw new TypeError('ShallowWrapper can only wrap valid elements');
}

privateSet(this, ROOT, this);
privateSet(this, UNRENDERED, nodes);
const renderer = adapter.createRenderer({ mode: 'shallow', ...options });
Expand Down

0 comments on commit 997f95a

Please sign in to comment.