From 1b802fbd65cede34e4a1ed36f8f2e780ca9409e5 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 25 May 2016 23:40:59 +0100 Subject: [PATCH] Add a test verifying undefined key and ref are ignored It currently fails in `createElement` because of #6879 which was introduced in #5744. It also fails in `cloneElement` because the code with that bug was extracted and shared in 94d0dc68c89c0292cd430976a7dafb95f8d13cf4. --- .../element/__tests__/ReactElement-test.js | 82 ++++++++++++++----- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/src/isomorphic/classic/element/__tests__/ReactElement-test.js b/src/isomorphic/classic/element/__tests__/ReactElement-test.js index 85165e762e..4700c6b406 100644 --- a/src/isomorphic/classic/element/__tests__/ReactElement-test.js +++ b/src/isomorphic/classic/element/__tests__/ReactElement-test.js @@ -170,23 +170,26 @@ describe('ReactElement', function() { expect(element.props).toEqual(expectation); }); - it('should not extract key and ref getters from the config when creating an element', function() { + it('extracts null key and ref values when creating an element', function() { + var element = React.createFactory(ComponentClass)({ + key: null, + ref: null, + foo: '12', + }); + expect(element.type).toBe(ComponentClass); + expect(element.key).toBe('null'); + expect(element.ref).toBe(null); + var expectation = {foo: '12'}; + Object.freeze(expectation); + expect(element.props).toEqual(expectation); + }); + + it('ignores undefined key and ref when creating an element', function() { var props = { foo: '56', + key: undefined, + ref: undefined, }; - - Object.defineProperty(props, 'key', { - get: function() { - return '12'; - }, - }); - - Object.defineProperty(props, 'ref', { - get: function() { - return '34'; - }, - }); - var element = React.createFactory(ComponentClass)(props); expect(element.type).toBe(ComponentClass); expect(element.key).toBe(null); @@ -196,29 +199,48 @@ describe('ReactElement', function() { expect(element.props).toEqual(expectation); }); - it('should not extract key and ref getters from the config when cloning an element', function() { + it('ignores key and ref getters when creating an element', function() { + var props = { + foo: '56', + }; + Object.defineProperty(props, 'key', { + get: function() { + return '12'; + }, + }); + Object.defineProperty(props, 'ref', { + get: function() { + return '34'; + }, + }); + var element = React.createFactory(ComponentClass)(props); + expect(element.type).toBe(ComponentClass); + expect(element.key).toBe(null); + expect(element.ref).toBe(null); + var expectation = {foo: '56'}; + Object.freeze(expectation); + expect(element.props).toEqual(expectation); + }); + + it('ignores key and ref getters when cloning an element', function() { var element = React.createFactory(ComponentClass)({ key: '12', ref: '34', foo: '56', }); - var props = { foo: 'ef', }; - Object.defineProperty(props, 'key', { get: function() { return 'ab'; }, }); - Object.defineProperty(props, 'ref', { get: function() { return 'cd'; }, }); - var clone = React.cloneElement(element, props); expect(clone.type).toBe(ComponentClass); expect(clone.key).toBe('12'); @@ -228,19 +250,37 @@ describe('ReactElement', function() { expect(clone.props).toEqual(expectation); }); - it('should allow null key and ref values when cloning an element', function() { + it('ignores undefined key and ref values when cloning an element', function() { var element = React.createFactory(ComponentClass)({ key: '12', ref: '34', foo: '56', }); + var props = { + key: undefined, + ref: undefined, + foo: 'ef', + }; + var clone = React.cloneElement(element, props); + expect(clone.type).toBe(ComponentClass); + expect(clone.key).toBe('12'); + expect(clone.ref).toBe('34'); + var expectation = {foo: 'ef'}; + Object.freeze(expectation); + expect(clone.props).toEqual(expectation); + }); + it('extracts null key and ref values when cloning an element', function() { + var element = React.createFactory(ComponentClass)({ + key: '12', + ref: '34', + foo: '56', + }); var props = { key: null, ref: null, foo: 'ef', }; - var clone = React.cloneElement(element, props); expect(clone.type).toBe(ComponentClass); expect(clone.key).toBe('null');