Open History to see the last commit
1import { StrictMode } from "react";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4
5import { render } from "../../../test/render";
6import {
7 CUSTOM_THEMES_STORAGE_KEY,
8 getCustomThemes,
9 installCustomTheme,
10 invalidateCustomThemes,
11 parseThemeFile,
12 themeColorToHex,
13 THEME_FILE_VERSION,
14} from "../../themePalette";
15import type { OpenVsxThemeExtension } from "../../openVsxThemes";
16import { ThemeImportDialog } from "./ThemeImportDialog";
17
18const searchOpenVsxThemes = vi.hoisted(() => vi.fn());
19
20vi.mock("../../openVsxThemes", () => ({
21 searchOpenVsxThemes,
22 importOpenVsxThemeExtension: vi.fn(),
23}));
24
25const extension: OpenVsxThemeExtension = {
26 id: "acme.nightfall",
27 collectionId: "acme.nightfall",
28 name: "Nightfall",
29 publisher: "acme",
30 description: "A dark theme",
31 downloadCount: 10,
32 iconUrl: null,
33 sourceUrl: null,
34 manifestUrl: "https://example.test/manifest",
35 sha256Url: "https://example.test/sha256",
36 vsixUrl: "https://example.test/vsix",
37 version: "1.0.0",
38 license: "MIT",
39};
40
41describe("ThemeImportDialog", () => {
42 // StrictMode replays ref attach/detach on mount, which must not end the session.
43 it("searches Open VSX under StrictMode", async () => {
44 searchOpenVsxThemes.mockReset();
45 searchOpenVsxThemes.mockResolvedValue([extension]);
46 await using _screen = await render(
47 <StrictMode>
48 <ThemeImportDialog
49 open
50 onOpenChange={() => {}}
51 onImported={() => true}
52 onImportedMany={() => {}}
53 />
54 </StrictMode>,
55 );
56
57 await userEvent.type(page.getByLabelText("Search Open VSX themes"), "night");
58
59 await expect.element(page.getByRole("heading", { name: "Nightfall" })).toBeVisible();
60 expect(searchOpenVsxThemes).toHaveBeenCalledWith(
61 "night",
62 expect.objectContaining({ sortBy: "downloadCount" }),
63 );
64 });
65});
66
67const theme = (name: string, accent = "#3b82f6") => ({
68 version: THEME_FILE_VERSION,
69 name,
70 appearance: "dark",
71 colors: { accent },
72});
73
74const themeFile = (name: string, accent?: string) =>
75 new File([JSON.stringify(theme(name, accent))], `${name}.json`, { type: "application/json" });
76
77it.each(["Update existing", "Keep both"])(
78 "keeps batch failures visible after %s",
79 async (choice) => {
80 using _themes = {
81 [Symbol.dispose]() {
82 window.localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
83 invalidateCustomThemes();
84 },
85 };
86 installCustomTheme(parseThemeFile(theme("Dusk")));
87 const onOpenChange = vi.fn();
88 await using _screen = await render(
89 <ThemeImportDialog
90 open
91 onOpenChange={onOpenChange}
92 onImported={() => true}
93 onImportedMany={() => {}}
94 />,
95 );
96 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
97 await userEvent.upload(input, [
98 themeFile("Dawn"),
99 themeFile("Dusk", "#f97316"),
100 new File(["{"], "broken.json", { type: "application/json" }),
101 ]);
102
103 await expect.element(page.getByText("Already installed")).toBeVisible();
104 await expect.element(page.getByText(/^broken\.json:/)).toBeVisible();
105 expect(getCustomThemes().map((theme) => theme.label)).toContain("Dawn");
106 await page.getByRole("button", { name: choice }).click();
107 invalidateCustomThemes();
108 const label = choice === "Update existing" ? "Dusk" : "Dusk (1)";
109 expect(getCustomThemes().find((theme) => theme.label === label)?.colors).toEqual(
110 parseThemeFile(theme("Dusk", "#f97316")).colors,
111 );
112 await expect.element(page.getByText(/^broken\.json:/)).toBeVisible();
113 expect(onOpenChange).not.toHaveBeenCalledWith(false);
114 },
115);
116
117function isolateThemes() {
118 const previous = localStorage.getItem(CUSTOM_THEMES_STORAGE_KEY);
119 localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
120 invalidateCustomThemes();
121 return {
122 [Symbol.dispose]() {
123 if (previous === null) localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
124 else localStorage.setItem(CUSTOM_THEMES_STORAGE_KEY, previous);
125 invalidateCustomThemes();
126 },
127 };
128}
129
130function themeJsonc(name: string, background: string) {
131 return `{
132 // VS Code themes use JSONC even with a .json extension.
133 "name": "${name}",
134 "colors": {
135 "editor.background": "${background}",
136 },
137 "tokenColors": [],
138 }`;
139}
140
141describe("ThemeImportDialog JSONC imports", () => {
142 it.each(["paste", "file"])("imports a VS Code JSONC theme from %s", async (source) => {
143 using _themes = isolateThemes();
144 await using _screen = await render(
145 <ThemeImportDialog
146 open
147 onOpenChange={() => {}}
148 onImported={() => true}
149 onImportedMany={() => {}}
150 />,
151 );
152 const json = themeJsonc("JSONC Night", "#002b36");
153 if (source === "paste") {
154 await page.getByRole("textbox", { name: "Theme JSON" }).fill(json);
155 } else {
156 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
157 await userEvent.upload(input, new File([json], "night.json", { type: "application/json" }));
158 await expect.element(page.getByRole("textbox", { name: "Theme JSON" })).toHaveValue(json);
159 }
160 await page.getByRole("button", { name: "Add theme", exact: true }).click();
161
162 expect(getCustomThemes().map((theme) => theme.label)).toEqual(["JSONC Night"]);
163 expect(themeColorToHex(getCustomThemes()[0]!.colors.canvas)).toBe("#002b36");
164 });
165
166 it("imports each VS Code JSONC theme in a file batch", async () => {
167 using _themes = isolateThemes();
168 await using _screen = await render(
169 <ThemeImportDialog
170 open
171 onOpenChange={() => {}}
172 onImported={() => true}
173 onImportedMany={() => {}}
174 />,
175 );
176 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
177 await userEvent.upload(input, [
178 new File([themeJsonc("JSONC Night", "#002b36")], "night.json", {
179 type: "application/json",
180 }),
181 new File([themeJsonc("JSONC Day", "#fdf6e3")], "day.json", {
182 type: "application/json",
183 }),
184 ]);
185
186 await vi.waitFor(() => {
187 expect(getCustomThemes().map((theme) => theme.label)).toEqual(["JSONC Night", "JSONC Day"]);
188 });
189 expect(getCustomThemes().map((theme) => themeColorToHex(theme.colors.canvas))).toEqual([
190 "#002b36",
191 "#fdf6e3",
192 ]);
193 });
194});