Skip to content

Commit

Permalink
Support portals
Browse files Browse the repository at this point in the history
  • Loading branch information
louisscruz committed Nov 9, 2017
1 parent 29aab57 commit 6ec76ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const HostRoot = 3;
const ClassComponent = 2;
const Fragment = 10;
const FunctionalComponent = 1;
const HostPortal = 4;
const HostComponent = 5;
const HostText = 6;

Expand Down Expand Up @@ -68,6 +69,8 @@ function toTree(vnode) {
switch (node.tag) {
case HostRoot: // 3
return toTree(node.child);
case HostPortal: // 4
return toTree(node.child);
case ClassComponent:
return {
nodeType: 'class',
Expand Down
19 changes: 18 additions & 1 deletion packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ITERATOR_SYMBOL, sym } from 'enzyme/build/Utils';

import './_helpers/setupAdapters';
import { createClass } from './_helpers/react-compat';
import { createClass, createPortal } from './_helpers/react-compat';
import {
describeWithDOM,
describeIf,
Expand Down Expand Up @@ -758,6 +758,23 @@ describeWithDOM('mount', () => {
expect(wrapper.find('[data-foo="bar baz quz"]')).to.have.length(0);
});

itIf(REACT16, 'should find elements through portals', () => {
const containerDiv = global.document.createElement('div');

class FooPortal extends React.Component {
render() {
return createPortal(
this.props.children,
containerDiv,
);
}
}

const wrapper = mount(<FooPortal><h1>Successful Portal!</h1></FooPortal>);
expect(wrapper.find('h1').length).to.equal(1);
expect(containerDiv.querySelectorAll('h1').length).to.equal(1);
});

describeIf(!REACT013, 'stateless function components', () => {
it('should find a component based on a constructor', () => {
const Foo = () => <div />;
Expand Down
8 changes: 8 additions & 0 deletions packages/enzyme-test-suite/test/_helpers/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { is } from './version';

let createClass;
let renderToString;
let createPortal;

if (is('>=15.5 || ^16.0.0-alpha')) {
// eslint-disable-next-line import/no-extraneous-dependencies
Expand All @@ -23,7 +24,14 @@ if (is('^0.13.0')) {
({ renderToString } = require('react-dom/server'));
}

if (is('^16.0.0-alpha')) {
({ createPortal } = require('react-dom'));
} else {
createPortal = null;
}

export {
createClass,
renderToString,
createPortal,
};

0 comments on commit 6ec76ae

Please sign in to comment.