Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saadeghi committed Jun 7, 2023
1 parent 611d3c7 commit 8d3260d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
18 changes: 9 additions & 9 deletions src/tests/a11y.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as themes from "../theming/themes"
import {
trimThemeName,
trimCssVariable,
lchValuesToHex,
hslValuesToHex,
ContrastRatioWarningThreshold,
ContrastRatioErrorThreshold,
isColorContrastOkay,
Expand Down Expand Up @@ -35,13 +35,13 @@ describe.each(
])(`${trimThemeName(themeKey)} color pairs`, (pair1, pair2) => {
if (
isColorContrastOkay(
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair1]),
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair2]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair1]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair2]),
ContrastRatioWarningThreshold
) === false &&
isColorContrastOkay(
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair1]),
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair2]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair1]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair2]),
ContrastRatioErrorThreshold
) === true
) {
Expand All @@ -54,8 +54,8 @@ describe.each(

if (
isColorContrastOkay(
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair1]),
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair2]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair1]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair2]),
ContrastRatioErrorThreshold
) === false
) {
Expand All @@ -68,8 +68,8 @@ describe.each(

expect(
isColorContrastOkay(
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair1]),
lchValuesToHex(colorFunctions.convertToLch(themes[themeKey])[pair2]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair1]),
hslValuesToHex(colorFunctions.convertColorFormat(themes[themeKey])[pair2]),
ContrastRatioErrorThreshold
)
).toBe(true)
Expand Down
16 changes: 6 additions & 10 deletions src/tests/colors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import { expect, test } from "vitest"
import * as colorFunctions from "../theming/functions"

test("Foreground color generator function", () => {
expect(colorFunctions.generateForegroundColorFrom("lch(100% 0 0)")).toBe("20 0 0")
expect(colorFunctions.generateForegroundColorFrom("lch(0% 0 0)")).toBe("80 0 0")
expect(colorFunctions.generateForegroundColorFrom("lch(100% 0 0)", 1)).toBe("0 0 0")
expect(colorFunctions.generateForegroundColorFrom("lch(0% 0 0)", 1)).toBe("100 0 0")
expect(colorFunctions.generateForegroundColorFrom("lch(60.83% 53.53 259.57)")).toBe(
"92.17 10.71 259.57"
)
expect(colorFunctions.generateForegroundColorFrom("lch(45.82% 76.92 31.84)")).toBe(
"89.16 15.38 31.84"
)
expect(colorFunctions.generateForegroundColorFrom("#fff")).toBe("146 0% 19%")
expect(colorFunctions.generateForegroundColorFrom("#000")).toBe("145 0% 78%")
expect(colorFunctions.generateForegroundColorFrom("#fff", 1)).toBe("0 0% 0%")
expect(colorFunctions.generateForegroundColorFrom("#000", 1)).toBe("161 100% 100%")
expect(colorFunctions.generateForegroundColorFrom("#1D9BF0")).toBe("215 87% 93%")
expect(colorFunctions.generateForegroundColorFrom("#CF212E")).toBe("8 93% 91%")
})
6 changes: 4 additions & 2 deletions src/tests/themes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe.each(
return colorName !== "default"
})
)(`${trimThemeName(themeKey)} has colors`, (colorName) => {
expect(colorFunctions.convertToLch(themes[themeKey])).toHaveProperty(colorNames[colorName])
expect(colorFunctions.convertColorFormat(themes[themeKey])).toHaveProperty(
colorNames[colorName]
)
})
})

Expand All @@ -41,6 +43,6 @@ describe.each(
"--tab-radius",
]
)(`${trimThemeName(themeKey)} has CSS variables`, (variableName) => {
expect(colorFunctions.convertToLch(themes[themeKey])).toHaveProperty(variableName)
expect(colorFunctions.convertColorFormat(themes[themeKey])).toHaveProperty(variableName)
})
})
14 changes: 7 additions & 7 deletions src/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ export const isColorContrastOkay = (input1, input2, ratio) => {
return false
}

function changeLchValuesToObject(input) {
const [l, c, h] = input.match(/\d+(\.\d+)?%|\d+(\.\d+)?/g).map(parseFloat)
return { l, c, h, a: 1 }
function changeHslValuesToObject(input) {
const [h, s, l] = input.match(/\d+(\.\d+)?%|\d+(\.\d+)?/g).map(parseFloat)
return { h, s, l, a: 1 }
}

export const lchValuesToHex = (input) => {
export const hslValuesToHex = (input) => {
return colord(
`lch(${changeLchValuesToObject(input).l}% ${changeLchValuesToObject(input).c} ${
changeLchValuesToObject(input).h
})`
`hsl(${changeHslValuesToObject(input).h} ${changeHslValuesToObject(input).s}% ${
changeHslValuesToObject(input).l
}%)`
).toHex()
}

Expand Down

0 comments on commit 8d3260d

Please sign in to comment.