Skip to content

Commit 4854d43

Browse files
author
Jason Zukewich
committed
add portal shallow support
1 parent 1b0caee commit 4854d43

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

packages/enzyme-adapter-utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"dependencies": {
3737
"function.prototype.name": "^1.1.0",
3838
"object.assign": "^4.1.0",
39-
"prop-types": "^15.6.2"
39+
"prop-types": "^15.6.2",
40+
"react-is": "^16.4.2"
4041
},
4142
"peerDependencies": {
4243
"react": "0.13.x || 0.14.x || ^15.0.0-0 || ^16.0.0-0"

packages/enzyme-adapter-utils/src/Utils.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import functionName from 'function.prototype.name';
22
import createMountWrapper from './createMountWrapper';
33
import createRenderWrapper from './createRenderWrapper';
4+
import {
5+
Portal,
6+
} from 'react-is';
47

58
export { createMountWrapper, createRenderWrapper };
69

@@ -94,6 +97,10 @@ export function displayNameOfNode(node) {
9497

9598
if (!type) return null;
9699

100+
if (type === Portal) {
101+
return 'Portal';
102+
}
103+
97104
return type.displayName || (typeof type === 'function' ? functionName(type) : type.name || type);
98105
}
99106

@@ -104,6 +111,9 @@ export function nodeTypeFromType(type) {
104111
if (type && type.prototype && type.prototype.isReactComponent) {
105112
return 'class';
106113
}
114+
if (type && type === Portal) {
115+
return 'portal';
116+
}
107117
return 'function';
108118
}
109119

@@ -168,9 +178,20 @@ export function ensureKeyOrUndefined(key) {
168178
}
169179

170180
export function elementToTree(el) {
171-
if (el === null || typeof el !== 'object' || !('type' in el)) {
181+
if (el === null || typeof el !== 'object' || !('type' in el || el.$$typeof === Portal)) {
172182
return el;
173183
}
184+
if (el.$$typeof === Portal) {
185+
return {
186+
nodeType: nodeTypeFromType(Portal),
187+
type: Portal,
188+
props: {},
189+
key: ensureKeyOrUndefined(el.key),
190+
ref: el.ref,
191+
instance: null,
192+
rendered: elementToTree(el.children),
193+
};
194+
}
174195
const {
175196
type,
176197
props,

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ import {
3535
REACT16,
3636
is,
3737
} from './_helpers/version';
38+
import {
39+
Portal,
40+
} from 'react-is';
3841
import realArrowFunction from './_helpers/realArrowFunction';
3942
import sloppyReturnThis from './_helpers/untranspiledSloppyReturnThis';
4043

0 commit comments

Comments
 (0)