Skip to content

Commit 701b481

Browse files
committed
fix #1488 by returning null for missing keys
1 parent f080dc9 commit 701b481

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,6 +3153,15 @@ describeWithDOM('mount', () => {
31533153
expect(wrapper.at(1).key()).to.equal('bar');
31543154
expect(wrapper.at(2).key()).to.equal('');
31553155
});
3156+
3157+
it('should return null when no key is specified', () => {
3158+
const wrapper = mount((
3159+
<ul>
3160+
<li>foo</li>
3161+
</ul>
3162+
)).find('li');
3163+
expect(wrapper.key()).to.equal(null);
3164+
});
31563165
});
31573166

31583167
describe('.matchesElement(node)', () => {

packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4014,6 +4014,15 @@ describe('shallow', () => {
40144014
expect(wrapper.at(1).key()).to.equal('bar');
40154015
expect(wrapper.at(2).key()).to.equal('');
40164016
});
4017+
4018+
it('should return null when no key is specified', () => {
4019+
const wrapper = shallow((
4020+
<ul>
4021+
<li>foo</li>
4022+
</ul>
4023+
)).find('li');
4024+
expect(wrapper.key()).to.equal(null);
4025+
});
40174026
});
40184027

40194028
describe('.matchesElement(node)', () => {

packages/enzyme/src/ReactWrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ class ReactWrapper {
703703
* @returns {String}
704704
*/
705705
key() {
706-
return this.single('key', n => n.key);
706+
return this.single('key', n => (n.key === undefined ? null : n.key));
707707
}
708708

709709
/**

packages/enzyme/src/ShallowWrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ class ShallowWrapper {
845845
* @returns {String}
846846
*/
847847
key() {
848-
return this.single('key', n => n.key);
848+
return this.single('key', n => (n.key === undefined ? null : n.key));
849849
}
850850

851851
/**

0 commit comments

Comments
 (0)