forked from bots-garden/ori
| ✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme | 1 | import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | import { | |
| 3 | DEFAULT_THEME, | |
| 4 | THEME_KEY, | |
| 5 | isTheme, | |
| 6 | resetTheme, | |
| 7 | setTheme, | |
| 8 | toggleTheme, | |
| 9 | useTheme, | |
| 10 | } from "./theme"; | |
| 11 | ||
| 12 | describe("theme store", () => { | |
| 13 | beforeEach(() => { | |
| 14 | localStorage.clear(); | |
| 15 | resetTheme(); | |
| 16 | }); | |
| 17 | ||
| 18 | it("starts light and stamps the document", () => { | |
| 19 | expect(useTheme.getState().theme).toBe(DEFAULT_THEME); | |
| 20 | expect(document.documentElement.dataset.theme).toBe("light"); | |
| 21 | }); | |
| 22 | ||
| 23 | it("setTheme applies, stamps and remembers the theme", () => { | |
| 24 | setTheme("dark"); | |
| 25 | expect(useTheme.getState().theme).toBe("dark"); | |
| 26 | expect(document.documentElement.dataset.theme).toBe("dark"); | |
| 27 | expect(localStorage.getItem(THEME_KEY)).toBe("dark"); | |
| 28 | }); | |
| 29 | ||
| 30 | it("toggleTheme alternates between light and dark", () => { | |
| 31 | toggleTheme(); | |
| 32 | expect(useTheme.getState().theme).toBe("dark"); | |
| 33 | toggleTheme(); | |
| 34 | expect(useTheme.getState().theme).toBe("light"); | |
| 35 | expect(document.documentElement.dataset.theme).toBe("light"); | |
| 36 | }); | |
| 37 | ||
| 38 | it("resetTheme goes back to light", () => { | |
| 39 | setTheme("dark"); | |
| 40 | resetTheme(); | |
| 41 | expect(useTheme.getState().theme).toBe("light"); | |
| 42 | expect(localStorage.getItem(THEME_KEY)).toBe("light"); | |
| 43 | }); | |
| 44 | }); | |
| 45 | ||
| 46 | describe("isTheme", () => { | |
| 47 | it("accepts the two themes and rejects anything else", () => { | |
| 48 | expect(isTheme("light")).toBe(true); | |
| 49 | expect(isTheme("dark")).toBe(true); | |
| 50 | expect(isTheme("system")).toBe(false); | |
| 51 | expect(isTheme(null)).toBe(false); | |
| 52 | expect(isTheme(42)).toBe(false); | |
| 53 | }); | |
| 54 | }); |