Skip to content

Commit

Permalink
Fix oneOf when used with Symbols
Browse files Browse the repository at this point in the history
- Fixes error due to an attempt to coerce a Symbol to a string
- Improves formatting of the "expected" portion of the generated
  warning, outputting for example `["Symbol(A)","Symbol(B)"]` rather
  than `[null,null]`

Fixes #10
  • Loading branch information
jimf committed Oct 2, 2018
1 parent cfc7f43 commit 5b9854a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions __tests__/PropTypesDevelopmentReact15.js
Expand Up @@ -850,6 +850,12 @@ describe('PropTypesDevelopmentReact15', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
typeCheckFail(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
Expand Down
6 changes: 6 additions & 0 deletions __tests__/PropTypesDevelopmentStandalone-test.js
Expand Up @@ -852,6 +852,12 @@ describe('PropTypesDevelopmentStandalone', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
typeCheckFail(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
Expand Down
6 changes: 6 additions & 0 deletions __tests__/PropTypesProductionReact15-test.js
Expand Up @@ -718,6 +718,12 @@ describe('PropTypesProductionReact15', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
expectNoop(
PropTypes.oneOf([Symbol('red'), Symbol('blue')]),
Symbol('green'),
'Invalid prop `testProp` of value `Symbol(green)` supplied to ' +
'`testComponent`, expected one of ["Symbol(red)","Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
Expand Down
9 changes: 7 additions & 2 deletions factoryWithTypeCheckers.js
Expand Up @@ -302,8 +302,13 @@ module.exports = function(isValidElement, throwOnDirectAccess) {
}
}

var valuesString = JSON.stringify(expectedValues);
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + propValue + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));
var valuesString = JSON.stringify(expectedValues, function replacer(key, value) {
if (getPropType(value) === 'symbol') {
return String(value);
}
return value;
});
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of value `' + String(propValue) + '` ' + ('supplied to `' + componentName + '`, expected one of ' + valuesString + '.'));
}
return createChainableTypeChecker(validate);
}
Expand Down

0 comments on commit 5b9854a

Please sign in to comment.