Skip to content

Commit

Permalink
fix #1488 by returning null for missing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwain authored and ljharb committed Feb 23, 2018
1 parent f080dc9 commit 0efc948
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,15 @@ describeWithDOM('mount', () => {
expect(wrapper.at(1).key()).to.equal('bar');
expect(wrapper.at(2).key()).to.equal('');
});

it('should return null when no key is specified', () => {
const wrapper = mount((
<ul>
<li>foo</li>
</ul>
)).find('li');
expect(wrapper.key()).to.equal(null);
});
});

describe('.matchesElement(node)', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4014,6 +4014,15 @@ describe('shallow', () => {
expect(wrapper.at(1).key()).to.equal('bar');
expect(wrapper.at(2).key()).to.equal('');
});

it('should return null when no key is specified', () => {
const wrapper = shallow((
<ul>
<li>foo</li>
</ul>
)).find('li');
expect(wrapper.key()).to.equal(null);
});
});

describe('.matchesElement(node)', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ class ReactWrapper {
* @returns {String}
*/
key() {
return this.single('key', n => n.key);
return this.single('key', n => (n.key === undefined ? null : n.key));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ class ShallowWrapper {
* @returns {String}
*/
key() {
return this.single('key', n => n.key);
return this.single('key', n => (n.key === undefined ? null : n.key));
}

/**
Expand Down

0 comments on commit 0efc948

Please sign in to comment.