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.
Dan Abramov committed
May 25, 2016 at 23:40 UTC
1b802fbd65cede34e4a1ed36f8f2e780ca9409e5
1 file changed
+50
-10
src/isomorphic/classic/element/__tests__/ReactElement-test.js
+50
-10
@@ -170,23 +170,49 @@ describe('ReactElement', function() {
170
expect(element.props).toEqual(expectation);
171
});
172
173
- it('should not extract key and ref getters from the config when creating an element', function() {
173
+ it('extracts null key and ref values when creating an element', function() {
174
+ var element = React.createFactory(ComponentClass)({
175
+ key: null,
176
+ ref: null,
177
+ foo: '12',
178
+ });
179
+ expect(element.type).toBe(ComponentClass);
180
+ expect(element.key).toBe('null');
181
+ expect(element.ref).toBe(null);
182
+ var expectation = {foo: '12'};
183
+ Object.freeze(expectation);
184
+ expect(element.props).toEqual(expectation);
185
+ });
186
+
187
+ it('ignores undefined key and ref when creating an element', function() {
188
var props = {
189
foo: '56',
190
+ key: undefined,
191
+ ref: undefined,
192
};
193
+ var element = React.createFactory(ComponentClass)(props);
194
+ expect(element.type).toBe(ComponentClass);
195
+ expect(element.key).toBe(null);
196
+ expect(element.ref).toBe(null);
197
+ var expectation = {foo: '56'};
198
+ Object.freeze(expectation);
199
+ expect(element.props).toEqual(expectation);
200
+ });
201
202
+ it('ignores key and ref getters when creating an element', function() {
203
+ var props = {
204
+ foo: '56',
205
+ };
206
Object.defineProperty(props, 'key', {
207
get: function() {
208
return '12';
209
},
210
});
183
-
211
Object.defineProperty(props, 'ref', {
212
get: function() {
213
return '34';
214
},
215
});
189
-
216
var element = React.createFactory(ComponentClass)(props);
217
expect(element.type).toBe(ComponentClass);
218
expect(element.key).toBe(null);
@@ -196,29 +222,25 @@ describe('ReactElement', function() {
222
expect(element.props).toEqual(expectation);
223
});
224
199
- it('should not extract key and ref getters from the config when cloning an element', function() {
225
+ it('ignores key and ref getters when cloning an element', function() {
226
var element = React.createFactory(ComponentClass)({
227
key: '12',
228
ref: '34',
229
foo: '56',
230
});
205
-
231
var props = {
232
foo: 'ef',
233
};
209
-
234
Object.defineProperty(props, 'key', {
235
get: function() {
236
return 'ab';
237
},
238
});
215
-
239
Object.defineProperty(props, 'ref', {
240
get: function() {
241
return 'cd';
242
},
243
});
221
-
244
var clone = React.cloneElement(element, props);
245
expect(clone.type).toBe(ComponentClass);
246
expect(clone.key).toBe('12');
@@ -228,19 +250,37 @@ describe('ReactElement', function() {
250
expect(clone.props).toEqual(expectation);
251
});
252
231
- it('should allow null key and ref values when cloning an element', function() {
253
+ it('ignores undefined key and ref values when cloning an element', function() {
254
var element = React.createFactory(ComponentClass)({
255
key: '12',
256
ref: '34',
257
foo: '56',
258
});
259
+ var props = {
260
+ key: undefined,
261
+ ref: undefined,
262
+ foo: 'ef',
263
+ };
264
+ var clone = React.cloneElement(element, props);
265
+ expect(clone.type).toBe(ComponentClass);
266
+ expect(clone.key).toBe('12');
267
+ expect(clone.ref).toBe('34');
268
+ var expectation = {foo: 'ef'};
269
+ Object.freeze(expectation);
270
+ expect(clone.props).toEqual(expectation);
271
+ });
272
273
+ it('extracts null key and ref values when cloning an element', function() {
274
+ var element = React.createFactory(ComponentClass)({
275
+ key: '12',
276
+ ref: '34',
277
+ foo: '56',
278
+ });
279
var props = {
280
key: null,
281
ref: null,
282
foo: 'ef',
283
};
243
-
284
var clone = React.cloneElement(element, props);
285
expect(clone.type).toBe(ComponentClass);
286
expect(clone.key).toBe('null');