Skip to content

Commit

Permalink
[enzyme-adapter-react-15.4] [fix] handle DOM nodes in react v15.0 and…
Browse files Browse the repository at this point in the history
… v15.1
  • Loading branch information
mul53 authored and ljharb committed Jan 31, 2021
1 parent a64a60b commit db8e4c6
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TestUtils from 'react-addons-test-utils';
import values from 'object.values';
import { isElement, isValidElementType } from 'react-is';
import { EnzymeAdapter } from 'enzyme';
import has from 'has';
import {
displayNameOfNode,
elementToTree,
Expand Down Expand Up @@ -117,6 +116,31 @@ function instanceToTree(inst) {
};
}

function nodeToDOMNode(node) {
// TODO: investigate other primitive types
if (typeof node === 'string' || typeof node === 'number') {
return global.document.createTextNode(node);
}

if (node && node.nodeType === 'host') {
const domNode = global.document.createElement(node.type);

Object.keys(node.props).filter((x) => x !== 'children').forEach((propKey) => {
if (propKey === 'className') {
domNode.setAttribute('class', node.props[propKey]);
} else {
domNode.setAttribute(propKey, node.props[propKey]);
}
});

node.rendered.map(nodeToDOMNode).filter(Boolean).forEach((child) => {
domNode.appendChild(child);
});

return domNode;
}
}

const eventOptions = { animation: true };

class ReactFifteenFourAdapter extends EnzymeAdapter {
Expand Down Expand Up @@ -304,6 +328,9 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
}

nodeToHostNode(node) {
if (!node.instance) {
return nodeToDOMNode(node);
}
return ReactDOM.findDOMNode(node.instance);
}

Expand Down

0 comments on commit db8e4c6

Please sign in to comment.