63 lines
2.1 KiB
JavaScript
63 lines
2.1 KiB
JavaScript
import { describe, it, expect } from "vitest";
|
|
|
|
import {
|
|
contrastRatio,
|
|
parseHex,
|
|
relativeLuminance,
|
|
runColorContrastAudit,
|
|
} from "./verify-color-contrast.mjs";
|
|
|
|
describe("verify-color-contrast", () => {
|
|
describe("parseHex", () => {
|
|
it("parses 3-digit shorthand", () => {
|
|
expect(parseHex("#fff")).toEqual({ r: 255, g: 255, b: 255 });
|
|
expect(parseHex("#000")).toEqual({ r: 0, g: 0, b: 0 });
|
|
});
|
|
it("parses 6-digit hex", () => {
|
|
expect(parseHex("#2563eb")).toEqual({ r: 0x25, g: 0x63, b: 0xeb });
|
|
});
|
|
it("throws on invalid hex", () => {
|
|
expect(() => parseHex("oops")).toThrow();
|
|
});
|
|
});
|
|
|
|
describe("contrastRatio", () => {
|
|
it("returns 21 for black-on-white", () => {
|
|
expect(contrastRatio("#000000", "#ffffff")).toBeCloseTo(21, 1);
|
|
});
|
|
it("returns 1 for identical colours", () => {
|
|
expect(contrastRatio("#abcdef", "#abcdef")).toBeCloseTo(1, 5);
|
|
});
|
|
it("is symmetric (order does not matter)", () => {
|
|
const a = contrastRatio("#2563eb", "#ffffff");
|
|
const b = contrastRatio("#ffffff", "#2563eb");
|
|
expect(a).toBeCloseTo(b, 5);
|
|
});
|
|
it("returns ~5.17 for primary on white (known passing pair)", () => {
|
|
expect(contrastRatio("#2563eb", "#ffffff")).toBeCloseTo(5.17, 1);
|
|
});
|
|
it("returns < 3 for the known low-contrast border/background pair", () => {
|
|
expect(contrastRatio("#e5e7eb", "#f6f8fb")).toBeLessThan(3);
|
|
});
|
|
});
|
|
|
|
describe("relativeLuminance", () => {
|
|
it("returns 0 for black", () => {
|
|
expect(relativeLuminance({ r: 0, g: 0, b: 0 })).toBeCloseTo(0, 5);
|
|
});
|
|
it("returns 1 for white", () => {
|
|
expect(relativeLuminance({ r: 255, g: 255, b: 255 })).toBeCloseTo(1, 5);
|
|
});
|
|
});
|
|
|
|
describe("runColorContrastAudit (integration)", () => {
|
|
it("loads brand colours, runs all pairs, and currently passes AA", () => {
|
|
const result = runColorContrastAudit();
|
|
expect(result.ok).toBe(true);
|
|
expect(result.rows.length).toBeGreaterThan(0);
|
|
// No required pair should fail.
|
|
expect(result.failed).toEqual([]);
|
|
});
|
|
});
|
|
});
|