|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { act } from "react"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import { ThemeToggle } from "../components/ThemeToggle"; |
| 7 | +import { cleanupMounted, mountHost } from "../components/ui/mountHost.testHelpers"; |
| 8 | +import { savedStudioTheme, shownStudioTheme } from "./studioTheme"; |
| 9 | + |
| 10 | +const KEY = "hf-studio-ui-preferences"; |
| 11 | +const page = new DOMParser().parseFromString( |
| 12 | + readFileSync(path.join(__dirname, "../../index.html"), "utf8"), |
| 13 | + "text/html", |
| 14 | +); |
| 15 | +const scripts = [...page.querySelectorAll("script")]; |
| 16 | +const boot = scripts.find((script) => !script.type)?.textContent ?? ""; |
| 17 | + |
| 18 | +function store(theme: unknown) { |
| 19 | + localStorage.setItem(KEY, JSON.stringify({ theme })); |
| 20 | +} |
| 21 | + |
| 22 | +function boots(): "light" | "dark" { |
| 23 | + new Function(boot)(); |
| 24 | + return shownStudioTheme(); |
| 25 | +} |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + cleanupMounted(); |
| 29 | + localStorage.clear(); |
| 30 | + delete document.documentElement.dataset.theme; |
| 31 | +}); |
| 32 | + |
| 33 | +describe("Studio's theme", () => { |
| 34 | + it.each([["light"], ["dark"], [undefined], ["neon"]])( |
| 35 | + "boots to the theme the owner picks (saved %s)", |
| 36 | + (saved) => { |
| 37 | + store(saved); |
| 38 | + expect(boots()).toBe(savedStudioTheme()); |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + it.each([["null"], ["{not json"], ['"dark"']])("boots to the owner's pick from %s", (raw) => { |
| 43 | + localStorage.setItem(KEY, raw); |
| 44 | + expect(boots()).toBe(savedStudioTheme()); |
| 45 | + }); |
| 46 | + |
| 47 | + it("opens light the first time", () => { |
| 48 | + expect(boots()).toBe("light"); |
| 49 | + expect(savedStudioTheme()).toBe("light"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("runs the boot script before the app's module", () => { |
| 53 | + expect(boot).toContain(`"${KEY}"`); |
| 54 | + expect(scripts.findIndex((script) => !script.type)).toBeLessThan( |
| 55 | + scripts.findIndex((script) => script.type === "module"), |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it("flips the document between light and dark and keeps the choice", async () => { |
| 60 | + store("dark"); |
| 61 | + const host = mountHost(<ThemeToggle />); |
| 62 | + const button = () => host.querySelector("button")!; |
| 63 | + expect(button().getAttribute("aria-label")).toBe("Switch to light theme"); |
| 64 | + |
| 65 | + await act(async () => button().click()); |
| 66 | + expect(document.documentElement.dataset.theme).toBe("paper"); |
| 67 | + expect(JSON.parse(localStorage.getItem(KEY)!).theme).toBe("light"); |
| 68 | + expect(button().getAttribute("aria-label")).toBe("Switch to dark theme"); |
| 69 | + |
| 70 | + await act(async () => button().click()); |
| 71 | + expect(document.documentElement.dataset.theme).toBeUndefined(); |
| 72 | + expect(JSON.parse(localStorage.getItem(KEY)!).theme).toBe("dark"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("follows the theme on screen, not only its own clicks", async () => { |
| 76 | + store("light"); |
| 77 | + const host = mountHost(<ThemeToggle />); |
| 78 | + expect(host.querySelector("button")!.getAttribute("aria-label")).toBe("Switch to light theme"); |
| 79 | + await act(async () => { |
| 80 | + document.documentElement.dataset.theme = "paper"; |
| 81 | + }); |
| 82 | + expect(host.querySelector("button")!.getAttribute("aria-label")).toBe("Switch to dark theme"); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("the reveal", () => { |
| 87 | + function stubTransition() { |
| 88 | + const animate = vi.fn(); |
| 89 | + Object.assign(document.documentElement, { animate }); |
| 90 | + const markers: (string | undefined)[] = []; |
| 91 | + const finish: (() => void)[] = []; |
| 92 | + const start = vi.fn((update: () => void) => { |
| 93 | + markers.push(document.documentElement.dataset.themeReveal); |
| 94 | + update(); |
| 95 | + return { ready: Promise.resolve(), finished: new Promise<void>((done) => finish.push(done)) }; |
| 96 | + }); |
| 97 | + Object.assign(document, { startViewTransition: start }); |
| 98 | + return { animate, start, markers, finish }; |
| 99 | + } |
| 100 | + |
| 101 | + afterEach(() => { |
| 102 | + Reflect.deleteProperty(document, "startViewTransition"); |
| 103 | + Reflect.deleteProperty(document.documentElement, "animate"); |
| 104 | + vi.unstubAllGlobals(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("grows from the button's centre when the keyboard presses it, marked while it runs", async () => { |
| 108 | + const { animate, markers, finish } = stubTransition(); |
| 109 | + vi.stubGlobal("innerWidth", 1000); |
| 110 | + vi.stubGlobal("innerHeight", 500); |
| 111 | + const button = mountHost(<ThemeToggle />).querySelector("button")!; |
| 112 | + button.getBoundingClientRect = () => ({ left: 890, top: 10, width: 20, height: 20 }) as DOMRect; |
| 113 | + await act(async () => { |
| 114 | + button.dispatchEvent(new MouseEvent("click", { bubbles: true, detail: 0 })); |
| 115 | + }); |
| 116 | + expect(markers).toEqual([""]); |
| 117 | + expect(animate.mock.calls[0]![0].clipPath[1]).toMatch(/^circle\(129\.0\d*% at 90% 4%\)$/); |
| 118 | + await act(async () => finish[0]!()); |
| 119 | + expect(document.documentElement.dataset.themeReveal).toBeUndefined(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("keeps the marker for a second reveal that starts before the first ends", async () => { |
| 123 | + const { finish } = stubTransition(); |
| 124 | + const button = mountHost(<ThemeToggle />).querySelector("button")!; |
| 125 | + await act(async () => button.click()); |
| 126 | + await act(async () => button.click()); |
| 127 | + await act(async () => finish[0]!()); |
| 128 | + expect(document.documentElement.dataset.themeReveal).toBe(""); |
| 129 | + await act(async () => finish[1]!()); |
| 130 | + expect(document.documentElement.dataset.themeReveal).toBeUndefined(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("switches without the reveal under reduced motion", async () => { |
| 134 | + const { start } = stubTransition(); |
| 135 | + vi.stubGlobal("matchMedia", (query: string) => ({ matches: query.includes("reduce") })); |
| 136 | + const button = mountHost(<ThemeToggle />).querySelector("button")!; |
| 137 | + await act(async () => button.click()); |
| 138 | + expect(start).not.toHaveBeenCalled(); |
| 139 | + expect(document.documentElement.dataset.theme).toBe("paper"); |
| 140 | + }); |
| 141 | +}); |
0 commit comments