Files
Epic-The-Musical-Timeline/tests/state.test.ts
T
2026-10-30 04:57:36 +01:00

29 lines
1.2 KiB
TypeScript

import { expect, test } from "bun:test";
import { createStore, DEFAULT_STATE, parseState, stateToSearch } from "../src/app/state";
test("a full state round-trips through the URL", () => {
const s = parseState("?song=survive&tab=listen&motif=polyphemus&timeline=closed&ribbons=1&read=1");
expect(s).toEqual({ songId: "survive", tab: "listen", motif: "polyphemus", timeline: "closed", ribbons: true, read: true });
expect(parseState(stateToSearch(s))).toEqual(s);
});
test("unknown song, tab and motif fall back to the defaults", () => {
expect(parseState("?song=nope&tab=x&motif=nobody&timeline=sideways")).toEqual(DEFAULT_STATE);
expect(DEFAULT_STATE.songId).toBe("the-horse-and-the-infant");
});
test("the default state has an empty query", () => {
expect(stateToSearch(DEFAULT_STATE)).toBe("");
});
test("the store notifies subscribers with the new and previous state", () => {
const store = createStore(DEFAULT_STATE, { history: false });
const seen: [string, string][] = [];
const off = store.subscribe((s, prev) => seen.push([prev.songId, s.songId]));
store.set({ songId: "polyphemus" });
off();
store.set({ songId: "storm" });
expect(seen).toEqual([["the-horse-and-the-infant", "polyphemus"]]);
expect(store.get().songId).toBe("storm");
});