From 2106bf5085c8143d7e7d48c2ca9a167bc1a6c0ce Mon Sep 17 00:00:00 2001 From: Alex Zherdev Date: Wed, 20 Jul 2016 21:16:55 +0300 Subject: [PATCH] Mention actual prop type in element type checker (#7319) (cherry picked from commit 8bcea5310e4ac21c7c4f0552c01a270c4746b08b) --- .../classic/types/ReactPropTypes.js | 8 +++-- .../types/__tests__/ReactPropTypes-test.js | 30 +++++++++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/isomorphic/classic/types/ReactPropTypes.js b/src/isomorphic/classic/types/ReactPropTypes.js index ec060fbde7..eab133b350 100644 --- a/src/isomorphic/classic/types/ReactPropTypes.js +++ b/src/isomorphic/classic/types/ReactPropTypes.js @@ -236,11 +236,13 @@ function createArrayOfTypeChecker(typeChecker) { function createElementTypeChecker() { function validate(props, propName, componentName, location, propFullName) { - if (!ReactElement.isValidElement(props[propName])) { + var propValue = props[propName]; + if (!ReactElement.isValidElement(propValue)) { var locationName = ReactPropTypeLocationNames[location]; + var propType = getPropType(propValue); return new Error( - `Invalid ${locationName} \`${propFullName}\` supplied to ` + - `\`${componentName}\`, expected a single ReactElement.` + `Invalid ${locationName} \`${propFullName}\` of type ` + + `\`${propType}\` supplied to \`${componentName}\`, expected a single ReactElement.` ); } return null; diff --git a/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js b/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js index b11179a11f..bee48f5407 100644 --- a/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js +++ b/src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js @@ -356,12 +356,30 @@ describe('ReactPropTypes', function() { }); it('should not support multiple components or scalar values', () => { - var message = 'Invalid prop `testProp` supplied to `testComponent`, ' + - 'expected a single ReactElement.'; - typeCheckFail(PropTypes.element, [
,
], message); - typeCheckFail(PropTypes.element, 123, message); - typeCheckFail(PropTypes.element, 'foo', message); - typeCheckFail(PropTypes.element, false, message); + typeCheckFail( + PropTypes.element, + [
,
], + 'Invalid prop `testProp` of type `array` supplied to `testComponent`, ' + + 'expected a single ReactElement.' + ); + typeCheckFail( + PropTypes.element, + 123, + 'Invalid prop `testProp` of type `number` supplied to `testComponent`, ' + + 'expected a single ReactElement.' + ); + typeCheckFail( + PropTypes.element, + 'foo', + 'Invalid prop `testProp` of type `string` supplied to `testComponent`, ' + + 'expected a single ReactElement.' + ); + typeCheckFail( + PropTypes.element, + false, + 'Invalid prop `testProp` of type `boolean` supplied to `testComponent`, ' + + 'expected a single ReactElement.' + ); }); it('should be able to define a single child as label', () => {