Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shallow renders though portal child #1761

Merged
merged 2 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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