Skip to content

Commit

Permalink
Merge pull request #1478 from KordonDev/master
Browse files Browse the repository at this point in the history
[Fix] `shallow`: `.at()`: return an empty wrapper when an index does not exist

Fixes #1475.
  • Loading branch information
ljharb authored Jul 7, 2018
2 parents 18de4ed + 9739619 commit 8ec2106
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
13 changes: 13 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3140,6 +3140,19 @@ describeWithDOM('mount', () => {
expect(wrapper.find('.bar').at(2).hasClass('bux')).to.equal(true);
expect(wrapper.find('.bar').at(3).hasClass('baz')).to.equal(true);
});

it('`.at()` does not affect the results of `.exists()`', () => {
const wrapper = mount((
<div>
<div className="foo" />
</div>
));
expect(wrapper.find('.bar').exists()).to.equal(false);
expect(wrapper.find('.bar').at(0).exists()).to.equal(false);

expect(wrapper.find('.foo').exists()).to.equal(true);
expect(wrapper.find('.foo').at(0).exists()).to.equal(true);
});
});

describe('.get(index)', () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,19 @@ describe('shallow', () => {
expect(wrapper.find('.bar').at(2).hasClass('bux')).to.equal(true);
expect(wrapper.find('.bar').at(3).hasClass('baz')).to.equal(true);
});

it('`.at()` does not affect the results of `.exists()`', () => {
const wrapper = shallow((
<div>
<div className="foo" />
</div>
));
expect(wrapper.find('.bar').exists()).to.equal(false);
expect(wrapper.find('.bar').at(0).exists()).to.equal(false);

expect(wrapper.find('.foo').exists()).to.equal(true);
expect(wrapper.find('.foo').at(0).exists()).to.equal(true);
});
});

describe('.get(index)', () => {
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 @@ -919,7 +919,11 @@ class ReactWrapper {
* @returns {ReactWrapper}
*/
at(index) {
return this.wrap(this.getNodesInternal()[index]);
const nodes = this.getNodesInternal();
if (index < nodes.length) {
return this.wrap(nodes[index]);
}
return this.wrap([]);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ class ShallowWrapper {
* @returns {ReactElement}
*/
get(index) {
return getAdapter(this[OPTIONS]).nodeToElement(this.getNodesInternal()[index]);
return this.getElements()[index];
}

/**
Expand All @@ -1126,7 +1126,11 @@ class ShallowWrapper {
* @returns {ShallowWrapper}
*/
at(index) {
return this.wrap(this.getNodesInternal()[index]);
const nodes = this.getNodesInternal();
if (index < nodes.length) {
return this.wrap(nodes[index]);
}
return this.wrap([]);
}

/**
Expand Down

0 comments on commit 8ec2106

Please sign in to comment.