Skip to content

Commit

Permalink
test: utils test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Jan 30, 2024
1 parent 579145a commit 75027f4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"release": "bumpp && npm publish",
"start": "esno src/index.ts",
"test": "vitest",
"test:update": "vitest -u",
"typecheck": "tsc --noEmit",
"prepare": "simple-git-hooks"
},
Expand Down
85 changes: 85 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, it } from 'vitest'
import { convertColor, hexToHsb, hexToHsl, hexToRgb, hsbToHex, hsbToHsl, hsbToRgb, hslToHex, hslToHsb, hslToRgb, isColor, rgbToHex, rgbToHsb, rgbToHsl } from '../src/index'
import type { ColorType } from '../src/types'

describe('utils scoped', () => {
const hex = '#ff0000'
const rgb = 'rgb(255, 0, 0)'
const hsl = 'hsl(0, 100%, 50%)'
const hsb = 'hsb(0, 100%, 100%)'
const isnotHex = 'rgb(255, 0, 0, 0.5)'
const isnotRgb = 'rgb(255, 0)'
const isnotHsl = 'hsl(0, 100%)'
const isnotHsb = 'hsb(0, 100%)'

const colors = [hex, rgb, hsl, hsb]
const notColors = [isnotHex, isnotRgb, isnotHsl, isnotHsb]

it('isColor', () => {
expect(colors.every(isColor)).toEqual(true)
expect(notColors.every(isColor)).toEqual(false)
})

it('simple convert', () => {
// rgb to others test case
expect(rgbToHsl(rgb)).toEqual(hsl)
expect(rgbToHsb(rgb)).toEqual(hsb)
expect(rgbToHex(rgb)).toEqual(hex)

// hex to others test case
expect(hexToHsb(hex)).toEqual(hsb)
expect(hexToHsl(hex)).toEqual(hsl)
expect(hexToRgb(hex)).toEqual(rgb)

// hsl to others test case
expect(hslToRgb(hsl)).toEqual(rgb)
expect(hslToHex(hsl)).toEqual(hex)
expect(hslToHsb(hsl)).toEqual(hsb)

// hsb to others test case
expect(hsbToRgb(hsb)).toEqual(rgb)
expect(hsbToHex(hsb)).toEqual(hex)
expect(hsbToHsl(hsb)).toEqual(hsl)
})

it('convertColor', () => {
function testTrueConvert(type: ColorType) {
return colors.map(c => convertColor(c, type))
}

function testFalseConvert(type: ColorType) {
return notColors.map(c => convertColor(c, type))
}

expect((['rgb', 'hex', 'hsl', 'hsb'] as ColorType[]).map(testTrueConvert)).toMatchInlineSnapshot(`
[
[
"rgb(255, 0, 0)",
"rgb(255, 0, 0)",
"rgb(255, 0, 0)",
"rgb(255, 0, 0)",
],
[
"#ff0000",
"#ff0000",
"#ff0000",
"#ff0000",
],
[
"hsl(0, 100%, 50%)",
"hsl(0, 100%, 50%)",
"hsl(0, 100%, 50%)",
"hsl(0, 100%, 50%)",
],
[
"hsb(0, 100%, 100%)",
"hsb(0, 100%, 100%)",
"hsb(0, 100%, 100%)",
"hsb(0, 100%, 100%)",
],
]
`)

expect(testFalseConvert('hex').filter(Boolean)).toEqual([])
})
})

0 comments on commit 75027f4

Please sign in to comment.