Skip to content

Commit

Permalink
fix enzymejs#1488 by returning null for missing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwain committed Feb 23, 2018
1 parent 973f5c0 commit 654f7be
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 @@ -3152,6 +3152,15 @@ describeWithDOM('mount', () => {
expect(wrapper.at(0).key()).to.equal('foo');
expect(wrapper.at(1).key()).to.equal('bar');
});

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 @@ -4013,6 +4013,15 @@ describe('shallow', () => {
expect(wrapper.at(0).key()).to.equal('foo');
expect(wrapper.at(1).key()).to.equal('bar');
});

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 654f7be

Please sign in to comment.