import { expect } from 'chai'; import { bool, func, oneOf, number, } from 'prop-types'; import React from 'react'; import { shape } from '..'; import callValidator from './_callValidator'; describe('shape', () => { it('is a function', () => { expect(typeof shape).to.equal('function'); }); describe('errors', () => { it('throws if not given a shape object', () => { expect(() => shape()).to.throw(TypeError); expect(() => shape(undefined)).to.throw(TypeError); expect(() => shape(null)).to.throw(TypeError); expect(() => shape([])).to.throw(TypeError); expect(() => shape('')).to.throw(TypeError); expect(() => shape(42)).to.throw(TypeError); expect(() => shape(NaN)).to.throw(TypeError); }); }); describe('validator', () => { it('returns a function', () => { expect(typeof shape({})).to.equal('function'); }); function assertPasses(validator, element, propName) { expect(callValidator(validator, element, propName, '"shape" test')).to.equal(null); } function assertFails(validator, element, propName) { expect(callValidator(validator, element, propName, '"shape" test')).to.be.instanceOf(Error); } it('behaves when nullary', () => { const validator = shape({ toFixed: func.isRequired }); assertPasses(validator, (
), 'a'); assertPasses(validator, (), 'a'); assertPasses(validator, (), 'a'); assertFails(validator.isRequired, (), 'a'); assertFails(validator.isRequired, (), 'a'); assertFails(validator.isRequired, (), 'a'); }); it('enforces the provided type', () => { assertPasses( shape({ toFixed: func.isRequired }), (), 'foo', ); assertFails( shape({ missing: bool.isRequired }), (), 'foo', ); }); it('enforces the provided shape', () => { assertPasses( shape({ length: oneOf([2]), }), (), 'foo', ); assertFails( shape({ length: oneOf([2]), missing: number.isRequired, }), (), 'foo', ); assertFails( shape({ length: oneOf([1]), }), (), 'foo', ); }); it('skips falsy shape keys', () => { assertPasses( shape({ toFixed: func.isRequired, missing: null, nope: false }), (), 'foo', ); }); }); });