Skip to content

Commit

Permalink
[Fix] mount: properly handle HTML of multiple nodes
Browse files Browse the repository at this point in the history
Fixes #2052.
  • Loading branch information
ljharb committed Mar 15, 2019
1 parent e695eb4 commit 558d5cf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
26 changes: 26 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -5399,6 +5399,27 @@ describeWithDOM('mount', () => {
</Fragment>
);

class ClassChild extends React.Component {
render() {
return <div>Class child</div>;
}
}

function SFCChild() {
return <div>SFC child</div>;
}

class FragmentWithCustomChildClass extends React.Component {
render() {
return (
<Fragment>
<ClassChild />
<SFCChild />
</Fragment>
);
}
}

it('correctly renders html for both children for class', () => {
const classWrapper = mount(<FragmentClassExample />);
expect(classWrapper.html()).to.equal('<div><span>Foo</span></div><div><span>Bar</span></div>');
Expand All @@ -5408,6 +5429,11 @@ describeWithDOM('mount', () => {
const constWrapper = mount(<FragmentConstExample />);
expect(constWrapper.html()).to.equal('<div><span>Foo</span></div><div><span>Bar</span></div>');
});

it('correctly renders html for custom component children', () => {
const withChildrenWrapper = mount(<FragmentWithCustomChildClass />);
expect(withChildrenWrapper.html()).to.equal('<div>Class child</div><div>SFC child</div>');
});
});
});

Expand Down
26 changes: 26 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -5487,6 +5487,27 @@ describe('shallow', () => {
</Fragment>
);

class ClassChild extends React.Component {
render() {
return <div>Class child</div>;
}
}

function SFCChild() {
return <div>SFC child</div>;
}

class FragmentWithCustomChildClass extends React.Component {
render() {
return (
<Fragment>
<ClassChild />
<SFCChild />
</Fragment>
);
}
}

it('correctly renders html for both children for class', () => {
const classWrapper = shallow(<FragmentClassExample />);
expect(classWrapper.html()).to.equal('<div><span>Foo</span></div><div><span>Bar</span></div>');
Expand All @@ -5496,6 +5517,11 @@ describe('shallow', () => {
const constWrapper = shallow(<FragmentConstExample />);
expect(constWrapper.html()).to.equal('<div><span>Foo</span></div><div><span>Bar</span></div>');
});

it('correctly renders html for custom component children', () => {
const withChildrenWrapper = shallow(<FragmentWithCustomChildClass />);
expect(withChildrenWrapper.html()).to.equal('<div>Class child</div><div>SFC child</div>');
});
});
});

Expand Down
23 changes: 14 additions & 9 deletions packages/enzyme/src/RSTTraversal.js
Expand Up @@ -142,6 +142,7 @@ function getTextFromRSTNode(node, {
getCustom,
handleHostNodes,
recurse,
nullRenderReturnsNull = false,
}) {
if (node == null) {
return '';
Expand All @@ -158,6 +159,9 @@ function getTextFromRSTNode(node, {
if (handleHostNodes && node.nodeType === 'host') {
return handleHostNodes(node);
}
if (node.rendered == null && nullRenderReturnsNull) {
return null;
}
return childrenOfNode(node).map(recurse).join('');
}

Expand Down Expand Up @@ -190,13 +194,14 @@ function getHTMLFromHostNode(hostNode) {
}

export function getHTMLFromHostNodes(node, adapter) {
if (node === null) return null;

const hostNode = adapter.nodeToHostNode(node, true);
if (hostNode === null) return null;

const nodeArray = Array.isArray(hostNode) ? hostNode : [hostNode];
const nodesHTML = nodeArray.map(item => getHTMLFromHostNode(item));

return nodesHTML.join('');
return getTextFromRSTNode(node, {
recurse(item) {
return getHTMLFromHostNodes(item, adapter);
},
handleHostNodes(item) {
const nodes = [].concat(adapter.nodeToHostNode(item, true));
return nodes.map(getHTMLFromHostNode).join('');
},
nullRenderReturnsNull: true,
});
}

0 comments on commit 558d5cf

Please sign in to comment.