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

ThemeImportDialog.browser.test.tsx

114 lines3.6 KB

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 THEME_FILE_VERSION,
13} from "../../themePalette";
14import type { OpenVsxThemeExtension } from "../../openVsxThemes";
15import { ThemeImportDialog } from "./ThemeImportDialog";
16​
17const searchOpenVsxThemes = vi.hoisted(() => vi.fn());
18​
19vi.mock("../../openVsxThemes", () => ({
20 searchOpenVsxThemes,
21 importOpenVsxThemeExtension: vi.fn(),
22}));
23​
24const extension: OpenVsxThemeExtension = {
25 id: "acme.nightfall",
26 collectionId: "acme.nightfall",
27 name: "Nightfall",
28 publisher: "acme",
29 description: "A dark theme",
30 downloadCount: 10,
31 iconUrl: null,
32 sourceUrl: null,
33 manifestUrl: "https://example.test/manifest",
34 sha256Url: "https://example.test/sha256",
35 vsixUrl: "https://example.test/vsix",
36 version: "1.0.0",
37 license: "MIT",
38};
39​
40describe("ThemeImportDialog", () => {
41 // StrictMode replays ref attach/detach on mount, which must not end the session.
42 it("searches Open VSX under StrictMode", async () => {
43 searchOpenVsxThemes.mockReset();
44 searchOpenVsxThemes.mockResolvedValue([extension]);
45 await using _screen = await render(
46 <StrictMode>
47 <ThemeImportDialog
48 open
49 onOpenChange={() => {}}
50 onImported={() => true}
51 onImportedMany={() => {}}
52 />
53 </StrictMode>,
54 );
55​
56 await userEvent.type(page.getByLabelText("Search Open VSX themes"), "night");
57​
58 await expect.element(page.getByRole("heading", { name: "Nightfall" })).toBeVisible();
59 expect(searchOpenVsxThemes).toHaveBeenCalledWith(
60 "night",
61 expect.objectContaining({ sortBy: "downloadCount" }),
62 );
63 });
64});
65​
66const theme = (name: string, accent = "#3b82f6") => ({
67 version: THEME_FILE_VERSION,
68 name,
69 appearance: "dark",
70 colors: { accent },
71});
72​
73const themeFile = (name: string, accent?: string) =>
74 new File([JSON.stringify(theme(name, accent))], `${name}.json`, { type: "application/json" });
75​
76it.each(["Update existing", "Keep both"])(
77 "keeps batch failures visible after %s",
78 async (choice) => {
79 using _themes = {
80 [Symbol.dispose]() {
81 window.localStorage.removeItem(CUSTOM_THEMES_STORAGE_KEY);
82 invalidateCustomThemes();
83 },
84 };
85 installCustomTheme(parseThemeFile(theme("Dusk")));
86 const onOpenChange = vi.fn();
87 await using _screen = await render(
88 <ThemeImportDialog
89 open
90 onOpenChange={onOpenChange}
91 onImported={() => true}
92 onImportedMany={() => {}}
93 />,
94 );
95 const input = document.querySelector<HTMLInputElement>('input[type="file"]')!;
96 await userEvent.upload(input, [
97 themeFile("Dawn"),
98 themeFile("Dusk", "#f97316"),
99 new File(["{"], "broken.json", { type: "application/json" }),
100 ]);
101​
102 await expect.element(page.getByText("Already installed")).toBeVisible();
103 await expect.element(page.getByText(/^broken\.json:/)).toBeVisible();
104 expect(getCustomThemes().map((theme) => theme.label)).toContain("Dawn");
105 await page.getByRole("button", { name: choice }).click();
106 invalidateCustomThemes();
107 const label = choice === "Update existing" ? "Dusk" : "Dusk (1)";
108 expect(getCustomThemes().find((theme) => theme.label === label)?.colors).toEqual(
109 parseThemeFile(theme("Dusk", "#f97316")).colors,
110 );
111 await expect.element(page.getByText(/^broken\.json:/)).toBeVisible();
112 expect(onOpenChange).not.toHaveBeenCalledWith(false);
113 },
114);