Files
writer-work-flow/apps/web/lib/ui/theme.test.ts
2026-06-28 07:31:20 +02:00

47 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
DEFAULT_THEME_MODE,
isThemeMode,
nextThemeMode,
normalizeThemeMode,
themeBootstrapScript,
themeModeLabel,
themeToggleLabel,
} from "./theme";
describe("theme mode", () => {
it("accepts only supported theme modes", () => {
expect(isThemeMode("paper")).toBe(true);
expect(isThemeMode("night")).toBe(true);
expect(isThemeMode("dark")).toBe(false);
expect(isThemeMode(null)).toBe(false);
});
it("falls back to paper for unknown stored values", () => {
expect(normalizeThemeMode("night")).toBe("night");
expect(normalizeThemeMode("")).toBe(DEFAULT_THEME_MODE);
expect(normalizeThemeMode("auto")).toBe(DEFAULT_THEME_MODE);
});
it("toggles between paper and night", () => {
expect(nextThemeMode("paper")).toBe("night");
expect(nextThemeMode("night")).toBe("paper");
});
it("returns labels for the current mode and the next action", () => {
expect(themeModeLabel("paper")).toBe("纸感模式");
expect(themeModeLabel("night")).toBe("夜读模式");
expect(themeToggleLabel("paper")).toBe("切换到夜读模式");
expect(themeToggleLabel("night")).toBe("切换到纸感模式");
});
it("builds a bootstrap script that applies the stored mode before hydration", () => {
const script = themeBootstrapScript();
expect(script).toContain("localStorage.getItem(\"ww.theme_mode\")");
expect(script).toContain("document.documentElement.dataset.theme");
expect(script).toContain("mode !== \"paper\" && mode !== \"night\"");
});
});