From 171e95281a4c84654173f588dd7b3ad1651ec969 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 30 Aug 2018 23:54:41 -0700 Subject: [PATCH] [Fix] `shallow`/`mount`: `containsMatchingElement`: trim whitespace Fixes #636. --- .../test/ReactWrapper-spec.jsx | 22 ++++++++++++ .../test/ShallowWrapper-spec.jsx | 34 ++++++++++++++----- packages/enzyme/package.json | 3 +- packages/enzyme/src/Utils.js | 11 ++++-- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index e0456e898..b4cf62492 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -5940,6 +5940,28 @@ describeWithDOM('mount', () => { expect(wrapper.containsMatchingElement(
)).to.equal(true); expect(wrapper.containsMatchingElement(
)).to.equal(true); }); + + it('works with leading and trailing spaces', () => { + const wrapper = mount(( +
  • + All Operations +
  • + )); + + expect(wrapper.containsMatchingElement( All Operations )).to.equal(true); + }); + + it('works with leading and trailing newlines', () => { + const wrapper = mount(( +
  • + + All Operations + +
  • + )); + + expect(wrapper.containsMatchingElement( All Operations )).to.equal(true); + }); }); describe('.containsAllMatchingElements(nodes)', () => { diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 4cea41229..a97bc1412 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -6040,12 +6040,8 @@ describe('shallow', () => {
    Goodbye World
    )); - expect(wrapper.containsMatchingElement(( -
    Hello World
    - ))).to.equal(true); - expect(wrapper.containsMatchingElement(( -
    Goodbye World
    - ))).to.equal(true); + expect(wrapper.containsMatchingElement(
    Hello World
    )).to.equal(true); + expect(wrapper.containsMatchingElement(
    Goodbye World
    )).to.equal(true); expect(wrapper.containsMatchingElement((
    Hello World
    ))).to.equal(true); @@ -6071,9 +6067,7 @@ describe('shallow', () => {
    Goodbye World
    )); - expect(wrapper.containsMatchingElement(( -
    Bonjour le monde
    - ))).to.equal(false); + expect(wrapper.containsMatchingElement(
    Bonjour le monde
    )).to.equal(false); expect(wrapper.containsMatchingElement((
    Au revoir le monde
    ))).to.equal(false); @@ -6102,6 +6096,28 @@ describe('shallow', () => { expect(wrapper.containsMatchingElement(
    )).to.equal(true); expect(wrapper.containsMatchingElement(
    )).to.equal(true); }); + + it('works with leading and trailing spaces', () => { + const wrapper = shallow(( +
  • + All Operations +
  • + )); + + expect(wrapper.containsMatchingElement( All Operations )).to.equal(true); + }); + + it('works with leading and trailing newlines', () => { + const wrapper = shallow(( +
  • + + All Operations + +
  • + )); + + expect(wrapper.containsMatchingElement( All Operations )).to.equal(true); + }); }); describe('.containsAllMatchingElements(nodes)', () => { diff --git a/packages/enzyme/package.json b/packages/enzyme/package.json index 4757656fc..08af8659a 100644 --- a/packages/enzyme/package.json +++ b/packages/enzyme/package.json @@ -51,7 +51,8 @@ "object.entries": "^1.0.4", "object.values": "^1.0.4", "raf": "^3.4.0", - "rst-selector-parser": "^2.2.3" + "rst-selector-parser": "^2.2.3", + "string.prototype.trim": "^1.1.2" }, "devDependencies": { "babel-cli": "^6.26.0", diff --git a/packages/enzyme/src/Utils.js b/packages/enzyme/src/Utils.js index 7da31c141..63448a777 100644 --- a/packages/enzyme/src/Utils.js +++ b/packages/enzyme/src/Utils.js @@ -5,6 +5,7 @@ import entries from 'object.entries'; import functionName from 'function.prototype.name'; import has from 'has'; import flat from 'array.prototype.flat'; +import trim from 'string.prototype.trim'; import { get } from './configuration'; import { childrenOfNode } from './RSTTraversal'; @@ -134,8 +135,8 @@ function internalNodeCompare(a, b, lenComp, isLoose) { const childCompare = isLoose ? childrenMatch : childrenEqual; if (leftHasChildren || rightHasChildren) { if (!childCompare( - childrenToSimplifiedArray(left.children), - childrenToSimplifiedArray(right.children), + childrenToSimplifiedArray(left.children, isLoose), + childrenToSimplifiedArray(right.children, isLoose), lenComp, )) { return false; @@ -184,7 +185,7 @@ function childrenToArray(children) { return result; } -export function childrenToSimplifiedArray(nodeChildren) { +export function childrenToSimplifiedArray(nodeChildren, isLoose = false) { const childrenArray = childrenToArray(nodeChildren); const simplifiedArray = []; @@ -202,6 +203,10 @@ export function childrenToSimplifiedArray(nodeChildren) { } } + if (isLoose) { + return simplifiedArray.map(x => (typeof x === 'string' ? trim(x) : x)); + } + return simplifiedArray; }