import assert from "assert"; import { store, stored } from "./theme.js"; // localStorage as far as theme.js uses it, holding `entries`. function storage(entries = {}) { const items = new Map(Object.entries(entries)); return { items, getItem: (key) => items.get(key) ?? null, setItem: (key, value) => items.set(key, value), removeItem: (key) => items.delete(key), }; } // What the browser hands over when the page may not use storage at all. const refused = () => { throw new DOMException("refused", "SecurityError"); }; Deno.test("D14: with nothing kept, the page follows the system", () => { const kept = storage(); assert.equal(stored(() => kept), "system"); }); Deno.test("D14: a kept light or dark is the choice", () => { const light = storage({ theme: "light" }); const dark = storage({ theme: "dark" }); assert.equal(stored(() => light), "light"); assert.equal(stored(() => dark), "dark"); }); Deno.test("D14: a kept value that is no choice follows the system", () => { const junk = storage({ theme: "blue" }); const system = storage({ theme: "system" }); assert.equal(stored(() => junk), "system"); assert.equal(stored(() => system), "system"); }); Deno.test("D14: storage that cannot be read follows the system", () => { assert.equal(stored(refused), "system"); }); Deno.test("D14: choosing light or dark keeps it", () => { const kept = storage(); store(() => kept, "dark"); assert.deepEqual([...kept.items], [["theme", "dark"]]); store(() => kept, "light"); assert.deepEqual([...kept.items], [["theme", "light"]]); }); Deno.test("D14: choosing the system keeps nothing", () => { const kept = storage({ theme: "dark" }); store(() => kept, "system"); assert.deepEqual([...kept.items], []); }); Deno.test("D14: storage that cannot be written does not stop the choice", () => { assert.doesNotThrow(() => store(refused, "dark")); const full = { ...storage(), setItem: refused }; assert.doesNotThrow(() => store(() => full, "dark")); });