From b4c8170b4099214df0d07f2af424f324b7ad3d47 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Tue, 18 Dec 2018 14:43:26 +0100 Subject: [PATCH] `oneOf`: improve warning when multiple arguments are supplied 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. --- __tests__/PropTypesDevelopmentReact15.js | 16 +++++++++++++++- __tests__/PropTypesDevelopmentStandalone-test.js | 16 +++++++++++++++- factoryWithTypeCheckers.js | 11 ++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/__tests__/PropTypesDevelopmentReact15.js b/__tests__/PropTypesDevelopmentReact15.js index 7db92d7..9f92f48 100644 --- a/__tests__/PropTypesDevelopmentReact15.js +++ b/__tests__/PropTypesDevelopmentReact15.js @@ -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'); diff --git a/__tests__/PropTypesDevelopmentStandalone-test.js b/__tests__/PropTypesDevelopmentStandalone-test.js index bdb6325..b2ffaa1 100644 --- a/__tests__/PropTypesDevelopmentStandalone-test.js +++ b/__tests__/PropTypesDevelopmentStandalone-test.js @@ -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'); diff --git a/factoryWithTypeCheckers.js b/factoryWithTypeCheckers.js index e1598d3..1b73c3b 100644 --- a/factoryWithTypeCheckers.js +++ b/factoryWithTypeCheckers.js @@ -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; }