warn when shouldComponentUpdate() returns undefined

This commit is contained in:
Daniel Schonfeld committed 2014-05-13 22:44:00 -04:00
1 parent b66202eb98
commit 7d91277a50
2 files changed
+48 -3

No files matched your search

+16 -3
View File
@@ -1000,9 +1000,22 @@ var ReactCompositeComponentMixin = {
this._pendingState = null;
try {
if (this._pendingForceUpdate ||
!this.shouldComponentUpdate ||
this.shouldComponentUpdate(nextProps, nextState, nextContext)) {
var shouldUpdate =
this._pendingForceUpdate ||
!this.shouldComponentUpdate ||
this.shouldComponentUpdate(nextProps, nextState, nextContext);
if (__DEV__) {
if (typeof shouldUpdate === "undefined") {
console.warn(
(this.constructor.displayName || 'ReactCompositeComponent') +
'.shouldComponentUpdate(): Returned undefined instead of a ' +
'boolean value. Make sure to return true or false.'
);
}
}
if (shouldUpdate) {
this._pendingForceUpdate = false;
// Will set `this.props`, `this.state` and `this.context`.
this._performComponentUpdate(
@@ -871,6 +871,38 @@ describe('ReactCompositeComponent', function() {
expect(React.isValidClass(TrickFnComponent)).toBe(false);
});
it('should warn when shouldComponentUpdate() returns undefined', function() {
var warn = console.warn;
console.warn = mocks.getMockFunction();
try {
var Component = React.createClass({
getInitialState: function () {
return {bogus: false};
},
shouldComponentUpdate: function() {
return undefined;
},
render: function() {
return <div />;
}
});
var instance = ReactTestUtils.renderIntoDocument(<Component />);
instance.setState({bogus: true});
expect(console.warn.mock.calls.length).toBe(1);
expect(console.warn.mock.calls[0][0]).toBe(
'Component.shouldComponentUpdate(): Returned undefined instead of a ' +
'boolean value. Make sure to return true or false.'
);
} finally {
console.warn = warn;
}
});
it('should warn when mispelling shouldComponentUpdate', function() {
var warn = console.warn;
console.warn = mocks.getMockFunction();