49 lines
1.7 KiB
JavaScript
49 lines
1.7 KiB
JavaScript
// @vitest-environment jsdom
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
normalizeColorSchemePreference,
|
|
resolveColorScheme,
|
|
} from "../../src/application/policies/color-scheme.js";
|
|
import { initializeColorScheme } from "../../src/bootstrap/initialize-color-scheme.js";
|
|
|
|
describe("color scheme policy", () => {
|
|
it("normalizes unknown persisted values to the safe system default", () => {
|
|
expect(normalizeColorSchemePreference("sepia")).toBe("system");
|
|
expect(normalizeColorSchemePreference("dark")).toBe("dark");
|
|
});
|
|
|
|
it("resolves system and explicit preferences deterministically", () => {
|
|
expect(resolveColorScheme("system", true)).toBe("dark");
|
|
expect(resolveColorScheme("system", false)).toBe("light");
|
|
expect(resolveColorScheme("light", true)).toBe("light");
|
|
});
|
|
|
|
it("applies a persisted preference before application paint", () => {
|
|
const storage =
|
|
/** @type {import("../../src/application/ports/storage-port.js").StoragePort} */ ({
|
|
read: () => ({
|
|
ok: /** @type {const} */ (true),
|
|
value: "system",
|
|
}),
|
|
write: () => ({ ok: /** @type {const} */ (true) }),
|
|
remove: () => ({ ok: /** @type {const} */ (true) }),
|
|
});
|
|
|
|
const result = initializeColorScheme(storage, {
|
|
documentElement: document.documentElement,
|
|
matchMedia: () =>
|
|
/** @type {MediaQueryList} */ ({ matches: true }),
|
|
});
|
|
|
|
expect(result).toEqual({ preference: "system", resolved: "dark" });
|
|
expect(document.documentElement).toHaveAttribute("data-theme", "dark");
|
|
expect(document.documentElement).toHaveAttribute(
|
|
"data-theme-preference",
|
|
"system",
|
|
);
|
|
expect(document.documentElement.style.colorScheme).toBe("dark");
|
|
});
|
|
});
|