UNPKG

corporate-frontend-mithril

Version:

Corporate frontend MithrilJS modules

181 lines (145 loc) 7.9 kB
const helpers = require('./helpers'); describe('/src/js/helpers/helpers', () => { /** @test {Booleans} */ describe('Booleans', () => { /** @test {Booleans#isTruthyValue} */ describe('isTruthyValue', () => { it('It should return true if the value is \'truthy\'.', () => { expect(helpers.booleans.isTruthyValue(0)).toBe(true); expect(helpers.booleans.isTruthyValue(123)).toBe(true); expect(helpers.booleans.isTruthyValue('test')).toBe(true); expect(helpers.booleans.isTruthyValue(new Date())).toBe(true); }); it('It should return false if the value is not \'truthy\'.', () => { expect(helpers.booleans.isTruthyValue(null)).toBe(false); expect(helpers.booleans.isTruthyValue(undefined)).toBe(false); expect(helpers.booleans.isTruthyValue(NaN)).toBe(false); expect(helpers.booleans.isTruthyValue('')).toBe(false); }); }); }); /** @test {Colors} */ describe('Colors', () => { /** @test {Colors#rgb2rgba} */ describe('rgb2rgba', () => { it('It should convert rgb color to rgba color.', () => { expect( helpers.colors.rgb2rgba('rgb(85,85,85)', 0.5) ).toEqual('rgba(85,85,85, 0.5)'); }); it('It should return null if rgb is null.', () => { expect( helpers.colors.rgb2rgba(null, 0.5) ).toEqual(null); expect( helpers.colors.rgb2rgba('', 0.5) ).toEqual(null); }); }); /** @test {Colors#hex2rgb} */ describe('hex2rgb', () => { it('It should covert hex code to rgb color.', () => { expect( helpers.colors.hex2rgb('#FFF000') ).toEqual('rgb(255,240,0)'); expect( helpers.colors.hex2rgb('#fff') ).toEqual('rgb(255,255,255)'); expect( helpers.colors.hex2rgb('FFF') ).toEqual('rgb(255,255,255)'); expect( helpers.colors.hex2rgb('123') ).toEqual('rgb(17,34,51)'); }); it('It should return null if hex code is null.', () => { expect( helpers.colors.hex2rgb(null) ).toEqual(null); expect( helpers.colors.hex2rgb('') ).toEqual(null); }); it('It should return null for invalid hex code.', () => { expect( helpers.colors.hex2rgb('FF12') ).toEqual(null); }); }); /** @test {Colors#rgb2hex} */ describe('hex2rgb', () => { it('It should covert rgb color to hex color.', () => { expect( helpers.colors.rgb2hex(175,210,80) ).toEqual('#afd250'); }); it('It should return null for missing one of the colours.', () => { expect( helpers.colors.rgb2hex(175) ).toEqual(null); expect( helpers.colors.rgb2hex(null,210) ).toEqual(null); expect( helpers.colors.rgb2hex(null,null,80) ).toEqual(null); expect( helpers.colors.rgb2hex() ).toEqual(null); }); it('It should return null for invalid colour code.', () => { expect( helpers.colors.rgb2hex(256,255,255) ).toEqual(null); expect( helpers.colors.rgb2hex(255,256,255) ).toEqual(null); expect( helpers.colors.rgb2hex(255,255,256) ).toEqual(null); expect( helpers.colors.rgb2hex(-1,255,255) ).toEqual(null); expect( helpers.colors.rgb2hex(255,-1,255) ).toEqual(null); expect( helpers.colors.rgb2hex(255,255,-1) ).toEqual(null); }); }); /** @test {Colors#colorLuminance} */ describe('colorLuminance', () => { it('It should change color luminance with valid hex code and lum.', () => { expect( helpers.colors.colorLuminance('#FFFFFE', -0.5) ).toEqual('#80807f'); expect( helpers.colors.colorLuminance('#FFF', -0.2) ).toEqual('#cccccc'); expect( helpers.colors.colorLuminance('#fFf', -0.3) ).toEqual('#b3b3b3'); expect( helpers.colors.colorLuminance('#aaa', 0.2) ).toEqual('#cccccc'); expect( helpers.colors.colorLuminance('#fffeee', 0.1) ).toEqual('#ffffff'); }); it('It should return null for invalid hex code.', () => { expect( helpers.colors.colorLuminance(null, -0.5) ).toEqual(null); }); it('It should return null if luminance is out of the range from -1 to 1.', () => { expect( helpers.colors.colorLuminance('aaa', -1.1) ).toEqual(null); expect( helpers.colors.colorLuminance('aaa', 1.1) ).toEqual(null); }); }); }); /** @test {Numbers} */ describe('Numbers', () => { /** @test {Numbers#isNumber} */ describe('isNumber', () => { it('It should return true if the value is number.', () => { expect(helpers.numbers.isNumber(+123)).toBe(true); expect(helpers.numbers.isNumber(123)).toBe(true); expect(helpers.numbers.isNumber('123')).toBe(true); }); it('It should return true if the number is negative.', () => { expect(helpers.numbers.isNumber(-123)).toBe(true); expect(helpers.numbers.isNumber('-123')).toBe(true); }); it('It should return false if the value is not \'truthy\'.', () => { expect(helpers.numbers.isNumber(null)).toBe(false); expect(helpers.numbers.isNumber(undefined)).toBe(false); expect(helpers.numbers.isNumber(NaN)).toBe(false); expect(helpers.numbers.isNumber('')).toBe(false); expect(helpers.numbers.isNumber('#@')).toBe(false); }); }); /** @test {Numbers#stripNonNumericCharacters} */ describe('stripNonNumericCharacters', () => { it('It should return a 10 bit float point number.', () => { expect(helpers.numbers.stripNonNumericCharacters('abc123.80<blah>')).toEqual(123.8); expect(helpers.numbers.stripNonNumericCharacters('abc123.00<blah>')).toEqual(123); }); it('It should support negative number.', () => { expect(helpers.numbers.stripNonNumericCharacters('<span>-12</span>')).toEqual(-12); }); }); }); /** @test {Strings} */ describe('Strings', () => { /** @test {Strings#random} */ describe('random', () => { it('It should return a random string.', () => { expect( helpers.strings.random() === helpers.strings.random() ).toBe(false); }); it('It should return a random string, the default length is the maximum 12.', () => { expect( helpers.strings.random().length ).toEqual(8); }); it('It should return a random string with the input length.', () => { expect( helpers.strings.random(8).length ).toEqual(8); expect( helpers.strings.random(12).length ).toEqual(12); }); it('When the input length is larger than the maximum 12, it should return a random string with the maximum length 12.', () => { expect( helpers.strings.random(16).length ).toEqual(12); }); }); /** @test {Strings#truncate} */ describe('truncate', () => { it('It should truncate a string.', () => { expect( helpers.strings.truncate('Maecenas tempus tellus eget condimentum', 12) ).toEqual('Maecenas...'); expect( helpers.strings.truncate('Maecs to tellus eget condimentum', 12, false) ).toEqual('Maecs to te...'); }); }); }); });