Skip to content

Commit 9739619

Browse files
committed
[Fix] shallow: .at(): return an empty wrapper when an index does not exist
Fixes #1475.
1 parent 3bbd51b commit 9739619

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

packages/enzyme/src/ReactWrapper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,11 @@ class ReactWrapper {
919919
* @returns {ReactWrapper}
920920
*/
921921
at(index) {
922-
return this.wrap(this.getNodesInternal()[index]);
922+
const nodes = this.getNodesInternal();
923+
if (index < nodes.length) {
924+
return this.wrap(nodes[index]);
925+
}
926+
return this.wrap([]);
923927
}
924928

925929
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ class ShallowWrapper {
11161116
* @returns {ReactElement}
11171117
*/
11181118
get(index) {
1119-
return getAdapter(this[OPTIONS]).nodeToElement(this.getNodesInternal()[index]);
1119+
return this.getElements()[index];
11201120
}
11211121

11221122
/**
@@ -1126,7 +1126,11 @@ class ShallowWrapper {
11261126
* @returns {ShallowWrapper}
11271127
*/
11281128
at(index) {
1129-
return this.wrap(this.getNodesInternal()[index]);
1129+
const nodes = this.getNodesInternal();
1130+
if (index < nodes.length) {
1131+
return this.wrap(nodes[index]);
1132+
}
1133+
return this.wrap([]);
11301134
}
11311135

11321136
/**

0 commit comments

Comments
 (0)