mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
Allow null return values for functions to-be-merged.
This commit is contained in:
parent
d9b959884b
commit
c885abbf21
|
|
@ -547,10 +547,14 @@ function mergeObjectsWithNoDuplicateKeys(one, two) {
|
|||
*/
|
||||
function createMergedResultFunction(one, two) {
|
||||
return function mergedResult() {
|
||||
return mergeObjectsWithNoDuplicateKeys(
|
||||
one.apply(this, arguments),
|
||||
two.apply(this, arguments)
|
||||
);
|
||||
var a = one.apply(this, arguments);
|
||||
var b = two.apply(this, arguments);
|
||||
if (a == null) {
|
||||
return b;
|
||||
} else if (b == null) {
|
||||
return a;
|
||||
}
|
||||
return mergeObjectsWithNoDuplicateKeys(a, b);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -477,30 +477,6 @@ describe('ReactCompositeComponent', function() {
|
|||
);
|
||||
});
|
||||
|
||||
it('should throw with bad getInitialState() return values', function() {
|
||||
var Mixin = {
|
||||
getInitialState: function() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
var Component = React.createClass({
|
||||
mixins: [Mixin],
|
||||
getInitialState: function() {
|
||||
return {x: true};
|
||||
},
|
||||
render: function() {
|
||||
return <span />;
|
||||
}
|
||||
});
|
||||
var instance = <Component />;
|
||||
expect(function() {
|
||||
ReactTestUtils.renderIntoDocument(instance);
|
||||
}).toThrow(
|
||||
'Invariant Violation: mergeObjectsWithNoDuplicateKeys(): ' +
|
||||
'Cannot merge non-objects'
|
||||
);
|
||||
});
|
||||
|
||||
it('should work with object getInitialState() return values', function() {
|
||||
var Component = React.createClass({
|
||||
getInitialState: function() {
|
||||
|
|
@ -546,7 +522,77 @@ describe('ReactCompositeComponent', function() {
|
|||
return <span />;
|
||||
}
|
||||
});
|
||||
expect(() => <Component />).not.toThrow();
|
||||
expect(
|
||||
() => ReactTestUtils.renderIntoDocument(<Component />)
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it('should work with a null getInitialState return value and a mixin', () => {
|
||||
var Component;
|
||||
var instance;
|
||||
|
||||
var Mixin = {
|
||||
getInitialState: function() {
|
||||
return {foo: 'bar'};
|
||||
}
|
||||
};
|
||||
Component = React.createClass({
|
||||
mixins: [Mixin],
|
||||
getInitialState: function() {
|
||||
return null;
|
||||
},
|
||||
render: function() {
|
||||
return <span />;
|
||||
}
|
||||
});
|
||||
expect(
|
||||
() => ReactTestUtils.renderIntoDocument(<Component />)
|
||||
).not.toThrow();
|
||||
|
||||
instance = <Component />;
|
||||
ReactTestUtils.renderIntoDocument(instance);
|
||||
expect(instance.state).toEqual({foo: 'bar'});
|
||||
|
||||
// Also the other way round should work
|
||||
var Mixin2 = {
|
||||
getInitialState: function() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Component = React.createClass({
|
||||
mixins: [Mixin2],
|
||||
getInitialState: function() {
|
||||
return {foo: 'bar'};
|
||||
},
|
||||
render: function() {
|
||||
return <span />;
|
||||
}
|
||||
});
|
||||
expect(
|
||||
() => ReactTestUtils.renderIntoDocument(<Component />)
|
||||
).not.toThrow();
|
||||
|
||||
instance = <Component />;
|
||||
ReactTestUtils.renderIntoDocument(instance);
|
||||
expect(instance.state).toEqual({foo: 'bar'});
|
||||
|
||||
// Multiple mixins should be fine too
|
||||
Component = React.createClass({
|
||||
mixins: [Mixin, Mixin2],
|
||||
getInitialState: function() {
|
||||
return {x: true};
|
||||
},
|
||||
render: function() {
|
||||
return <span />;
|
||||
}
|
||||
});
|
||||
expect(
|
||||
() => ReactTestUtils.renderIntoDocument(<Component />)
|
||||
).not.toThrow();
|
||||
|
||||
instance = <Component />;
|
||||
ReactTestUtils.renderIntoDocument(instance);
|
||||
expect(instance.state).toEqual({foo: 'bar', x: true});
|
||||
});
|
||||
|
||||
it('should work with object getInitialState() return values', function() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user