refactor(ui): extract reusable component package

This commit is contained in:
vince 2026-06-20 05:27:09 +02:00
parent 87261b5a3f
commit 2664804e91
86 changed files with 102 additions and 85 deletions

View file

@ -0,0 +1,56 @@
import { describe, expect, test } from "vitest";
import {
getNextUiTheme,
isUiTheme,
persistUiTheme,
resolveInitialUiTheme,
UI_THEME_STORAGE_KEY,
} from "./theme";
describe("UI theme preference", () => {
test("defaults to dark when no stored preference exists", () => {
const storage = new Map<string, string>();
expect(resolveInitialUiTheme(storage)).toBe("dark");
});
test("restores a valid stored preference", () => {
const storage = new Map<string, string>([[UI_THEME_STORAGE_KEY, "light"]]);
expect(resolveInitialUiTheme(storage)).toBe("light");
});
test("ignores invalid stored preferences", () => {
const storage = new Map<string, string>([[UI_THEME_STORAGE_KEY, "solarized"]]);
expect(resolveInitialUiTheme(storage)).toBe("dark");
});
test("falls back when stored preferences cannot be read", () => {
const storage = {
getItem() {
throw new Error("storage blocked");
},
};
expect(resolveInitialUiTheme(storage)).toBe("dark");
});
test("ignores persistence failures", () => {
const storage = {
setItem() {
throw new Error("quota exceeded");
},
};
expect(() => persistUiTheme("light", storage)).not.toThrow();
});
test("detects and toggles supported themes", () => {
expect(isUiTheme("light")).toBe(true);
expect(isUiTheme("dark")).toBe(true);
expect(isUiTheme("contrast")).toBe(false);
expect(getNextUiTheme("dark")).toBe("light");
expect(getNextUiTheme("light")).toBe("dark");
});
});