Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThemeEditorHost.browser.test.tsx

138 lines4.2 KB

1import { act } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import {
6 installCustomTheme,
7 invalidateCustomThemes,
8 parseThemeFile,
9 removeCustomTheme,
10 THEME_FILE_VERSION,
11 themeColorToHex,
12 updateCustomTheme,
13 type ThemeDefinition,
14} from "../../themePalette";
15​
16vi.mock(import("../../hooks/useTheme"), async (original) => ({
17 ...(await original()),
18 useTheme: () =>
19 ({ theme: "system", themeHalves: null, setTheme: vi.fn(), refreshTheme: vi.fn() }) as never,
20}));
21// The real panel is a large lazy editor; this stand-in shows which theme the host hands it.
22vi.mock(import("./ThemeEditorPanel"), async (original) => ({
23 ...(await original()),
24 ThemeEditorPanel: (({
25 editingTheme,
26 seedTheme,
27 }: {
28 editingTheme: ThemeDefinition | null;
29 seedTheme: ThemeDefinition | null;
30 }) => (
31 <dl>
32 <dt>editingTheme</dt>
33 <dd>{editingTheme ? themeColorToHex(editingTheme.colors.accent ?? "") : "none"}</dd>
34 <dt>seedTheme</dt>
35 <dd>{seedTheme ? themeColorToHex(seedTheme.colors.accent ?? "") : "none"}</dd>
36 </dl>
37 )) as never,
38}));
39​
40import { ThemeEditorHost } from "./ThemeEditorHost";
41import { useThemeEditorStore } from "./themeEditorStore";
42​
43type Field = "editingTheme" | "seedTheme";
44let root: Root;
45let container: HTMLDivElement;
46​
47function install(id: string) {
48 return installCustomTheme(
49 parseThemeFile({
50 version: THEME_FILE_VERSION,
51 id,
52 name: id,
53 appearance: "dark",
54 colors: { accent: "#1f6e4a" },
55 }),
56 );
58function open(field: Field, themeId: string) {
59 return act(async () =>
60 useThemeEditorStore.getState().openThemeEditor({
61 editingThemeId: field === "editingTheme" ? themeId : null,
62 seedThemeId: field === "seedTheme" ? themeId : null,
63 seedName: null,
64 initialAppearance: "dark",
65 }),
66 );
68async function shown(field: Field) {
69 await vi.waitFor(() => expect(container.querySelector("dl")).not.toBeNull(), {
70 timeout: 5_000,
71 });
72 const terms = [...container.querySelectorAll("dt")];
73 return terms.find((term) => term.textContent === field)?.nextElementSibling?.textContent;
75​
76// The host lazy-loads the panel; load it once so the first test does not race the import.
77beforeAll(() => import("./ThemeEditorPanel"));
78​
79beforeEach(async () => {
80 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
81 localStorage.clear();
82 invalidateCustomThemes();
83 useThemeEditorStore.setState({ session: null });
84 container = document.createElement("div");
85 document.body.append(container);
86 root = createRoot(container);
87 await act(async () => root.render(<ThemeEditorHost />));
88});
89afterEach(async () => {
90 await act(async () => root.unmount());
91 container.remove();
92 localStorage.clear();
93 invalidateCustomThemes();
94 vi.unstubAllGlobals();
95});
96​
97describe("ThemeEditorHost", () => {
98 it.each(["editingTheme", "seedTheme"] as const)(
99 "reopens the same %s with its saved colors",
100 async (field) => {
101 const theme = install("saved-colors");
102 await open(field, theme.id);
103 expect(await shown(field)).toBe("#1f6e4a");
104​
105 await act(async () => useThemeEditorStore.getState().closeThemeEditor());
106 expect(container.querySelector("dl")).toBeNull();
107 updateCustomTheme({ ...theme, colors: { ...theme.colors, accent: "#7241b8" } });
108 await open(field, theme.id);
109​
110 expect(await shown(field)).toBe("#7241b8");
111 },
112 );
113​
114 it.each(["editingTheme", "seedTheme"] as const)(
115 "refreshes an open %s when the library changes",
116 async (field) => {
117 const theme = install("updated-theme");
118 await open(field, theme.id);
119 expect(await shown(field)).toBe("#1f6e4a");
120​
121 await act(async () =>
122 updateCustomTheme({ ...theme, colors: { ...theme.colors, accent: "#7241b8" } }),
123 );
124​
125 expect(await shown(field)).toBe("#7241b8");
126 },
127 );
128​
129 it("does not keep editing a theme removed from the library", async () => {
130 const theme = install("removed-theme");
131 await open("editingTheme", theme.id);
132 expect(await shown("editingTheme")).toBe("#1f6e4a");
133​
134 await act(async () => removeCustomTheme(theme.id));
135​
136 expect(await shown("editingTheme")).toBe("none");
137 });
138});