diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 0895b7cd1..b25d04854 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -425,6 +425,35 @@ describe('shallow', () => { expect(shallow().equals()).to.equal(false); }); }); + + it('flattens arrays of children to compare', () => { + class TwoChildren extends React.Component { + render() { + return ( +
+
+
+
+ ); + } + } + + class TwoChildrenOneArrayed extends React.Component { + render() { + return ( +
+
+ {[
]} +
+ ); + } + } + const twoChildren = shallow(); + const twoChildrenOneArrayed = shallow(); + + expect(twoChildren.equals(twoChildrenOneArrayed.getElement())).to.equal(true); + expect(twoChildrenOneArrayed.equals(twoChildren.getElement())).to.equal(true); + }); }); describe('.hostNodes()', () => { diff --git a/packages/enzyme/package.json b/packages/enzyme/package.json index 1f5ae114f..8314b8138 100644 --- a/packages/enzyme/package.json +++ b/packages/enzyme/package.json @@ -34,6 +34,7 @@ "author": "Leland Richardson ", "license": "MIT", "dependencies": { + "array.prototype.flat": "^1.2.1", "cheerio": "^1.0.0-rc.2", "function.prototype.name": "^1.0.3", "has": "^1.0.1", diff --git a/packages/enzyme/src/Utils.js b/packages/enzyme/src/Utils.js index 20e295f30..bc7d49cf8 100644 --- a/packages/enzyme/src/Utils.js +++ b/packages/enzyme/src/Utils.js @@ -4,6 +4,8 @@ import is from 'object-is'; import entries from 'object.entries'; import functionName from 'function.prototype.name'; import has from 'has'; +import flat from 'array.prototype.flat'; + import configuration from './configuration'; import validateAdapter from './validateAdapter'; import { childrenOfNode } from './RSTTraversal'; @@ -72,11 +74,12 @@ function internalChildrenCompare(a, b, lenComp, isLoose) { if (!Array.isArray(a) && !Array.isArray(b)) { return nodeCompare(a, b, lenComp); } - if (!a && !b) return true; - if (a.length !== b.length) return false; - if (a.length === 0 && b.length === 0) return true; - for (let i = 0; i < a.length; i += 1) { - if (!nodeCompare(a[i], b[i], lenComp)) return false; + const flatA = flat(a, Infinity); + const flatB = flat(b, Infinity); + if (flatA.length !== flatB.length) return false; + if (flatA.length === 0 && flatB.length === 0) return true; + for (let i = 0; i < flatA.length; i += 1) { + if (!nodeCompare(flatA[i], flatB[i], lenComp)) return false; } return true; }