Files
archie/ui/test/appearance.test.ts

79 lines
2.2 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { createPinia, setActivePinia } from "pinia";
class MemoryStorage {
readonly values = new Map<string, string>();
getItem(key: string): string | null {
return this.values.get(key) ?? null;
}
setItem(key: string, value: string): void {
this.values.set(key, value);
}
}
function browser(matchesDark = false): MemoryStorage {
const storage = new MemoryStorage();
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
value: storage,
});
Object.defineProperty(globalThis, "document", {
configurable: true,
value: { documentElement: { dataset: {} as Record<string, string> } },
});
Object.defineProperty(globalThis, "matchMedia", {
configurable: true,
value: () => ({ matches: matchesDark }),
});
return storage;
}
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
value: new MemoryStorage(),
});
const appearance = await import("../src/stores/appearance.ts");
function freshStore(matchesDark = false): {
storage: MemoryStorage;
store: ReturnType<typeof appearance.useAppearanceStore>;
} {
const storage = browser(matchesDark);
setActivePinia(createPinia());
return { storage, store: appearance.useAppearanceStore() };
}
test("system theme follows the browser preference and remains the stored choice", () => {
const { storage, store } = freshStore(true);
store.setTheme("system");
assert.equal(store.theme, "system");
assert.equal(document.documentElement.dataset.theme, "dark");
assert.equal(storage.getItem(appearance.THEME_KEY), "system");
});
test("density applies immediately and persists", () => {
const { storage, store } = freshStore();
store.setDensity("compact");
assert.equal(store.density, "compact");
assert.equal(document.documentElement.dataset.density, "compact");
assert.equal(storage.getItem(appearance.DENSITY_KEY), "compact");
});
test("reduced motion applies immediately and persists", () => {
const { storage, store } = freshStore();
store.setMotion("reduced");
assert.equal(store.motion, "reduced");
assert.equal(document.documentElement.dataset.motion, "reduced");
assert.equal(storage.getItem(appearance.MOTION_KEY), "reduced");
});