import { describe } from 'mocha' import { should } from 'chai' import Not from 'you-are-not' const { is, not, define } = require('../src/index') const { NotTypeError } = Not should() describe('Boolean test - is', () => { it('should return true for comparing with bool true', () => { is(true).boolean().should.be.true }) it('should return true for comparing with bool false', () => { is(false).boolean().should.be.true }) it('should throw error for comparing with non-boolean', () => { ;(() => { is(1).boolean() }).should.Throw( NotTypeError, 'Wrong Type (Anonymous param): Expecting type `boolean` but got `number` with value of `1`.', ) }) }) describe('Boolean test - not', () => { it('should not throw error (return true) for comparing with non-bool', () => { not('someString').boolean().should.be.true }) it('should return true for comparing with bool false', () => { is(false).boolean().should.be.true }) it('should throw error for comparing with non-boolean', () => { ;(() => { is(1).boolean() }).should.Throw( NotTypeError, 'Wrong Type (Anonymous param): Expecting type `boolean` but got `number` with value of `1`', ) }) }) describe('Object test', () => { it('should be able to check if something is object', () => { is({}).object().should.be.true }) it('should return object for comparing with correct schema', () => { is({ foo: { bar: 1, }, }) .object({ foo: { bar: 'number', }, }) .should.deep.include({ foo: { bar: 1 } }) }) }) describe('Status code', () => { it('should set status code', () => { is(true).statusCode(555)._statusCode.should.equal(555) }) it('should throw error with set status code', () => { var error try { is(1, 'foo').statusCode(666).boolean() } catch (err: any) { error = err } error.statusCode.should.equal(666) }) }) describe('Custom messaging', () => { it('should have custom message', () => { ;(() => { is([], 'FooFoo').msg('You need to provide an object.').object() }).should.Throw( NotTypeError, '(Param: FooFoo) You need to provide an object.', ) }) it('should have custom message', () => { ;(() => { not('string', 'FooFoo').msg('It must not be string.').string() }).should.Throw(NotTypeError, '(Param: FooFoo) It must not be string.') }) it('should throw error with set status code', () => { var error try { is(1, 'foo').statusCode(666).boolean() } catch (err: any) { error = err } error.statusCode.should.equal(666) }) }) describe('Define new checks', () => { it('should be able to define new checks/types', () => { define({ primitives: ['string', 'boolean'], nameOfTyping: 'truthy', toPass: (candidate: any) => { if (candidate === 'true') return true if (candidate === true) return true return false }, }) is('true').custom('truthy').should.be.true ;(() => { is([]).custom('truthy') }).should.Throw( NotTypeError, 'Wrong Type (Anonymous param): Expecting type `custom:truthy` but got `array` with value of `[]`.', ) not(1).custom('truthy').should.be.true ;(() => { not(true).custom('truthy') }).should.Throw(NotTypeError, 'Anonymous param is of type truthy.') }) })