import { expect } from 'chai'; import { number, string } from 'prop-types'; import React from 'react'; import { childrenSequenceOf } from '..'; import callValidator from './_callValidator'; describe('childrenSequenceOf', () => { it('is a function', () => { expect(typeof childrenSequenceOf).to.equal('function'); }); function assertPasses(validator, element, propName) { const error = callValidator(validator, element, propName, '"childrenSequenceOf" passing test'); expect(error).to.equal(null); } function assertFails(validator, element, propName) { const error = callValidator(validator, element, propName, '"childrenSequenceOf" failing test'); expect(error).to.be.instanceOf(Error); } it('throws if not given at least one specifier', () => { expect(() => childrenSequenceOf()).to.throw(RangeError); expect(() => childrenSequenceOf(undefined)).to.throw(TypeError); expect(() => childrenSequenceOf(null)).to.throw(TypeError); expect(() => childrenSequenceOf([])).to.throw(TypeError); expect(() => childrenSequenceOf('')).to.throw(TypeError); expect(() => childrenSequenceOf(42)).to.throw(TypeError); expect(() => childrenSequenceOf(NaN)).to.throw(TypeError); }); it('throws if given an invalid validator', () => { expect(() => childrenSequenceOf({ validator: null })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator: undefined })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator: false })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator: [] })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator: {} })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator: '' })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator: 3 })).to.throw(TypeError); }); it('throws given a non-positive-integer min or max', () => { const validator = number; expect(() => childrenSequenceOf({ validator, min: -1 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: -1 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, min: -1.4 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: -1.4 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, min: NaN })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: NaN })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, min: Infinity })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: Infinity })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, min: -Infinity })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: -Infinity })).to.throw(TypeError); }); it('throws given inverted "min"/"max"', () => { const validator = number; expect(() => childrenSequenceOf({ validator, min: 2, max: 1 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: 1, min: 2 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, min: 2, max: 0 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: 0, min: 2 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, min: 1, max: 0 })).to.throw(TypeError); expect(() => childrenSequenceOf({ validator, max: 0, min: 1 })).to.throw(TypeError); // "min" defaults to 1 expect(() => childrenSequenceOf({ validator, max: 0 })).to.throw(TypeError); }); it('returns a function', () => { expect(typeof childrenSequenceOf({ validator: number })).to.equal('function'); }); it('fails on a non-children prop', () => { const validator = childrenSequenceOf({ validator: number }); assertFails(validator, (
), 'cousins'); assertFails(validator.isRequired, (), 'cousins'); }); it('passes with null/undefined when optional', () => { const validator = childrenSequenceOf({ validator: number }); assertPasses(validator, (), 'children'); assertPasses(validator, (