test: add test specs

This commit is contained in:
bubkoo
2021-03-30 16:30:13 +08:00
parent 76b999bf7a
commit f01da404c7
3 changed files with 133 additions and 9 deletions

View File

@ -3,6 +3,52 @@ import { Color } from './color'
const { objectContaining } = jasmine
describe('Color', () => {
describe('static', () => {
describe('isRgbLike()', () => {
it('shoud return true if the given object is rgb like', () => {
expect(Color.isRgbLike({ r: 1, g: 1, b: 1 })).toBeTrue()
expect(Color.isRgbLike({ r: 1, g: 1, b: 1, a: 0.5 })).toBeTrue()
})
it('shoud return false if the given object is not rgb like', () => {
expect(Color.isRgbLike({ r: 1, g: 1 })).toBeFalse()
expect(Color.isRgbLike({ r: 1, b: 1, a: 0.5 })).toBeFalse()
})
})
describe('invert', () => {
it('shoud invert the given hex color', () => {
expect(Color.invert('#ffffff')).toEqual('#000000')
expect(Color.invert([255, 255, 255, 1])).toEqual([0, 0, 0, 1])
expect(Color.invert([100, 100, 100, 1], true)).toEqual([
255,
255,
255,
1,
])
expect(Color.invert([200, 200, 200, 1], true)).toEqual([0, 0, 0, 1])
})
})
describe('fromHex', () => {
it('should return null when the given string is not a valid hex string', () => {
expect(Color.fromHex('')).toBeNull()
})
})
describe('fromHsl', () => {
it('should return null when the given string is not a valid hsla string', () => {
expect(Color.fromHsl('')).toBeNull()
})
})
describe('fromRgb', () => {
it('should return null when the given string is not a valid rgba string', () => {
expect(Color.fromRgb('')).toBeNull()
})
})
})
describe('constructor', () => {
it('shoud create a Color with default args', () => {
const color = new Color()
@ -19,14 +65,14 @@ describe('Color', () => {
expect(black.b).toBe(0)
expect(black.a).toBe(1)
const white = new Color(Color.presets.white)
const white = new Color('white')
expect(white.r).toBe(255)
expect(white.g).toBe(255)
expect(white.b).toBe(255)
expect(black.a).toBe(1)
})
it('should create a Color from hex color', () => {
it('should create a Color from hex string', () => {
const black = new Color('#000000')
expect(black.r).toBe(0)
expect(black.g).toBe(0)
@ -40,6 +86,30 @@ describe('Color', () => {
expect(black.a).toBe(1)
})
it('should create a Color from rgb string', () => {
expect(new Color('rgb(255,255,255)')).toEqual(
objectContaining({ r: 255, g: 255, b: 255, a: 1 }),
)
})
it('should create a Color from rgba string', () => {
expect(new Color('rgba(255,255,255,0.5)')).toEqual(
objectContaining({ r: 255, g: 255, b: 255, a: 0.5 }),
)
})
it('should create a Color from hsl string', () => {
expect(new Color('hsl(10,10,10)')).toEqual(
objectContaining({ r: 28, g: 24, b: 23, a: 1 }),
)
})
it('should create a Color from hsla string', () => {
expect(new Color('hsl(10,10,10,0.5)')).toEqual(
objectContaining({ r: 28, g: 24, b: 23, a: 0.5 }),
)
})
it('should not parse invalid string', () => {
const color = new Color('')
expect(color.r).toBeUndefined()
@ -132,6 +202,8 @@ describe('Color', () => {
expect(new Color().darken(10)).toEqual(
objectContaining({ r: 245, g: 245, b: 245, a: 1 }),
)
expect(Color.darken('#ffffff', 10)).toEqual('#f5f5f5')
})
})

View File

@ -145,7 +145,8 @@ export namespace Color {
typeof val === 'object' &&
typeof val.r === 'number' &&
typeof val.g === 'number' &&
typeof val.b === 'number'
typeof val.b === 'number' &&
(typeof val.a === 'undefined' || typeof val.a === 'number')
)
}
@ -176,7 +177,7 @@ export namespace Color {
export function fromRgb(rgba: string) {
const matches = rgba.toLowerCase().match(rRgb)
if (matches) {
const arr = matches[1].split(/\s*,\s*/).map((v) => parseInt(v, 10))
const arr = matches[1].split(/\s*,\s*/).map((v) => parseFloat(v))
return fromArray(arr as Color.RGBA)
}
@ -186,11 +187,11 @@ export namespace Color {
export function fromHsl(color: string) {
const matches = color.toLowerCase().match(rHsl)
if (matches) {
const arr = matches[2].split(/\s*,\s*/)
const arr = matches[1].split(/\s*,\s*/)
const h = (((parseFloat(arr[0]) % 360) + 360) % 360) / 360
const s = parseFloat(arr[1]) / 100
const l = parseFloat(arr[2]) / 100
const a = arr[3] == null ? 1 : parseInt(arr[3], 10)
const a = arr[3] == null ? 1 : parseFloat(arr[3])
return new Color(Util.hsla2rgba(h, s, l, a))
}
@ -244,9 +245,9 @@ export namespace Color {
return random(ignoreAlpha).toString()
}
export function invert(rgba: RGBA, bw: boolean): RGBA
export function invert(hex: string, bw: boolean): string
export function invert(color: string | RGBA, bw: boolean) {
export function invert(rgba: RGBA, bw?: boolean): RGBA
export function invert(hex: string, bw?: boolean): string
export function invert(color: string | RGBA, bw?: boolean) {
if (typeof color === 'string') {
const pound = color[0] === '#'
const [r, g, b] = Util.hex2rgb(color)

View File

@ -0,0 +1,51 @@
import * as Fn from './index'
describe('types', () => {
describe('isFalsy()', () => {
it('should return true when the given value is falsy', () => {
const vals = [null, undefined, 0, false]
vals.forEach((v) => {
expect(Fn.isFalsy(v)).toBeTrue()
})
})
it('should return false when the given value is not falsy', () => {
const vals = [1, {}, () => {}, true]
vals.forEach((v) => {
expect(Fn.isFalsy(v)).toBeFalse()
})
})
})
describe('isNullish()', () => {
it('should return true when the given value is nil', () => {
const vals = [null, undefined]
vals.forEach((v) => {
expect(Fn.isNullish(v)).toBeTrue()
})
})
it('should return false when the given value is not nil', () => {
const vals = [1, {}, () => {}, true]
vals.forEach((v) => {
expect(Fn.isNullish(v)).toBeFalse()
})
})
})
describe('isPrimitive()', () => {
it('should return true when the given value is primitive', () => {
const vals = [null, undefined, 1, 'abc', true, Symbol('test')]
vals.forEach((v) => {
expect(Fn.isPrimitive(v)).toBeTrue()
})
})
it('should return true when the given value is not primitive', () => {
const vals = [{}, () => {}]
vals.forEach((v) => {
expect(Fn.isPrimitive(v)).toBeFalsy()
})
})
})
})