Skip to content

Commit 171e952

Browse files
committedAug 31, 2018
[Fix] shallow/mount: containsMatchingElement: trim whitespace
Fixes #636.
1 parent c4e4b75 commit 171e952

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed
 

‎packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

+22
Original file line numberDiff line numberDiff line change
@@ -5940,6 +5940,28 @@ describeWithDOM('mount', () => {
59405940
expect(wrapper.containsMatchingElement(<div className="c" id={null} />)).to.equal(true);
59415941
expect(wrapper.containsMatchingElement(<div className="c" id={undefined} />)).to.equal(true);
59425942
});
5943+
5944+
it('works with leading and trailing spaces', () => {
5945+
const wrapper = mount((
5946+
<li>
5947+
<a> All Operations </a>
5948+
</li>
5949+
));
5950+
5951+
expect(wrapper.containsMatchingElement(<a> All Operations </a>)).to.equal(true);
5952+
});
5953+
5954+
it('works with leading and trailing newlines', () => {
5955+
const wrapper = mount((
5956+
<li>
5957+
<a>
5958+
All Operations
5959+
</a>
5960+
</li>
5961+
));
5962+
5963+
expect(wrapper.containsMatchingElement(<a> All Operations </a>)).to.equal(true);
5964+
});
59435965
});
59445966

59455967
describe('.containsAllMatchingElements(nodes)', () => {

‎packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

+25-9
Original file line numberDiff line numberDiff line change
@@ -6040,12 +6040,8 @@ describe('shallow', () => {
60406040
<div onClick={spy2} style={{ fontSize: 13, color: 'blue' }}>Goodbye World</div>
60416041
</div>
60426042
));
6043-
expect(wrapper.containsMatchingElement((
6044-
<div>Hello World</div>
6045-
))).to.equal(true);
6046-
expect(wrapper.containsMatchingElement((
6047-
<div>Goodbye World</div>
6048-
))).to.equal(true);
6043+
expect(wrapper.containsMatchingElement(<div>Hello World</div>)).to.equal(true);
6044+
expect(wrapper.containsMatchingElement(<div>Goodbye World</div>)).to.equal(true);
60496045
expect(wrapper.containsMatchingElement((
60506046
<div onClick={spy1} style={{ fontSize: 12, color: 'red' }}>Hello World</div>
60516047
))).to.equal(true);
@@ -6071,9 +6067,7 @@ describe('shallow', () => {
60716067
<div onClick={spy2} style={{ fontSize: 13, color: 'blue' }}>Goodbye World</div>
60726068
</div>
60736069
));
6074-
expect(wrapper.containsMatchingElement((
6075-
<div>Bonjour le monde</div>
6076-
))).to.equal(false);
6070+
expect(wrapper.containsMatchingElement(<div>Bonjour le monde</div>)).to.equal(false);
60776071
expect(wrapper.containsMatchingElement((
60786072
<div onClick={spy2}>Au revoir le monde</div>
60796073
))).to.equal(false);
@@ -6102,6 +6096,28 @@ describe('shallow', () => {
61026096
expect(wrapper.containsMatchingElement(<div className="c" id={null} />)).to.equal(true);
61036097
expect(wrapper.containsMatchingElement(<div className="c" id={undefined} />)).to.equal(true);
61046098
});
6099+
6100+
it('works with leading and trailing spaces', () => {
6101+
const wrapper = shallow((
6102+
<li>
6103+
<a> All Operations </a>
6104+
</li>
6105+
));
6106+
6107+
expect(wrapper.containsMatchingElement(<a> All Operations </a>)).to.equal(true);
6108+
});
6109+
6110+
it('works with leading and trailing newlines', () => {
6111+
const wrapper = shallow((
6112+
<li>
6113+
<a>
6114+
All Operations
6115+
</a>
6116+
</li>
6117+
));
6118+
6119+
expect(wrapper.containsMatchingElement(<a> All Operations </a>)).to.equal(true);
6120+
});
61056121
});
61066122

61076123
describe('.containsAllMatchingElements(nodes)', () => {

‎packages/enzyme/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"object.entries": "^1.0.4",
5252
"object.values": "^1.0.4",
5353
"raf": "^3.4.0",
54-
"rst-selector-parser": "^2.2.3"
54+
"rst-selector-parser": "^2.2.3",
55+
"string.prototype.trim": "^1.1.2"
5556
},
5657
"devDependencies": {
5758
"babel-cli": "^6.26.0",

‎packages/enzyme/src/Utils.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import entries from 'object.entries';
55
import functionName from 'function.prototype.name';
66
import has from 'has';
77
import flat from 'array.prototype.flat';
8+
import trim from 'string.prototype.trim';
89

910
import { get } from './configuration';
1011
import { childrenOfNode } from './RSTTraversal';
@@ -134,8 +135,8 @@ function internalNodeCompare(a, b, lenComp, isLoose) {
134135
const childCompare = isLoose ? childrenMatch : childrenEqual;
135136
if (leftHasChildren || rightHasChildren) {
136137
if (!childCompare(
137-
childrenToSimplifiedArray(left.children),
138-
childrenToSimplifiedArray(right.children),
138+
childrenToSimplifiedArray(left.children, isLoose),
139+
childrenToSimplifiedArray(right.children, isLoose),
139140
lenComp,
140141
)) {
141142
return false;
@@ -184,7 +185,7 @@ function childrenToArray(children) {
184185
return result;
185186
}
186187

187-
export function childrenToSimplifiedArray(nodeChildren) {
188+
export function childrenToSimplifiedArray(nodeChildren, isLoose = false) {
188189
const childrenArray = childrenToArray(nodeChildren);
189190
const simplifiedArray = [];
190191

@@ -202,6 +203,10 @@ export function childrenToSimplifiedArray(nodeChildren) {
202203
}
203204
}
204205

206+
if (isLoose) {
207+
return simplifiedArray.map(x => (typeof x === 'string' ? trim(x) : x));
208+
}
209+
205210
return simplifiedArray;
206211
}
207212

0 commit comments

Comments
 (0)
Please sign in to comment.