uci-utils-to-boolean/test/boolean.test.js

70 lines
2.6 KiB
JavaScript

import { expect } from 'chai'
import {toBoolean, createBoolean, rTrue,rFalse }from '../src/boolean'
describe('No options default function check - ', function () {
it('should return false for undefined', function () {
expect(toBoolean()).to.equal(false)
})
it('should return false for null', function () {
expect(toBoolean(null)).to.equal(false)
})
console.log('falsy', rFalse)
it('should return false for all falsy', function () {
expect(rFalse.reduce((acc,value)=>acc && toBoolean(value),false)).to.equal(false)
})
console.log('truthy', rTrue)
it('should return true for all truthy', function () {
expect(rTrue.reduce((acc,value)=>acc && toBoolean(value),true)).to.equal(true)
})
it('should return false for random word not in either list', function () {
expect(toBoolean('bogus')).to.equal(false)
})
it('should return false for number <=0', function () {
expect(toBoolean(-1)).to.equal(false)
})
it('should return true for number >0', function () {
expect(toBoolean(3)).to.equal(true)
})
})
let toBoolean2 = createBoolean({undefined:'same', null:'same',string:true,number:5})
describe('created Boolean function with these options \'{undefined:\'same\', null:\'same\',string:true,number:5}', function () {
it('should return undefined for undefined', function () {
expect(toBoolean2()).to.equal(undefined)
})
it('should return null for null', function () {
expect(toBoolean2(null)).to.equal(null)
})
it('should return true for random word', function () {
expect(toBoolean2('bogus')).to.equal(true)
})
it('but should return false for empty sting \'\' or \' \'', function () {
expect(!!(toBoolean2('') ^ toBoolean2(' '))).to.equal(false)
})
it('but should still return false for all falsy words', function () {
expect(rFalse.reduce((acc,value)=>acc && toBoolean(value),false)).to.equal(false)
})
it('should return false for number <=5', function () {
expect(toBoolean2(4)).to.equal(false)
})
})
let toBoolean3 = createBoolean({undefined:true, null:true,number:'default'})
describe('created boolean with {undefined:true, null:true, number:\'default\'}', function () {
it('should return true for undefined', function () {
expect(toBoolean3()).to.equal(true)
})
it('should return true for null', function () {
expect(toBoolean3(null)).to.equal(true)
})
it('should return false number =0', function () {
expect(toBoolean3(0)).to.equal(false)
})
it('and should return true for any other number like default casting', function () {
expect(toBoolean3(-1) && toBoolean3(1)).to.equal(true)
})
})