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

Match children before and after interpolation #512

Merged
merged 2 commits into from
Mar 3, 2017
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
1 change: 1 addition & 0 deletions INTHEWILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Organizations
- [Brave](https://brave.com)
- [Simple](https://github.com/simplefinance)
- [Grab](https://github.com/grab)
- [Megalytic](https://megalytic.com/)

Projects
----------
Expand Down
25 changes: 23 additions & 2 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ function internalNodeCompare(a, b, lenComp, isLoose) {
const childCompare = isLoose ? childrenMatch : childrenEqual;
if (leftHasChildren || rightHasChildren) {
if (!childCompare(
childrenToArray(left.children),
childrenToArray(right.children),
childrenToSimplifiedArray(left.children),
childrenToSimplifiedArray(right.children),
lenComp,
)) {
return false;
Expand Down Expand Up @@ -168,6 +168,27 @@ function arraysEqual(match, left, right) {
return left.length === right.length && left.every((el, i) => match(el, right[i]));
}

export function childrenToSimplifiedArray(nodeChildren) {
const childrenArray = childrenToArray(nodeChildren);
const simplifiedArray = [];

for (let i = 0; i < childrenArray.length; i += 1) {
const child = childrenArray[i];
const previousChild = simplifiedArray.pop();

if (previousChild === undefined) {
simplifiedArray.push(child);
} else if (isTextualNode(child) && isTextualNode(previousChild)) {
simplifiedArray.push(previousChild + child);
} else {
simplifiedArray.push(previousChild);
simplifiedArray.push(child);
}
}

return simplifiedArray;
}

function childrenOfNode(node) {
const props = propsOfNode(node);
const { children } = props;
Expand Down
28 changes: 28 additions & 0 deletions test/Utils-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { describeWithDOM, describeIf } from './_helpers';
import { mount } from '../src';
import {
coercePropValue,
childrenToSimplifiedArray,
getNode,
nodeEqual,
nodeMatches,
Expand Down Expand Up @@ -145,6 +146,13 @@ describe('Utils', () => {
)).to.equal(false);
});

it('should match children before and after interpolation', () => {
expect(nodeEqual(
<div>{2}{' children'}{<span />} abc {'hey'}</div>,
<div>2 children<span /> abc hey</div>,
)).to.equal(true);
});

it('should skip null children', () => {
expect(nodeEqual(
<div>{null}</div>,
Expand Down Expand Up @@ -606,4 +614,24 @@ describe('Utils', () => {
});
});

describe('childrenToSimplifiedArray', () => {
function expectEqualArrays(a, b) {
expect(a.length).to.be.equal(b.length);

const nodesAreEqual = a.every((n, i) => nodeEqual(a[i], b[i]));
expect(nodesAreEqual).to.equal(true);
}

it('should join string and numerical children as a string', () => {
const children = [3, 'textual', 'children'];
const simplified = ['3textualchildren'];
expectEqualArrays(childrenToSimplifiedArray(children), simplified);
});

it('should handle non-textual nodes', () => {
const children = ['with', 1, <div />, 'other node'];
const simplified = ['with1', <div />, 'other node'];
expectEqualArrays(childrenToSimplifiedArray(children), simplified);
});
});
});