Skip to content

Commit

Permalink
add portal shallow support
Browse files Browse the repository at this point in the history
  • Loading branch information
jgzuke committed Aug 16, 2018
1 parent b63890b commit 9cf359f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/enzyme-adapter-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"dependencies": {
"function.prototype.name": "^1.1.0",
"object.assign": "^4.1.0",
"prop-types": "^15.6.2"
"prop-types": "^15.6.2",
"react-is": "^16.4.2"
},
"peerDependencies": {
"react": "0.13.x || 0.14.x || ^15.0.0-0 || ^16.0.0-0"
Expand Down
23 changes: 22 additions & 1 deletion packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import functionName from 'function.prototype.name';
import createMountWrapper from './createMountWrapper';
import createRenderWrapper from './createRenderWrapper';
import {
Portal,
} from 'react-is';

export { createMountWrapper, createRenderWrapper };

Expand Down Expand Up @@ -94,6 +97,10 @@ export function displayNameOfNode(node) {

if (!type) return null;

if (type === Portal) {
return 'Portal';
}

return type.displayName || (typeof type === 'function' ? functionName(type) : type.name || type);
}

Expand All @@ -104,6 +111,9 @@ export function nodeTypeFromType(type) {
if (type && type.prototype && type.prototype.isReactComponent) {
return 'class';
}
if (type && type === Portal) {
return 'portal';
}
return 'function';
}

Expand Down Expand Up @@ -168,9 +178,20 @@ export function ensureKeyOrUndefined(key) {
}

export function elementToTree(el) {
if (el === null || typeof el !== 'object' || !('type' in el)) {
if (el === null || typeof el !== 'object' || !('type' in el || el.$$typeof === Portal)) {
return el;
}
if (el.$$typeof === Portal) {
return {
nodeType: nodeTypeFromType(Portal),
type: Portal,
props: {},
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: null,
rendered: elementToTree(el.children),
};
}
const {
type,
props,
Expand Down
3 changes: 3 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import {
REACT16,
is,
} from './_helpers/version';
import {
Portal,
} from 'react-is';
import realArrowFunction from './_helpers/realArrowFunction';
import sloppyReturnThis from './_helpers/untranspiledSloppyReturnThis';

Expand Down

0 comments on commit 9cf359f

Please sign in to comment.