Skip to content
Source
Sign inSign up

thread-file-navigator-pane.test.tsx

79 lines3.0 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { act, type ReactNode } from "react";
3import { createRoot } from "react-dom/client";
4import { expect, it, vi } from "vite-plus/test";
5​
6const queries = vi.hoisted(() => [] as { cwd: string | null; searchQuery: string }[]);
7​
8vi.mock("@t3tools/client-runtime/state/filesystem", () => ({
9 resolveFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false }),
10}));
11vi.mock("../../state/session", () => ({ environmentSession: { sessionStateAtom: () => null } }));
12vi.mock("../../state/query", () => ({ useEnvironmentQuery: () => ({ data: null, error: null }) }));
13vi.mock("../../state/presentation", () => ({
14 useEnvironmentPresentation: () => ({ isReady: true, presentation: null }),
15}));
16vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
17 useAppearancePreferences: () => ({ themeAppearance: "dark" }),
18}));
19vi.mock("./preload-workspace-file", () => ({ preloadWorkspaceFileContents: () => {} }));
20vi.mock("./FileTreeBrowser", () => ({ FileTreeBrowser: () => null }));
21vi.mock("./useFileTreeEntries", () => ({
22 useFileTreeEntries: (input: { cwd: string | null; searchQuery: string }) => {
23 queries.push({ cwd: input.cwd, searchQuery: input.searchQuery });
24 return { refresh: () => {}, selectPath: () => {} };
25 },
26}));
27vi.mock("../../components/ui/SheetPane", () => ({
28 SheetPane: ({ children }: { children: ReactNode }) => children,
29}));
30vi.mock("../../components/ui/ScreenContent", () => ({
31 ScreenContent: ({ children }: { children: ReactNode }) => children,
32}));
33vi.mock("../../components/ui/AppBar", () => ({
34 AppBar: (props: { search: { value: string; onChangeText: (value: string) => void } }) => (
35 <input
36 value={props.search.value}
37 onChange={(event) => props.search.onChangeText(event.target.value)}
38 />
39 ),
40}));
41​
42import { ThreadFileNavigatorPane } from "./thread-file-navigator-pane";
43​
44it("starts unfiltered when the mounted pane switches to another workspace", () => {
45 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
46 const container = document.body.appendChild(document.createElement("div"));
47 const root = createRoot(container);
48 const render = (cwd: string) =>
49 act(() =>
50 root.render(
51 <ThreadFileNavigatorPane
52 cwd={cwd}
53 environmentId={EnvironmentId.make("env")}
54 projectName="Files"
55 selectedPath={null}
56 onSelectFile={() => {}}
57 />,
58 ),
59 );
60 try {
61 render("/first");
62 const input = container.querySelector("input")!;
63 act(() => {
64 Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(
65 input,
66 "alpha",
67 );
68 input.dispatchEvent(new Event("input", { bubbles: true }));
69 });
70 expect(queries.at(-1)).toEqual({ cwd: "/first", searchQuery: "alpha" });
71​
72 render("/second");
73 expect(queries.at(-1)).toEqual({ cwd: "/second", searchQuery: "" });
74 expect(container.querySelector("input")!.value).toBe("");
75 } finally {
76 act(() => root.unmount());
77 container.remove();
78 }
79});