Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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