Use specific method names in error messages

This commit is contained in:
Dan Abramov
2016-03-21 21:40:17 +00:00
parent 552f00b536
commit ebe5128489
4 changed files with 86 additions and 29 deletions
+1 -1
View File
@@ -710,7 +710,7 @@ var ReactClassMixin = {
replaceState: function(newState, callback) {
this.updater.enqueueReplaceState(this, newState);
if (callback) {
this.updater.enqueueCallback(this, callback);
this.updater.enqueueCallback(this, callback, 'replaceState');
}
},
@@ -76,7 +76,7 @@ ReactComponent.prototype.setState = function(partialState, callback) {
}
this.updater.enqueueSetState(this, partialState);
if (callback) {
this.updater.enqueueCallback(this, callback);
this.updater.enqueueCallback(this, callback, 'setState');
}
};
@@ -97,7 +97,7 @@ ReactComponent.prototype.setState = function(partialState, callback) {
ReactComponent.prototype.forceUpdate = function(callback) {
this.updater.enqueueForceUpdate(this);
if (callback) {
this.updater.enqueueCallback(this, callback);
this.updater.enqueueCallback(this, callback, 'forceUpdate');
}
};
@@ -116,14 +116,15 @@ var ReactUpdateQueue = {
*
* @param {ReactClass} publicInstance The instance to use as `this` context.
* @param {?function} callback Called after state is updated.
* @param {string} callerName Name of the calling function in the public API.
* @internal
*/
enqueueCallback: function(publicInstance, callback) {
enqueueCallback: function(publicInstance, callback, callerName) {
invariant(
typeof callback === 'function',
'enqueueCallback(...): You called `setState`, `replaceState`, or ' +
'`forceUpdate` with the last argument of type %s. When specified, ' +
'their last `callback` argument is expected to be a function.',
'enqueueCallback(...): You called `%s` with the last argument of type ' +
'%s. When specified, its last `callback` argument must be a function.',
callerName,
formatUnexpectedArgument(callback)
);
var internalInstance = getInternalInstanceReadyForUpdate(publicInstance);
@@ -938,7 +938,7 @@ describe('ReactUpdates', function() {
}
});
it('throws when the update callback is not a function', function() {
it('throws in setState if the update callback is not a function', function() {
function Foo() {
this.a = 1;
this.b = 2;
@@ -953,28 +953,84 @@ describe('ReactUpdates', function() {
});
var component = ReactTestUtils.renderIntoDocument(<A />);
var stringMessage =
'enqueueCallback(...): You called `setState`, `replaceState`, or '+
'`forceUpdate` with the last argument of type string. When specified, ' +
'their last `callback` argument is expected to be a function.';
expect(() => component.setState({}, 'no')).toThrow(stringMessage);
expect(() => component.replaceState({}, 'no')).toThrow(stringMessage);
expect(() => component.forceUpdate('no')).toThrow(stringMessage);
expect(() => component.setState({}, 'no')).toThrow(
'enqueueCallback(...): You called `setState` with the last argument of ' +
'type string. When specified, its last `callback` argument must be a ' +
'function.'
);
expect(() => component.setState({}, {})).toThrow(
'enqueueCallback(...): You called `setState` with the last argument of ' +
'type Object. When specified, its last `callback` argument must be a ' +
'function.'
);
expect(() => component.setState({}, new Foo())).toThrow(
'enqueueCallback(...): You called `setState` with the last argument of ' +
'type Foo (keys: a, b). When specified, its last `callback` argument ' +
'must be a function.'
);
});
var objectMessage =
'enqueueCallback(...): You called `setState`, `replaceState`, or '+
'`forceUpdate` with the last argument of type Object. When specified, ' +
'their last `callback` argument is expected to be a function.';
expect(() => component.setState({}, {})).toThrow(objectMessage);
expect(() => component.replaceState({}, {})).toThrow(objectMessage);
expect(() => component.forceUpdate({})).toThrow(objectMessage);
it('throws in replaceState if the update callback is not a function', function() {
function Foo() {
this.a = 1;
this.b = 2;
}
var A = React.createClass({
getInitialState: function() {
return {};
},
render: function() {
return <div />;
},
});
var component = ReactTestUtils.renderIntoDocument(<A />);
var fooMessage =
'enqueueCallback(...): You called `setState`, `replaceState`, or '+
'`forceUpdate` with the last argument of type Foo (keys: a, b). When ' +
'specified, their last `callback` argument is expected to be a function.';
expect(() => component.setState({}, new Foo())).toThrow(fooMessage);
expect(() => component.replaceState({}, new Foo())).toThrow(fooMessage);
expect(() => component.forceUpdate(new Foo())).toThrow(fooMessage);
expect(() => component.replaceState({}, 'no')).toThrow(
'enqueueCallback(...): You called `replaceState` with the last ' +
'argument of type string. When specified, its last `callback` argument ' +
'must be a function.'
);
expect(() => component.replaceState({}, {})).toThrow(
'enqueueCallback(...): You called `replaceState` with the last ' +
'argument of type Object. When specified, its last `callback` argument ' +
'must be a function.'
);
expect(() => component.replaceState({}, new Foo())).toThrow(
'enqueueCallback(...): You called `replaceState` with the last ' +
'argument of type Foo (keys: a, b). When specified, its last ' +
'`callback` argument must be a function.'
);
});
it('throws in forceUpdate if the update callback is not a function', function() {
function Foo() {
this.a = 1;
this.b = 2;
}
var A = React.createClass({
getInitialState: function() {
return {};
},
render: function() {
return <div />;
},
});
var component = ReactTestUtils.renderIntoDocument(<A />);
expect(() => component.forceUpdate('no')).toThrow(
'enqueueCallback(...): You called `forceUpdate` with the last ' +
'argument of type string. When specified, its last `callback` argument ' +
'must be a function.'
);
expect(() => component.forceUpdate({})).toThrow(
'enqueueCallback(...): You called `forceUpdate` with the last ' +
'argument of type Object. When specified, its last `callback` argument ' +
'must be a function.'
);
expect(() => component.forceUpdate(new Foo())).toThrow(
'enqueueCallback(...): You called `forceUpdate` with the last ' +
'argument of type Foo (keys: a, b). When specified, its last ' +
'`callback` argument must be a function.'
);
});
});