Skip to content

Commit

Permalink
[fix] mount/shallow: do not dedupe in flatMap
Browse files Browse the repository at this point in the history
Fixes #1275
  • Loading branch information
ljharb committed Jul 6, 2018
1 parent f7fee9c commit 7234174
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
18 changes: 18 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,24 @@ describeWithDOM('mount', () => {
expect(wrapper.children().children().at(2).hasClass('baz')).to.equal(true);
});
});

it('returns duplicates untouched', () => {
class Foo extends React.Component {
render() {
const foo = 'Foo';
return (
<div>
{foo} Bar {foo} Bar {foo}
</div>
)
}
}

const wrapper = mount(<Foo />);
const children = wrapper.children();
const textNodes = children.map(x => x.text());
expect(textNodes).to.eql(['Foo Bar Foo Bar Foo']);
});
});

describe('.childAt(index)', () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,23 @@ describe('shallow', () => {
});
});

it('returns duplicates untouched', () => {
class Foo extends React.Component {
render() {
const foo = 'Foo';
return (
<div>
{foo} Bar {foo} Bar {foo}
</div>
)
}
}

const wrapper = shallow(<Foo />);
const children = wrapper.children();
const textNodes = children.map(x => x.text());
expect(textNodes).to.eql(['Foo', ' Bar ', 'Foo', ' Bar ', 'Foo']);
});
});

describe('.childAt(index)', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import cheerio from 'cheerio';
import flatten from 'lodash/flatten';
import unique from 'lodash/uniq';
import compact from 'lodash/compact';

import {
Expand Down Expand Up @@ -888,8 +887,7 @@ class ReactWrapper {
flatMap(fn) {
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
const flattened = flatten(nodes, true);
const uniques = unique(flattened);
const compacted = compact(uniques);
const compacted = compact(flattened);
return this.wrap(compacted);
}

Expand Down
4 changes: 1 addition & 3 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import flatten from 'lodash/flatten';
import unique from 'lodash/uniq';
import compact from 'lodash/compact';
import cheerio from 'cheerio';

Expand Down Expand Up @@ -1094,8 +1093,7 @@ class ShallowWrapper {
flatMap(fn) {
const nodes = this.getNodesInternal().map((n, i) => fn.call(this, this.wrap(n), i));
const flattened = flatten(nodes, true);
const uniques = unique(flattened);
const compacted = compact(uniques);
const compacted = compact(flattened);
return this.wrap(compacted);
}

Expand Down

0 comments on commit 7234174

Please sign in to comment.