diff --git a/INTHEWILD.md b/INTHEWILD.md index 679672caf..2946f3855 100644 --- a/INTHEWILD.md +++ b/INTHEWILD.md @@ -19,6 +19,7 @@ Organizations - [Brave](https://brave.com) - [Simple](https://github.com/simplefinance) - [Grab](https://github.com/grab) + - [Megalytic](https://megalytic.com/) Projects ---------- diff --git a/src/Utils.js b/src/Utils.js index 9d6601768..b757b40a9 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -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; @@ -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; diff --git a/test/Utils-spec.jsx b/test/Utils-spec.jsx index 80f9291b7..f87af4290 100644 --- a/test/Utils-spec.jsx +++ b/test/Utils-spec.jsx @@ -7,6 +7,7 @@ import { describeWithDOM, describeIf } from './_helpers'; import { mount } from '../src'; import { coercePropValue, + childrenToSimplifiedArray, getNode, nodeEqual, nodeMatches, @@ -145,6 +146,13 @@ describe('Utils', () => { )).to.equal(false); }); + it('should match children before and after interpolation', () => { + expect(nodeEqual( +
{2}{' children'}{} abc {'hey'}
, +
2 children abc hey
, + )).to.equal(true); + }); + it('should skip null children', () => { expect(nodeEqual(
{null}
, @@ -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,
, 'other node']; + const simplified = ['with1',
, 'other node']; + expectEqualArrays(childrenToSimplifiedArray(children), simplified); + }); + }); });