From c74acb4b27d866e31662b0e1cc42e7e120410d72 Mon Sep 17 00:00:00 2001 From: Jason Zukewich Date: Thu, 16 Aug 2018 15:58:45 -0700 Subject: [PATCH] add portal support --- .../src/ReactSixteenOneAdapter.js | 13 ++++++++++--- .../src/ReactSixteenTwoAdapter.js | 13 ++++++++++--- .../src/ReactSixteenThreeAdapter.js | 13 ++++++++++--- .../src/ReactSixteenAdapter.js | 13 ++++++++++--- packages/enzyme-adapter-utils/package.json | 3 ++- packages/enzyme-adapter-utils/src/Utils.js | 10 ++++++++++ 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js b/packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js index 3608f6733..67588cde8 100644 --- a/packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js +++ b/packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js @@ -83,9 +83,16 @@ function toTree(vnode) { switch (node.tag) { case HostRoot: // 3 return childrenToTree(node.child); - case HostPortal: { // 4 - return childrenToTree(node.child); - } + case HostPortal: // 4 + return { + nodeType: 'portal', + type: Portal, + props: {}, + key: ensureKeyOrUndefined(node.key), + ref: node.ref, + instance: null, + rendered: childrenToTree(node.child), + }; case ClassComponent: return { nodeType: 'class', diff --git a/packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js b/packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js index 6e94a9850..6387bf8aa 100644 --- a/packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js +++ b/packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js @@ -84,9 +84,16 @@ function toTree(vnode) { switch (node.tag) { case HostRoot: // 3 return childrenToTree(node.child); - case HostPortal: { // 4 - return childrenToTree(node.child); - } + case HostPortal: // 4 + return { + nodeType: 'portal', + type: Portal, + props: {}, + key: ensureKeyOrUndefined(node.key), + ref: node.ref, + instance: null, + rendered: childrenToTree(node.child), + }; case ClassComponent: return { nodeType: 'class', diff --git a/packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js b/packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js index 0524d069d..22a45adba 100644 --- a/packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js +++ b/packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js @@ -89,9 +89,16 @@ function toTree(vnode) { switch (node.tag) { case HostRoot: // 3 return childrenToTree(node.child); - case HostPortal: { // 4 - return childrenToTree(node.child); - } + case HostPortal: // 4 + return { + nodeType: 'portal', + type: Portal, + props: {}, + key: ensureKeyOrUndefined(node.key), + ref: node.ref, + instance: null, + rendered: childrenToTree(node.child), + }; case ClassComponent: return { nodeType: 'class', diff --git a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js index 1cce2322a..3b80f6d9b 100644 --- a/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js +++ b/packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js @@ -89,9 +89,16 @@ function toTree(vnode) { switch (node.tag) { case HostRoot: // 3 return childrenToTree(node.child); - case HostPortal: { // 4 - return childrenToTree(node.child); - } + case HostPortal: // 4 + return { + nodeType: 'portal', + type: Portal, + props: {}, + key: ensureKeyOrUndefined(node.key), + ref: node.ref, + instance: null, + rendered: childrenToTree(node.child), + }; case ClassComponent: return { nodeType: 'class', diff --git a/packages/enzyme-adapter-utils/package.json b/packages/enzyme-adapter-utils/package.json index 81acdbdee..969b82646 100644 --- a/packages/enzyme-adapter-utils/package.json +++ b/packages/enzyme-adapter-utils/package.json @@ -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" diff --git a/packages/enzyme-adapter-utils/src/Utils.js b/packages/enzyme-adapter-utils/src/Utils.js index 6f3f77a72..53ad36c13 100644 --- a/packages/enzyme-adapter-utils/src/Utils.js +++ b/packages/enzyme-adapter-utils/src/Utils.js @@ -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 }; @@ -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); } @@ -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'; }