Skip to content

Commit

Permalink
[Fix] shallow: .closest(): ensure an empty wrapper is returned fo…
Browse files Browse the repository at this point in the history
…r no match

 - Also, refactor `closest` for `mount`

Fixes #193
  • Loading branch information
ljharb committed Jul 6, 2018
1 parent 1b4a9e0 commit ce1e113
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,21 @@ describeWithDOM('mount', () => {

expect(wrapper.find('.bux').closest('.baz').hasClass('bux')).to.equal(true);
});

it('should not find a nonexistent match', () => {
const wrapper = mount((
<div className="foo">
<div className="bar" />
</div>
));

expect(wrapper.find('.fooooo')).to.have.lengthOf(0);

const bar = wrapper.find('.bar');
expect(bar).to.have.lengthOf(1);

expect(bar.closest('.fooooo')).to.have.lengthOf(0);
});
});

describe('.hasClass(className)', () => {
Expand Down
17 changes: 16 additions & 1 deletion packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2297,8 +2297,8 @@ describe('shallow', () => {
));

const closestFoo = wrapper.find('.bar').closest('.foo');
expect(closestFoo.hasClass('baz')).to.equal(true);
expect(closestFoo).to.have.lengthOf(1);
expect(closestFoo.hasClass('baz')).to.equal(true);
});

it('should only ever return a wrapper of a single node', () => {
Expand Down Expand Up @@ -2328,6 +2328,21 @@ describe('shallow', () => {

expect(wrapper.find('.bux').closest('.baz').hasClass('bux')).to.equal(true);
});

it('should not find a nonexistent match', () => {
const wrapper = shallow((
<div className="foo">
<div className="bar" />
</div>
));

expect(wrapper.find('.fooooo')).to.have.lengthOf(0);

const bar = wrapper.find('.bar');
expect(bar).to.have.lengthOf(1);

expect(bar.closest('.fooooo')).to.have.lengthOf(0);
});
});

describe('.hasClass(className)', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,11 @@ class ReactWrapper {
* @returns {ReactWrapper}
*/
closest(selector) {
return this.is(selector) ? this : this.parents().filter(selector).first();
if (this.is(selector)) {
return this;
}
const matchingAncestors = this.parents().filter(selector);
return matchingAncestors.length > 0 ? matchingAncestors.first() : this.findWhere(() => false);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,11 @@ class ShallowWrapper {
* @returns {ShallowWrapper}
*/
closest(selector) {
return this.is(selector) ? this : this.parents().filter(selector).first();
if (this.is(selector)) {
return this;
}
const matchingAncestors = this.parents().filter(selector);
return matchingAncestors.length > 0 ? matchingAncestors.first() : this.findWhere(() => false);
}

/**
Expand Down

0 comments on commit ce1e113

Please sign in to comment.