From 0efc948363994f24caea4424daa746a861f702c1 Mon Sep 17 00:00:00 2001 From: bwain Date: Thu, 22 Feb 2018 23:47:05 -0600 Subject: [PATCH] fix #1488 by returning null for missing keys --- packages/enzyme-test-suite/test/ReactWrapper-spec.jsx | 9 +++++++++ packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx | 9 +++++++++ packages/enzyme/src/ReactWrapper.js | 2 +- packages/enzyme/src/ShallowWrapper.js | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx index 44304499d..db5419a17 100644 --- a/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ReactWrapper-spec.jsx @@ -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(( + + )).find('li'); + expect(wrapper.key()).to.equal(null); + }); }); describe('.matchesElement(node)', () => { diff --git a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx index 7d1a6508e..22e14d897 100644 --- a/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx +++ b/packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx @@ -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(( + + )).find('li'); + expect(wrapper.key()).to.equal(null); + }); }); describe('.matchesElement(node)', () => { diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index aac1e5701..d14a3fc0a 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -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)); } /** diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 0d905a26f..2144bc630 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -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)); } /**