import { describe, expect, it } from "vitest"; import { DEFAULT_THEME, parseTheme, themeAttribute, themeFromCookies } from "#lib/theme.ts"; describe("themeFromCookies", () => { it("reads the theme cookie among others", () => { expect(themeFromCookies("a=1; theme=dark; b=2")).toEqual({ system: false, preset: "dark" }); expect(themeFromCookies("theme=ash")).toEqual({ system: false, preset: "ash" }); expect(themeFromCookies("theme=snow:ash")).toEqual({ system: true, light: "snow", dark: "ash" }); }); it("falls back to the system default for missing or unknown values", () => { expect(themeFromCookies(null)).toEqual(DEFAULT_THEME); expect(themeFromCookies("a=1")).toEqual(DEFAULT_THEME); expect(themeFromCookies("theme=sepia")).toEqual(DEFAULT_THEME); expect(themeFromCookies("theme=constructor")).toEqual(DEFAULT_THEME); }); }); describe("parseTheme", () => { it("drops a system preset of the wrong mode", () => { expect(parseTheme("ash:snow")).toEqual(DEFAULT_THEME); expect(parseTheme("sepia:ash")).toEqual({ system: true, light: "light", dark: "ash" }); }); }); describe("themeAttribute", () => { it("only stamps a mode on fixed themes", () => { expect(themeAttribute({ system: false, preset: "dark" })).toBe(' data-theme="dark"'); expect(themeAttribute(DEFAULT_THEME)).toBe(""); }); it("stamps a fixed preset on its base mode", () => { expect(themeAttribute({ system: false, preset: "ash" })).toBe( ' data-theme="dark" data-palette-dark="ash"' ); expect(themeAttribute({ system: false, preset: "snow" })).toBe( ' data-theme="light" data-palette-light="snow"' ); }); it("stamps a system theme with a palette for each mode", () => { expect(themeAttribute({ system: true, light: "snow", dark: "ash" })).toBe( ' data-palette-light="snow" data-palette-dark="ash"' ); expect(themeAttribute({ system: true, light: "light", dark: "classic-dark" })).toBe( ' data-palette-dark="classic-dark"' ); }); });