Skip to content

Commit

Permalink
Merge pull request #1761 from jgzuke/jgzuke-support-portal-shallow
Browse files Browse the repository at this point in the history
[enzyme-adapter-react-16*, enzyme-adapter-utils] [new] `shallow`: add support for Portals
  • Loading branch information
ljharb authored Aug 18, 2018
2 parents 997f95a + 892d368 commit 5774d0d
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 16 deletions.
34 changes: 31 additions & 3 deletions packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import ShallowRenderer from 'react-test-renderer/shallow';
import TestUtils from 'react-dom/test-utils';
import {
isElement,
isPortal,
isValidElementType,
Fragment,
Portal,
} from 'react-is';
import { EnzymeAdapter } from 'enzyme';
import {
displayNameOfNode,
elementToTree,
nodeTypeFromType,
elementToTree as utilElementToTree,
nodeTypeFromType as utilNodeTypeFromType,
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
Expand Down Expand Up @@ -72,6 +73,33 @@ function flatten(arr) {
return result;
}

function nodeTypeFromType(type) {
if (type === Portal) {
return 'portal';
}

return utilNodeTypeFromType(type);
}

function elementToTree(el) {
if (!isPortal(el)) {
return utilElementToTree(el, elementToTree);
}

const { children, containerInfo } = el;
const props = { children, containerInfo };

return {
nodeType: 'portal',
type: Portal,
props,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: null,
rendered: elementToTree(el.children),
};
}

function toTree(vnode) {
if (vnode == null) {
return null;
Expand Down Expand Up @@ -280,7 +308,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
ref: cachedNode.ref,
instance: renderer._instance,
rendered: Array.isArray(output)
? flatten(output).map(elementToTree)
? flatten(output).map(el => elementToTree(el))
: elementToTree(output),
};
},
Expand Down
34 changes: 31 additions & 3 deletions packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ShallowRenderer from 'react-test-renderer/shallow';
import TestUtils from 'react-dom/test-utils';
import {
isElement,
isPortal,
isValidElementType,
Fragment,
Portal,
Expand All @@ -17,8 +18,8 @@ import { EnzymeAdapter } from 'enzyme';
import { typeOfNode } from 'enzyme/build/Utils';
import {
displayNameOfNode,
elementToTree,
nodeTypeFromType,
elementToTree as utilElementToTree,
nodeTypeFromType as utilNodeTypeFromType,
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
Expand Down Expand Up @@ -73,6 +74,33 @@ function flatten(arr) {
return result;
}

function nodeTypeFromType(type) {
if (type === Portal) {
return 'portal';
}

return utilNodeTypeFromType(type);
}

function elementToTree(el) {
if (!isPortal(el)) {
return utilElementToTree(el, elementToTree);
}

const { children, containerInfo } = el;
const props = { children, containerInfo };

return {
nodeType: 'portal',
type: Portal,
props,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: null,
rendered: elementToTree(el.children),
};
}

function toTree(vnode) {
if (vnode == null) {
return null;
Expand Down Expand Up @@ -282,7 +310,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
ref: cachedNode.ref,
instance: renderer._instance,
rendered: Array.isArray(output)
? flatten(output).map(elementToTree)
? flatten(output).map(el => elementToTree(el))
: elementToTree(output),
};
},
Expand Down
34 changes: 31 additions & 3 deletions packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ShallowRenderer from 'react-test-renderer/shallow';
import TestUtils from 'react-dom/test-utils';
import {
isElement,
isPortal,
isValidElementType,
AsyncMode,
Fragment,
Expand All @@ -24,8 +25,8 @@ import { EnzymeAdapter } from 'enzyme';
import { typeOfNode } from 'enzyme/build/Utils';
import {
displayNameOfNode,
elementToTree,
nodeTypeFromType,
elementToTree as utilElementToTree,
nodeTypeFromType as utilNodeTypeFromType,
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
Expand Down Expand Up @@ -78,6 +79,33 @@ function flatten(arr) {
return result;
}

function nodeTypeFromType(type) {
if (type === Portal) {
return 'portal';
}

return utilNodeTypeFromType(type);
}

function elementToTree(el) {
if (!isPortal(el)) {
return utilElementToTree(el, elementToTree);
}

const { children, containerInfo } = el;
const props = { children, containerInfo };

return {
nodeType: 'portal',
type: Portal,
props,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: null,
rendered: elementToTree(el.children),
};
}

function toTree(vnode) {
if (vnode == null) {
return null;
Expand Down Expand Up @@ -300,7 +328,7 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
ref: cachedNode.ref,
instance: renderer._instance,
rendered: Array.isArray(output)
? flatten(output).map(elementToTree)
? flatten(output).map(el => elementToTree(el))
: elementToTree(output),
};
},
Expand Down
34 changes: 31 additions & 3 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ShallowRenderer from 'react-test-renderer/shallow';
import TestUtils from 'react-dom/test-utils';
import {
isElement,
isPortal,
isValidElementType,
AsyncMode,
Fragment,
Expand All @@ -24,8 +25,8 @@ import { EnzymeAdapter } from 'enzyme';
import { typeOfNode } from 'enzyme/build/Utils';
import {
displayNameOfNode,
elementToTree,
nodeTypeFromType,
elementToTree as utilElementToTree,
nodeTypeFromType as utilNodeTypeFromType,
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
Expand Down Expand Up @@ -78,6 +79,33 @@ function flatten(arr) {
return result;
}

function nodeTypeFromType(type) {
if (type === Portal) {
return 'portal';
}

return utilNodeTypeFromType(type);
}

function elementToTree(el) {
if (!isPortal(el)) {
return utilElementToTree(el, elementToTree);
}

const { children, containerInfo } = el;
const props = { children, containerInfo };

return {
nodeType: 'portal',
type: Portal,
props,
key: ensureKeyOrUndefined(el.key),
ref: el.ref,
instance: null,
rendered: elementToTree(el.children),
};
}

function toTree(vnode) {
if (vnode == null) {
return null;
Expand Down Expand Up @@ -303,7 +331,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
ref: cachedNode.ref,
instance: renderer._instance,
rendered: Array.isArray(output)
? flatten(output).map(elementToTree)
? flatten(output).map(el => elementToTree(el))
: elementToTree(output),
};
},
Expand Down
10 changes: 7 additions & 3 deletions packages/enzyme-adapter-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ export function ensureKeyOrUndefined(key) {
return key || (key === '' ? '' : undefined);
}

export function elementToTree(el) {
export function elementToTree(el, recurse = elementToTree) {
if (typeof recurse !== 'function' && arguments.length === 3) {
// special case for backwards compat for `.map(elementToTree)`
recurse = elementToTree; // eslint-disable-line no-param-reassign
}
if (el === null || typeof el !== 'object' || !('type' in el)) {
return el;
}
Expand All @@ -193,9 +197,9 @@ export function elementToTree(el) {
const { children } = props;
let rendered = null;
if (isArrayLike(children)) {
rendered = flatten(children).map(elementToTree);
rendered = flatten(children).map(x => recurse(x));
} else if (typeof children !== 'undefined') {
rendered = elementToTree(children);
rendered = recurse(children);
}
return {
nodeType: nodeTypeFromType(type),
Expand Down
3 changes: 2 additions & 1 deletion packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.1"
"eslint-plugin-react": "^7.11.1",
"react-is": "^16.4.2"
}
}
Loading

0 comments on commit 5774d0d

Please sign in to comment.