~james/core

core/web/src/lib/theme.test.ts -rw-r--r-- 1.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
import { describe, expect, it } from "vitest";
import { themeAttribute, themeFromCookies } from "#lib/theme.ts";

describe("themeFromCookies", () => {
	it("reads the theme cookie among others", () => {
		expect(themeFromCookies("a=1; theme=dark; b=2")).toBe("dark");
		expect(themeFromCookies("theme=light")).toBe("light");
		expect(themeFromCookies("theme=ash")).toBe("ash");
	});

	it("falls back to auto for missing or unknown values", () => {
		expect(themeFromCookies(null)).toBe("auto");
		expect(themeFromCookies("a=1")).toBe("auto");
		expect(themeFromCookies("theme=sepia")).toBe("auto");
		expect(themeFromCookies("theme=constructor")).toBe("auto");
	});
});

describe("themeAttribute", () => {
	it("only stamps explicit themes", () => {
		expect(themeAttribute("dark")).toBe(' data-theme="dark"');
		expect(themeAttribute("auto")).toBe("");
	});

	it("stamps a preset on its base mode", () => {
		expect(themeAttribute("ash")).toBe(' data-theme="dark" data-palette="ash"');
		expect(themeAttribute("snow")).toBe(' data-theme="light" data-palette="snow"');
		expect(themeAttribute("classic-dark")).toBe(
			' data-theme="dark" data-palette="classic-dark"'
		);
	});
});