import { expect } from 'chai';
import { node, number, string } from 'prop-types';
import React from 'react';
import { childrenOf, elementType, or } from '..';
import callValidator from './_callValidator';
function SFC() {}
class Component extends React.Component {} // eslint-disable-line react/prefer-stateless-function
describe('childrenOf', () => {
function assertPasses(validator, element, propName, componentName) {
expect(callValidator(validator, element, propName, componentName)).to.equal(null);
}
function assertFails(validator, element, propName, componentName) {
expect(callValidator(validator, element, propName, componentName)).to.be.instanceOf(Error);
}
it('fails on a non-children prop', () => {
const validator = childrenOf(node);
assertFails(
validator,
(
),
'foo',
'non-children optional',
);
assertFails(
validator.isRequired,
(),
'foo',
'non-children optional',
);
});
describe('with no children', () => {
it('passes when optional', () => {
assertPasses(
childrenOf(node),
(),
'children',
'optional empty',
);
assertPasses(
childrenOf(node),
({[]}
),
'children',
'optional empty array',
);
});
it('fails when required', () => assertFails(
childrenOf(node).isRequired,
(),
'children',
'optional empty',
));
});
describe('with non-element children', () => {
it('passes with multiple numbers', () => assertPasses(
childrenOf(number),
(
{1}
{2}
{3}
),
'children',
'numbers',
));
it('passes with multiple numbers when required', () => assertPasses(
childrenOf(number).isRequired,
(
{1}
{2}
{3}
),
'children',
'numbers',
));
it('passes with multiple strings', () => assertPasses(
childrenOf(string),
(
a
b
c
),
'children',
'strings',
));
it('passes with multiple strings when required', () => assertPasses(
childrenOf(string).isRequired,
(
a
b
c
),
'children',
'strings',
));
it('passes with strings and numbers', () => assertPasses(
childrenOf(or([string, number])),
(
a
b
c
{1}
{2}
{3}
),
'children',
'strings + numbers',
));
it('passes with strings and numbers when required', () => assertPasses(
childrenOf(or([string, number])).isRequired,
(
a
b
c
{1}
{2}
{3}
),
'children',
'strings + numbers',
));
});
describe('with a single child of the specified type', () => {
it('passes with a DOM element', () => assertPasses(
childrenOf(elementType('span')),
(
),
'children',
'span!',
));
it('passes with an SFC', () => assertPasses(
childrenOf(elementType(SFC)),
(
),
'children',
'SFC!',
));
it('passes with a Component', () => assertPasses(
childrenOf(elementType(Component)),
(
),
'children',
'Component!',
));
});
describe('with multiple children of the specified type', () => {
it('passes with a DOM element', () => assertPasses(
childrenOf(elementType('span')),
(
),
'children',
'span!',
));
it('passes with an SFC', () => assertPasses(
childrenOf(elementType(SFC)),
(
),
'children',
'SFC!',
));
it('passes with a Component', () => assertPasses(
childrenOf(elementType(Component)),
(
),
'children',
'Component!',
));
});
describe('with children of the specified types passed as an array', () => {
it('passes with a DOM element', () => assertPasses(
childrenOf(elementType('span')),
(
{[
,
,
,
]}
),
'children',
'span!',
));
it('passes with an SFC', () => assertPasses(
childrenOf(elementType(SFC)),
(
{[
,
,
,
]}
),
'children',
'SFC!',
));
it('passes with a Component', () => assertPasses(
childrenOf(elementType(Component)),
(
{[
,
,
,
]}
),
'children',
'Component!',
));
});
describe('when an unspecified type is provided as a child', () => {
it('fails expecting a DOM element', () => assertFails(
childrenOf(elementType('span')),
(
),
'children',
'span!',
));
it('fails expecting an SFC', () => assertFails(
childrenOf(elementType(SFC)),
(
),
'children',
'SFC!',
));
it('fails expecting a Component', () => assertFails(
childrenOf(elementType(Component)),
(
),
'children',
'Component!',
));
});
});