Skip to content

Commit 7234174

Browse files
committed
[fix] mount/shallow: do not dedupe in flatMap
Fixes #1275
1 parent f7fee9c commit 7234174

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,24 @@ describeWithDOM('mount', () => {
23482348
expect(wrapper.children().children().at(2).hasClass('baz')).to.equal(true);
23492349
});
23502350
});
2351+
2352+
it('returns duplicates untouched', () => {
2353+
class Foo extends React.Component {
2354+
render() {
2355+
const foo = 'Foo';
2356+
return (
2357+
<div>
2358+
{foo} Bar {foo} Bar {foo}
2359+
</div>
2360+
)
2361+
}
2362+
}
2363+
2364+
const wrapper = mount(<Foo />);
2365+
const children = wrapper.children();
2366+
const textNodes = children.map(x => x.text());
2367+
expect(textNodes).to.eql(['Foo Bar Foo Bar Foo']);
2368+
});
23512369
});
23522370

23532371
describe('.childAt(index)', () => {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,23 @@ describe('shallow', () => {
21542154
});
21552155
});
21562156

2157+
it('returns duplicates untouched', () => {
2158+
class Foo extends React.Component {
2159+
render() {
2160+
const foo = 'Foo';
2161+
return (
2162+
<div>
2163+
{foo} Bar {foo} Bar {foo}
2164+
</div>
2165+
)
2166+
}
2167+
}
2168+
2169+
const wrapper = shallow(<Foo />);
2170+
const children = wrapper.children();
2171+
const textNodes = children.map(x => x.text());
2172+
expect(textNodes).to.eql(['Foo', ' Bar ', 'Foo', ' Bar ', 'Foo']);
2173+
});
21572174
});
21582175

21592176
describe('.childAt(index)', () => {

packages/enzyme/src/ReactWrapper.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import cheerio from 'cheerio';
22
import flatten from 'lodash/flatten';
3-
import unique from 'lodash/uniq';
43
import compact from 'lodash/compact';
54

65
import {
@@ -888,8 +887,7 @@ class ReactWrapper {
888887
flatMap(fn) {
889888
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
890889
const flattened = flatten(nodes, true);
891-
const uniques = unique(flattened);
892-
const compacted = compact(uniques);
890+
const compacted = compact(flattened);
893891
return this.wrap(compacted);
894892
}
895893

packages/enzyme/src/ShallowWrapper.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import flatten from 'lodash/flatten';
2-
import unique from 'lodash/uniq';
32
import compact from 'lodash/compact';
43
import cheerio from 'cheerio';
54

@@ -1094,8 +1093,7 @@ class ShallowWrapper {
10941093
flatMap(fn) {
10951094
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
10961095
const flattened = flatten(nodes, true);
1097-
const uniques = unique(flattened);
1098-
const compacted = compact(uniques);
1096+
const compacted = compact(flattened);
10991097
return this.wrap(compacted);
11001098
}
11011099

0 commit comments

Comments
 (0)