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 94d0dc68c8.
This commit is contained in:
Dan Abramov
2016-05-25 23:40:59 +01:00
parent 15cd66b91b
commit 1b802fbd65
@@ -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');