Skip to content

Commit

Permalink
Mention actual prop type in element type checker (#7319)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev authored and gaearon committed Jul 20, 2016
1 parent 5e3959e commit 8bcea53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 24 additions & 6 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, [<div />, <div />], message);
typeCheckFail(PropTypes.element, 123, message);
typeCheckFail(PropTypes.element, 'foo', message);
typeCheckFail(PropTypes.element, false, message);
typeCheckFail(
PropTypes.element,
[<div />, <div />],
'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', () => {
Expand Down

0 comments on commit 8bcea53

Please sign in to comment.