forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobals.contrast.test.tsx
More file actions
74 lines (63 loc) · 2.41 KB
/
Copy pathglobals.contrast.test.tsx
File metadata and controls
74 lines (63 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import fs from "fs";
import path from "path";
import { render } from "@testing-library/react";
import { axe, toHaveNoViolations } from "jest-axe";
import "./globals.css";
expect.extend(toHaveNoViolations);
const cssSource = fs.readFileSync(path.join(__dirname, "globals.css"), "utf8");
const extractToken = (name) => {
const match = new RegExp(`${name}:\\s*(#(?:[0-9a-f]{6}))`, "i").exec(cssSource);
return match ? match[1].toLowerCase() : null;
};
function parseHexColor(value) {
const normalized = value.trim().toLowerCase();
const match = normalized.match(/^#([0-9a-f]{6})$/);
if (!match) {
throw new Error(`Unsupported color format: ${value}`);
}
const hex = match[1];
return [0, 2, 4].map((index) => parseInt(hex.slice(index, index + 2), 16));
}
function relativeLuminance([r, g, b]) {
const transform = (channel) => {
const value = channel / 255;
return value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
};
return 0.2126 * transform(r) + 0.7152 * transform(g) + 0.0722 * transform(b);
}
function contrastRatio(foreground, background) {
const fg = relativeLuminance(parseHexColor(foreground));
const bg = relativeLuminance(parseHexColor(background));
const lighter = Math.max(fg, bg);
const darker = Math.min(fg, bg);
return (lighter + 0.05) / (darker + 0.05);
}
function ContrastFixture() {
return (
<div className="bg-slate-950 text-slate-100 p-4">
<p data-testid="body-text" className="text-slate-100">
Body text
</p>
<p data-testid="muted-text" className="text-slate-400">
Muted text
</p>
</div>
);
}
describe.skip("globals.css theming + WCAG contrast smoke", () => {
it("has no basic axe accessibility violations in a dark-themed fixture", async () => {
const { container } = render(<ContrastFixture />);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it("ensures foreground and muted tokens meet WCAG AA contrast on the dark background", () => {
const bg = extractToken("--color-bg");
const foreground = extractToken("--color-foreground");
const muted = extractToken("--color-muted");
expect(bg).toBe("#020617");
expect(foreground).toBe("#f1f5f9");
expect(muted).toBe("#94a3b8");
expect(contrastRatio(foreground || "", bg || "")).toBeGreaterThanOrEqual(4.5);
expect(contrastRatio(muted || "", bg || "")).toBeGreaterThanOrEqual(4.5);
});
});