mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
* Fix for 5468: Validate proptype definitions sooner
Added typeCheckWarn() func and updated the oneOf/oneOfType tests
Added __DEV__ warning for invalid oneOf/OneOfType args
* Suppress redundant error on warn; typeCheckWarn() removed
* Return no-op
* Using emptyFunction module for consistency
* Remove createChainableTypeChecker() call
* Adjust test to assert type check passes when warned
(cherry picked from commit 6cc037bd0d)
This commit is contained in:
committed by
Paul O’Shannessy
parent
d441128bf6
commit
4a0a534357
@@ -16,6 +16,7 @@ var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');
|
||||
|
||||
var emptyFunction = require('emptyFunction');
|
||||
var getIteratorFn = require('getIteratorFn');
|
||||
var warning = require('warning');
|
||||
|
||||
/**
|
||||
* Collection of methods that allow declaration and validation of props that are
|
||||
@@ -226,11 +227,8 @@ function createInstanceTypeChecker(expectedClass) {
|
||||
|
||||
function createEnumTypeChecker(expectedValues) {
|
||||
if (!Array.isArray(expectedValues)) {
|
||||
return createChainableTypeChecker(function() {
|
||||
return new Error(
|
||||
`Invalid argument supplied to oneOf, expected an instance of array.`
|
||||
);
|
||||
});
|
||||
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.');
|
||||
return emptyFunction.thatReturnsNull;
|
||||
}
|
||||
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
@@ -288,11 +286,8 @@ function createObjectOfTypeChecker(typeChecker) {
|
||||
|
||||
function createUnionTypeChecker(arrayOfTypeCheckers) {
|
||||
if (!Array.isArray(arrayOfTypeCheckers)) {
|
||||
return createChainableTypeChecker(function() {
|
||||
return new Error(
|
||||
`Invalid argument supplied to oneOfType, expected an instance of array.`
|
||||
);
|
||||
});
|
||||
warning(false, 'Invalid argument supplied to oneOfType, expected an instance of array.');
|
||||
return emptyFunction.thatReturnsNull;
|
||||
}
|
||||
|
||||
function validate(props, propName, componentName, location, propFullName) {
|
||||
|
||||
@@ -590,12 +590,16 @@ describe('ReactPropTypes', function() {
|
||||
});
|
||||
|
||||
describe('OneOf Types', function() {
|
||||
it('should fail for invalid argument', function() {
|
||||
typeCheckFail(
|
||||
PropTypes.oneOf('red', 'blue'),
|
||||
'red',
|
||||
'Invalid argument supplied to oneOf, expected an instance of array.'
|
||||
);
|
||||
it('should warn but not error for invalid argument', function() {
|
||||
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.');
|
||||
|
||||
typeCheckPass(PropTypes.oneOf('red', 'blue'), 'red');
|
||||
});
|
||||
|
||||
it('should warn for invalid values', function() {
|
||||
@@ -651,12 +655,16 @@ describe('ReactPropTypes', function() {
|
||||
});
|
||||
|
||||
describe('Union Types', function() {
|
||||
it('should fail for invalid argument', function() {
|
||||
typeCheckFail(
|
||||
PropTypes.oneOfType(PropTypes.string, PropTypes.number),
|
||||
'red',
|
||||
'Invalid argument supplied to oneOfType, expected an instance of array.'
|
||||
);
|
||||
it('should warn but not error for invalid argument', function() {
|
||||
spyOn(console, 'error');
|
||||
|
||||
PropTypes.oneOfType(PropTypes.string, PropTypes.number);
|
||||
|
||||
expect(console.error).toHaveBeenCalled();
|
||||
expect(console.error.calls.argsFor(0)[0])
|
||||
.toContain('Invalid argument supplied to oneOfType, expected an instance of array.');
|
||||
|
||||
typeCheckPass(PropTypes.oneOf(PropTypes.string, PropTypes.number), []);
|
||||
});
|
||||
|
||||
it('should warn if none of the types are valid', function() {
|
||||
|
||||
Reference in New Issue
Block a user