Repository navigation
Expand file tree
/
Copy pathWorkspaceDirectory.test.tsx
More file actions
99 lines (97 loc) · 2.92 KB
/
Copy pathWorkspaceDirectory.test.tsx
File metadata and controls
99 lines (97 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { afterEach, expect, it, vi } from "vitest";
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
} from "@testing-library/react";
import WorkspaceDirectory from "./WorkspaceDirectory";
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
});
it.each(["local", "remote"])(
"reads only the selected %s device after opening and returns its actual path",
async (environment) => {
const fetch = vi.fn(async () => ({
ok: true,
json: async () => ({
path: "/device/home",
parent: "/device",
directories: [{ name: "project", path: "/device/home/project" }],
truncated: false,
}),
}));
vi.stubGlobal("fetch", fetch);
const onChange = vi.fn();
render(
<WorkspaceDirectory
value=""
environment={environment}
token="fixture"
disabled={false}
browseEnabled
t={(zh) => zh}
onChange={onChange}
/>,
);
const button = screen.getByRole("button", {
name: environment === "local" ? "选择本机目录" : "选择远端目录",
});
expect(fetch).not.toHaveBeenCalled();
fireEvent.click(button);
await screen.findByRole("button", { name: "project" });
expect(fetch).toHaveBeenCalledWith(
`/api/directories?environment_id=${environment}&path=~`,
expect.any(Object),
);
fireEvent.click(screen.getByRole("button", { name: "使用此目录" }));
expect(onChange).toHaveBeenCalledWith("/device/home");
expect(screen.queryByRole("dialog")).toBeNull();
fireEvent.click(button);
fireEvent.click(button);
expect(screen.queryByRole("dialog")).toBeNull();
fireEvent.click(button);
fireEvent.click(screen.getByRole("button", { name: "关闭目录选择" }));
expect(screen.queryByRole("dialog")).toBeNull();
fireEvent.click(button);
fireEvent.keyDown(document, { key: "Escape" });
expect(screen.queryByRole("dialog")).toBeNull();
},
);
it("blocks selection on a failed read and supports retrying the same path", async () => {
const fetch = vi
.fn()
.mockRejectedValueOnce(new Error("offline"))
.mockResolvedValue({
ok: true,
json: async () => ({
path: "/remote",
parent: "/",
directories: [],
truncated: false,
}),
});
vi.stubGlobal("fetch", fetch);
render(
<WorkspaceDirectory
value="/remote"
environment="remote"
token="fixture"
disabled={false}
browseEnabled
t={(zh) => zh}
onChange={vi.fn()}
/>,
);
fireEvent.click(screen.getByRole("button", { name: "选择远端目录" }));
await screen.findByRole("alert");
expect(
(screen.getByRole("button", { name: "使用此目录" }) as HTMLButtonElement)
.disabled,
).toBe(true);
fireEvent.click(screen.getByRole("button", { name: "前往" }));
await screen.findByText("此目录没有子目录");
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
});