Skip to content
26 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { DEFAULT_THEME, THEME_CHOICES, THEME_COOKIE, readTheme, resolvedTheme, themeAttribute, themeCookie } from "./theme.ts";
5
6test("the cookie keeps auto, light or dark, and anything else is the default", () => {
7 for (const choice of THEME_CHOICES) assert.equal(readTheme(choice), choice);
8 for (const junk of [null, undefined, "", "LIGHT", "sepia", "dark; Path=/"]) assert.equal(readTheme(junk), DEFAULT_THEME);
9});
10
11test("the cookie is kept a year, on every path, and secure on https", () => {
12 assert.equal(themeCookie("light", true), `${THEME_COOKIE}=light; Path=/; Max-Age=31536000; SameSite=Lax; Secure`);
13 assert.equal(themeCookie("auto", false), `${THEME_COOKIE}=auto; Path=/; Max-Age=31536000; SameSite=Lax`);
14});
15
16test("Light and Dark are data-theme on <html>; Auto leaves it to the system", () => {
17 assert.equal(themeAttribute("light"), "light");
18 assert.equal(themeAttribute("dark"), "dark");
19 assert.equal(themeAttribute("auto"), undefined);
20});
21
22test("a chosen theme is itself; Auto with no browser to ask is dark", () => {
23 assert.equal(resolvedTheme("light"), "light");
24 assert.equal(resolvedTheme("dark"), "dark");
25 assert.equal(resolvedTheme("auto"), "dark");
26});