Skip to content

Commit

Permalink
Merge pull request #49 from rylanc/add-indexed-iterable
Browse files Browse the repository at this point in the history
Add validators for Iterable.Indexed and Iterable.Keyed
  • Loading branch information
HurricaneJames authored Mar 21, 2017
2 parents 44f9786 + cfda937 commit 5ff6faa
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ImmutablePropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ if (process.env.NODE_ENV !== 'production') {
};
}

ImmutablePropTypes.iterable.indexed = createIterableSubclassTypeChecker('Indexed', Immutable.Iterable.isIndexed);
ImmutablePropTypes.iterable.keyed = createIterableSubclassTypeChecker('Keyed', Immutable.Iterable.isKeyed);

function getPropType(propValue) {
var propType = typeof propValue;
if (Array.isArray(propValue)) {
Expand Down Expand Up @@ -124,6 +127,12 @@ function createImmutableTypeChecker(immutableClassName, immutableClassTypeValida
return createChainableTypeChecker(validate);
}

function createIterableSubclassTypeChecker(subclassName, validator) {
return createImmutableTypeChecker(`Iterable.${subclassName}`, (propValue) =>
Immutable.Iterable.isIterable(propValue) && validator(propValue)
);
}

function createIterableTypeChecker(typeChecker, immutableClassName, immutableClassTypeValidator) {

function validate(props, propName, componentName, location, propFullName, ...rest) {
Expand Down
174 changes: 174 additions & 0 deletions src/__tests__/ImmutablePropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ describe('ImmutablePropTypes', function() {
typeCheckPass(PropTypes.iterable, Immutable.OrderedSet());
typeCheckPass(PropTypes.iterable, Immutable.Stack());
typeCheckPass(PropTypes.iterable, Immutable.Seq());
typeCheckPass(PropTypes.iterable, Immutable.Seq());
typeCheckPass(PropTypes.iterable.indexed, Immutable.Iterable.Indexed());
typeCheckPass(PropTypes.iterable.indexed, Immutable.List());
typeCheckPass(PropTypes.iterable.indexed, Immutable.Stack());
typeCheckPass(PropTypes.iterable.indexed, Immutable.Range());
typeCheckPass(PropTypes.iterable.indexed, Immutable.Repeat());
typeCheckPass(PropTypes.iterable.indexed, Immutable.Seq.Indexed());
typeCheckPass(PropTypes.iterable.keyed, Immutable.Iterable.Keyed());
typeCheckPass(PropTypes.iterable.keyed, Immutable.Map());
typeCheckPass(PropTypes.iterable.keyed, Immutable.OrderedMap());
typeCheckPass(PropTypes.iterable.keyed, new (Immutable.Record({a: 1}))());
typeCheckPass(PropTypes.iterable.keyed, Immutable.Seq.Keyed());
});
it('should warn for invalid lists', function() {
typeCheckFail(
Expand Down Expand Up @@ -204,6 +216,168 @@ describe('ImmutablePropTypes', function() {
'`testComponent`, expected `Record`.'
);
});
it('should warn for invalid iterables', function() {
typeCheckFail(
PropTypes.iterable,
[],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected `Iterable`.'
);
typeCheckFail(
PropTypes.iterable,
{},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected `Iterable`.'
);
typeCheckFail(
PropTypes.iterable,
'',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected `Iterable`.'
);
typeCheckFail(
PropTypes.iterable,
false,
'Invalid prop `testProp` of type `boolean` supplied to ' +
'`testComponent`, expected `Iterable`.'
);
typeCheckFail(
PropTypes.iterable,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `Iterable`.'
);
});
it('should warn for invalid indexed iterables', function() {
typeCheckFail(
PropTypes.iterable.indexed,
[],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
{},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
'',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
false,
'Invalid prop `testProp` of type `boolean` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
Immutable.Map(),
'Invalid prop `testProp` of type `Immutable.Map` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
Immutable.Set(),
'Invalid prop `testProp` of type `Immutable.Set` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
typeCheckFail(
PropTypes.iterable.indexed,
new (Immutable.Record({a: 1}))(),
'Invalid prop `testProp` of type `Immutable.Record` supplied to ' +
'`testComponent`, expected `Iterable.Indexed`.'
);
});
it('should warn for invalid keyed iterables', function() {
typeCheckFail(
PropTypes.iterable.keyed,
[],
'Invalid prop `testProp` of type `array` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
{},
'Invalid prop `testProp` of type `object` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
'',
'Invalid prop `testProp` of type `string` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
false,
'Invalid prop `testProp` of type `boolean` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
0,
'Invalid prop `testProp` of type `number` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
Immutable.List(),
'Invalid prop `testProp` of type `Immutable.List` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
Immutable.Set(),
'Invalid prop `testProp` of type `Immutable.Set` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
Immutable.OrderedSet(),
'Invalid prop `testProp` of type `Immutable.OrderedSet` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
Immutable.Stack(),
'Invalid prop `testProp` of type `Immutable.Stack` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
Immutable.Range(),
'Invalid prop `testProp` of type `Immutable.Range` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
typeCheckFail(
PropTypes.iterable.keyed,
Immutable.Repeat(),
'Invalid prop `testProp` of type `Immutable.Repeat` supplied to ' +
'`testComponent`, expected `Iterable.Keyed`.'
);
});
it('should be implicitly optional and not warn without values', function() {
typeCheckPass(PropTypes.list, null);
typeCheckPass(PropTypes.list, undefined);
Expand Down

0 comments on commit 5ff6faa

Please sign in to comment.