Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Warn when multiple arguments were supplied to oneOf #244

Merged
merged 1 commit into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion __tests__/PropTypesDevelopmentReact15.js
Original file line number Diff line number Diff line change
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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it better to warn here, and not throw an error? Wouldn’t that surface it most rapidly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree - it's my first contribution to prop-types so I'm being cautious here. I didn't want the output of neither oneOf('red') nor oneOf('red', 'blue') to change, as this may be considered breaking by some. So I just "enhanced" an error message in one of the cases.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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