Skip to content

Commit 654f7be

Browse files
committed
fix #1488 by returning null for missing keys
1 parent 973f5c0 commit 654f7be

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
@@ -3152,6 +3152,15 @@ describeWithDOM('mount', () => {
31523152
expect(wrapper.at(0).key()).to.equal('foo');
31533153
expect(wrapper.at(1).key()).to.equal('bar');
31543154
});
3155+
3156+
it('should return null when no key is specified', () => {
3157+
const wrapper = mount((
3158+
<ul>
3159+
<li>foo</li>
3160+
</ul>
3161+
)).find('li');
3162+
expect(wrapper.key()).to.equal(null);
3163+
});
31553164
});
31563165

31573166
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
@@ -4013,6 +4013,15 @@ describe('shallow', () => {
40134013
expect(wrapper.at(0).key()).to.equal('foo');
40144014
expect(wrapper.at(1).key()).to.equal('bar');
40154015
});
4016+
4017+
it('should return null when no key is specified', () => {
4018+
const wrapper = shallow((
4019+
<ul>
4020+
<li>foo</li>
4021+
</ul>
4022+
)).find('li');
4023+
expect(wrapper.key()).to.equal(null);
4024+
});
40164025
});
40174026

40184027
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)