Skip to content

Commit

Permalink
oneOf: improve warning when multiple arguments are supplied
Browse files Browse the repository at this point in the history
Adds a different warning message for multiple arguments supplied to oneOf. A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]) and this should help developers identifying the error.
  • Loading branch information
wojtekmaj authored and ljharb committed Dec 18, 2018
1 parent 8e3cc5d commit b4c8170
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
16 changes: 15 additions & 1 deletion __tests__/PropTypesDevelopmentReact15.js
Expand Up @@ -894,11 +894,25 @@ describe('PropTypesDevelopmentReact15', () => {
it('should warn but not error for invalid argument', () => {
spyOn(console, 'error');

PropTypes.oneOf('red');

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOf, expected an array.',
);

typeCheckPass(PropTypes.oneOf('red', 'blue'), 'red');
});

it('should warn but not error for invalid multiple arguments', () => {
spyOn(console, 'error');

PropTypes.oneOf('red', 'blue');

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOf, expected an instance of array.',
'Invalid arguments supplied to oneOf, expected an array, got 2 arguments. '
+ 'A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]).',
);

typeCheckPass(PropTypes.oneOf('red', 'blue'), 'red');
Expand Down
16 changes: 15 additions & 1 deletion __tests__/PropTypesDevelopmentStandalone-test.js
Expand Up @@ -896,11 +896,25 @@ describe('PropTypesDevelopmentStandalone', () => {
it('should warn but not error for invalid argument', () => {
spyOn(console, 'error');

PropTypes.oneOf('red');

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOf, expected an array.',
);

typeCheckPass(PropTypes.oneOf('red'), 'red');
});

it('should warn but not error for invalid multiple arguments', () => {
spyOn(console, 'error');

PropTypes.oneOf('red', 'blue');

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0]).toContain(
'Invalid argument supplied to oneOf, expected an instance of array.',
'Invalid arguments supplied to oneOf, expected an array, got 2 arguments. '
+ 'A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]).',
);

typeCheckPass(PropTypes.oneOf('red', 'blue'), 'red');
Expand Down
11 changes: 10 additions & 1 deletion factoryWithTypeCheckers.js
Expand Up @@ -290,7 +290,16 @@ module.exports = function(isValidElement, throwOnDirectAccess) {

function createEnumTypeChecker(expectedValues) {
if (!Array.isArray(expectedValues)) {
process.env.NODE_ENV !== 'production' ? printWarning('Invalid argument supplied to oneOf, expected an instance of array.') : void 0;
if (process.env.NODE_ENV !== 'production') {
if (arguments.length > 1) {
printWarning(
`Invalid arguments supplied to oneOf, expected an array, got ${arguments.length} arguments. ` +
`A common mistake is to write oneOf(x, y, z) instead of oneOf([x, y, z]).`
);
} else {
printWarning('Invalid argument supplied to oneOf, expected an array.');
}
}
return emptyFunctionThatReturnsNull;
}

Expand Down

0 comments on commit b4c8170

Please sign in to comment.