Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention actual prop type in element type checker #7319

Merged
merged 1 commit into from
Jul 20, 2016
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
Mention actual prop type in element type checker
  • Loading branch information
Alex Zherdev committed Jul 20, 2016
commit ec50b09e0c3e4b879dc64e7dcfae92cc87483be8
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