74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
|
|
// Function to calculate relative luminance according to WCAG 2.1 specs
|
|
function getLuminance(hex: string): number {
|
|
const rgb = hex.replace("#", "");
|
|
const r = parseInt(rgb.substring(0, 2), 16) / 255;
|
|
const g = parseInt(rgb.substring(2, 4), 16) / 255;
|
|
const b = parseInt(rgb.substring(4, 6), 16) / 255;
|
|
|
|
const a = [r, g, b].map((v) => {
|
|
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
});
|
|
|
|
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
|
|
}
|
|
|
|
// Function to calculate contrast ratio between two hex colors
|
|
function getContrastRatio(hex1: string, hex2: string): number {
|
|
const lum1 = getLuminance(hex1);
|
|
const lum2 = getLuminance(hex2);
|
|
const brightest = Math.max(lum1, lum2);
|
|
const darkest = Math.min(lum1, lum2);
|
|
return (brightest + 0.05) / (darkest + 0.05);
|
|
}
|
|
|
|
describe("WCAG 2.1 Contrast & Readability Automated Gate (UX-34)", () => {
|
|
const colors = {
|
|
whiteBg: "#FFFFFF",
|
|
alternateBg: "#F4F7F6",
|
|
caninaBlue: "#1E4053",
|
|
medicalGray500: "#445D60",
|
|
medicalGray600: "#33484B",
|
|
medicalGray700: "#202F32",
|
|
medicalGray800: "#141F22",
|
|
medicalGray900: "#060709",
|
|
darkBg: "#060709",
|
|
};
|
|
|
|
it("ensures primary text tokens exceed WCAG AAA (7:1) on white background", () => {
|
|
const ratio900 = getContrastRatio(colors.medicalGray900, colors.whiteBg);
|
|
expect(ratio900).toBeGreaterThanOrEqual(7.0);
|
|
|
|
const ratio800 = getContrastRatio(colors.medicalGray800, colors.whiteBg);
|
|
expect(ratio800).toBeGreaterThanOrEqual(7.0);
|
|
|
|
const ratio700 = getContrastRatio(colors.medicalGray700, colors.whiteBg);
|
|
expect(ratio700).toBeGreaterThanOrEqual(7.0);
|
|
});
|
|
|
|
it("ensures secondary text tokens pass WCAG AA (4.5:1) on white background", () => {
|
|
const ratio500 = getContrastRatio(colors.medicalGray500, colors.whiteBg);
|
|
expect(ratio500).toBeGreaterThanOrEqual(4.5);
|
|
|
|
const ratio600 = getContrastRatio(colors.medicalGray600, colors.whiteBg);
|
|
expect(ratio600).toBeGreaterThanOrEqual(4.5);
|
|
});
|
|
|
|
it("ensures brand canina-blue passes WCAG AA for body text (>4.5:1) on white and alternate background", () => {
|
|
const ratioWhite = getContrastRatio(colors.caninaBlue, colors.whiteBg);
|
|
expect(ratioWhite).toBeGreaterThanOrEqual(4.5);
|
|
|
|
const ratioAlt = getContrastRatio(colors.caninaBlue, colors.alternateBg);
|
|
expect(ratioAlt).toBeGreaterThanOrEqual(4.5);
|
|
});
|
|
|
|
it("ensures white text passes WCAG AAA (>7:1) on brand canina-blue and dark background", () => {
|
|
const ratioBrand = getContrastRatio("#FFFFFF", colors.caninaBlue);
|
|
expect(ratioBrand).toBeGreaterThanOrEqual(4.5);
|
|
|
|
const ratioDark = getContrastRatio("#FFFFFF", colors.darkBg);
|
|
expect(ratioDark).toBeGreaterThanOrEqual(7.0);
|
|
});
|
|
});
|