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

FileBrowserPanel.browser.test.tsx

173 lines6.2 KB

1import { EnvironmentId, type ProjectEntry } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4​
5import { COMPOSER_MENTION_DRAG_TYPE } from "~/components/chat/composerMentionDrag";
6import { dismissContextMenu } from "~/contextMenuFallback";
7import { isMacPlatform } from "~/lib/utils";
8import { render } from "../../../test/render";
9import FileBrowserPanel from "./FileBrowserPanel";
10import { dispatchPreviewAction } from "../preview/previewActionBus";
11​
12const entries: readonly ProjectEntry[] = [
13 { path: "src", kind: "directory" },
14 { path: "README.md", kind: "file" },
15];
16const loadingDirectories: ReadonlySet<string> = new Set();
17const directory = {
18 entries,
19 load: () => Promise.resolve(),
20 refresh: () => {},
21 ready: true,
22 error: null,
23 isPending: false,
24 loadingDirectories,
25};
26const pathSearch = {
27 entries: [],
28 isPending: false,
29 error: null,
30 truncated: false,
31 refresh: () => {},
32};
33​
34vi.mock("./useDirectoryEntries", () => ({ useDirectoryEntries: () => directory }));
35vi.mock("~/state/queries", () => ({ useProjectPathSearch: () => pathSearch }));
36vi.mock("~/fileContextMenu", () => ({
37 useFileContextMenu: () => ({ buildItems: () => [], activate: () => Promise.resolve() }),
38}));
39vi.mock("~/hooks/useWorkspaceMutationRefresh", () => ({ useWorkspaceMutationRefresh: () => {} }));
40​
41function findTreeRow(path: string): HTMLElement | null {
42 for (const host of document.querySelectorAll("*")) {
43 const row = host.shadowRoot?.querySelector<HTMLElement>(`[data-item-path="${path}"]`);
44 if (row) return row;
45 }
46 return null;
48​
49describe("FileBrowserPanel", () => {
50 it("copies with the pointer and returns to the row after keyboard dismissal", async () => {
51 await using _screen = await render(
52 <FileBrowserPanel
53 environmentId={EnvironmentId.make("env-files")}
54 cwd="/repo"
55 projectName="repo"
56 selectedPath={null}
57 selectedPathRevealId={0}
58 onOpenFile={() => {}}
59 workspaceMutationId={null}
60 />,
61 );
62 using _menu = { [Symbol.dispose]: () => dismissContextMenu() };
63 using clipboard = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue(undefined);
64 await vi.waitFor(() => expect(findTreeRow("README.md")).not.toBeNull());
65 findTreeRow("README.md")!.dispatchEvent(
66 new MouseEvent("contextmenu", {
67 bubbles: true,
68 cancelable: true,
69 composed: true,
70 button: 2,
71 }),
72 );
73 const action = page.getByRole("menuitem", { name: "Copy mention", exact: true });
74 await expect.element(action).toBeVisible();
75 await userEvent.click(action);
76 await vi.waitFor(() => expect(clipboard).toHaveBeenCalledWith("[README.md](README.md)"));
77​
78 const row = findTreeRow("README.md")!;
79 await userEvent.keyboard("{Tab}");
80 row.focus();
81 await userEvent.keyboard("{Shift>}{F10}{/Shift}");
82 await expect.element(action).toHaveFocus();
83 await userEvent.keyboard("{Escape}");
84 await vi.waitFor(() => expect((row.getRootNode() as ShadowRoot).activeElement).toBe(row));
85 });
86​
87 it("dragging a file row carries a composer mention without opening it, and dragend releases it", async () => {
88 const onOpenFile = vi.fn();
89 await using _screen = await render(
90 <FileBrowserPanel
91 environmentId={EnvironmentId.make("env-files")}
92 cwd="/repo"
93 projectName="repo"
94 selectedPath={null}
95 selectedPathRevealId={0}
96 onOpenFile={onOpenFile}
97 workspaceMutationId={null}
98 />,
99 );
100 await vi.waitFor(() => expect(findTreeRow("README.md")).not.toBeNull());
101 const row = findTreeRow("README.md")!;
102​
103 const dataTransfer = new DataTransfer();
104 row.dispatchEvent(
105 new DragEvent("dragstart", { bubbles: true, cancelable: true, composed: true, dataTransfer }),
106 );
107 // The tree selects the row the drag picked up; mid-drag that must not open it.
108 row.click();
109 await vi.waitFor(() => expect(row.getAttribute("aria-selected")).toBe("true"));
110​
111 expect(dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE)).toContain("README.md");
112 expect(onOpenFile).not.toHaveBeenCalled();
113​
114 row.dispatchEvent(new DragEvent("dragend", { bubbles: true, composed: true, dataTransfer }));
115 await vi.waitFor(() => expect(row.getAttribute("aria-selected")).not.toBe("true"));
116​
117 row.click();
118 await vi.waitFor(() => expect(onOpenFile).toHaveBeenCalledTimes(1));
119 });
120​
121 it("mod+f from a tree row, or the panel find command, focuses the file search", async () => {
122 await using _screen = await render(
123 <FileBrowserPanel
124 environmentId={EnvironmentId.make("env-files")}
125 cwd="/repo"
126 projectName="repo"
127 selectedPath={null}
128 selectedPathRevealId={0}
129 onOpenFile={() => {}}
130 workspaceMutationId={null}
131 answersPanelFind
132 />,
133 );
134 await vi.waitFor(() => expect(findTreeRow("README.md")).not.toBeNull());
135 findTreeRow("README.md")!.focus();
136​
137 const modifier = isMacPlatform(navigator.platform) ? "Meta" : "Control";
138 await userEvent.keyboard(`{${modifier}>}f{/${modifier}}`);
139​
140 const search = page.getByRole("searchbox", { name: "Search repo files" });
141 await expect.element(search).toHaveFocus();
142​
143 (search.element() as HTMLElement).blur();
144 dispatchPreviewAction("find");
145 await expect.element(search).toHaveFocus();
146 });
147​
148 it("keeps expanded folders when the panel remounts", async () => {
149 const panel = () => (
150 <FileBrowserPanel
151 environmentId={EnvironmentId.make("env-files")}
152 cwd="/repo"
153 projectName="repo"
154 selectedPath={null}
155 selectedPathRevealId={0}
156 onOpenFile={() => {}}
157 workspaceMutationId={null}
158 />
159 );
160 {
161 await using _first = await render(panel());
162 await vi.waitFor(() => expect(findTreeRow("src/")).not.toBeNull());
163 findTreeRow("src/")!.click();
164 await vi.waitFor(() =>
165 expect(findTreeRow("src/")!.getAttribute("aria-expanded")).toBe("true"),
166 );
167 }
168​
169 await using _second = await render(panel());
170 await vi.waitFor(() => expect(findTreeRow("src/")).not.toBeNull());
171 await vi.waitFor(() => expect(findTreeRow("src/")!.getAttribute("aria-expanded")).toBe("true"));
172 });
173});