-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
73 lines (67 loc) · 2.7 KB
/
Copy pathindex.test.ts
File metadata and controls
73 lines (67 loc) · 2.7 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
import { describe, it, expect } from "vitest";
import { hexToOklch } from "./index.js";
describe("hexToOklch", () => {
describe("achromatic colors (hue should be 0)", () => {
it("white", () => expect(hexToOklch("#ffffff")).toBe("oklch(1 0 0)"));
it("black", () => expect(hexToOklch("#000000")).toBe("oklch(0 0 0)"));
it("gray", () => {
const r = hexToOklch("#808080", 4, { raw: true });
expect(r.h).toBe(0);
expect(r.c).toBeLessThan(0.001);
});
});
describe("known sRGB primaries", () => {
it("red", () => {
const r = hexToOklch("#ff0000", 4, { raw: true });
expect(r.l).toBeCloseTo(0.6279, 3);
expect(r.c).toBeCloseTo(0.2577, 3);
expect(r.h).toBeCloseTo(29.23, 1);
});
it("green", () => {
const r = hexToOklch("#00ff00", 4, { raw: true });
expect(r.l).toBeCloseTo(0.8664, 3);
expect(r.c).toBeCloseTo(0.2948, 3);
expect(r.h).toBeCloseTo(142.5, 1);
});
it("blue", () => {
const r = hexToOklch("#0000ff", 4, { raw: true });
expect(r.l).toBeCloseTo(0.4520, 3);
expect(r.c).toBeCloseTo(0.3132, 3);
expect(r.h).toBeCloseTo(264.05, 1);
});
});
describe("Tailwind palette spot checks", () => {
it("blue-500 (#3b82f6)", () => {
const r = hexToOklch("#3b82f6", 4, { raw: true });
expect(r.l).toBeCloseTo(0.6231, 2);
expect(r.h).toBeCloseTo(259.81, 1);
});
it("rose-600 (#e11d48)", () => {
const r = hexToOklch("#e11d48", 4, { raw: true });
expect(r.l).toBeCloseTo(0.5858, 2);
expect(r.h).toBeCloseTo(17.58, 1);
});
});
describe("input forms", () => {
it("accepts 3-digit hex", () => expect(hexToOklch("#fff")).toBe("oklch(1 0 0)"));
it("accepts hex without #", () => expect(hexToOklch("ffffff")).toBe("oklch(1 0 0)"));
it("handles uppercase", () => expect(hexToOklch("#FF0000")).toEqual(hexToOklch("#ff0000")));
it("trims whitespace", () => expect(hexToOklch(" #fff ")).toBe("oklch(1 0 0)"));
});
describe("API options", () => {
it("respects precision", () => {
const out = hexToOklch("#3b82f6", 2);
expect(out).toMatch(/^oklch\(0\.\d{1,2} 0\.\d{1,2} \d+(\.\d{1,2})?\)$/);
});
it("returns object when raw: true", () => {
const r = hexToOklch("#ffffff", 4, { raw: true });
expect(r).toEqual({ l: 1, c: 0, h: 0 });
});
});
describe("validation", () => {
it("throws on empty string", () => expect(() => hexToOklch("")).toThrow());
it("throws on garbage input", () => expect(() => hexToOklch("not-a-color")).toThrow());
it("throws on wrong length", () => expect(() => hexToOklch("#ff")).toThrow());
it("throws on non-hex chars", () => expect(() => hexToOklch("#zzzzzz")).toThrow());
});
});