projectEditorLayout.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
  4. import { afterEach, expect, it, vi } from "vitest";
  5. import { ProjectEditor } from "@/project-sidebar/ProjectEditor.js";
  6. import type { ProjectDevice } from "@/project-sidebar/contract.js";
  7. import zhCN from "@/i18n/locales/zh-CN.js";
  8. Element.prototype.scrollIntoView = vi.fn();
  9. vi.mock("@/i18n/IntlProvider.js", () => ({
  10. useZCodeIntl: () => ({
  11. intl: {
  12. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) =>
  13. Object.entries(values ?? {}).reduce(
  14. (text, [key, value]) => text.replace(`{${key}}`, value),
  15. (zhCN as Record<string, string>)[id] ?? id,
  16. ),
  17. },
  18. }),
  19. }));
  20. vi.mock("@/hooks/usePlatform.js", () => ({ useOptionalPlatform: () => null }));
  21. vi.mock("@/hooks/useConfirmDialog.js", () => ({ useConfirmDialog: () => vi.fn(async () => true) }));
  22. vi.mock("@/project-sidebar/useProjectDirectoryServices.js", () => ({
  23. useProjectDirectoryServices: () => ({ services: {} }),
  24. }));
  25. vi.mock("@/RemoteConnectionDialogContent.js", () => ({
  26. useRemoteBoundaryAcknowledgement: () => async () => true,
  27. }));
  28. afterEach(cleanup);
  29. const devices: ProjectDevice[] = [
  30. { id: "remote:offline", label: "Offline box", status: "offline" },
  31. { id: "remote:online", label: "Online box", status: "online" },
  32. { id: "remote:connecting", label: "Connecting box", status: "connecting" },
  33. { id: "remote:pending", label: "Pending box", status: "pending" },
  34. { id: "remote:failed", label: "Failed box", status: "failed" },
  35. ];
  36. function editor(defaultDeviceId = "remote:offline") {
  37. const onBrowseExisting = vi.fn(async () => true);
  38. render(
  39. createElement(ProjectEditor, {
  40. project: null,
  41. projects: [],
  42. devices,
  43. defaultDeviceId,
  44. service: { readTree: vi.fn(async () => ({ projects: [], tasks: [] })) } as never,
  45. remoteService: {} as never,
  46. onBrowseExisting,
  47. onAddDevice: vi.fn(),
  48. onClose: vi.fn(),
  49. onSaved: vi.fn(async () => {}),
  50. }),
  51. );
  52. return { onBrowseExisting };
  53. }
  54. const before = (a: Element, b: Element) =>
  55. Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING);
  56. it("orders fields as name, device, then folders", () => {
  57. editor();
  58. const device = screen.getByTestId("virtual-project-device");
  59. const name = screen.getByTestId("virtual-project-name");
  60. const browse = screen.getByTestId("virtual-project-browse");
  61. expect(before(name, device)).toBe(true);
  62. expect(before(device, browse)).toBe(true);
  63. });
  64. it("shows a status dot for every remote device and text only when it is not online", async () => {
  65. editor();
  66. const trigger = screen.getByTestId("virtual-project-device");
  67. const triggerStatus = within(trigger).getByTestId("virtual-project-device-status");
  68. expect(triggerStatus.dataset.status).toBe("offline");
  69. expect(triggerStatus.textContent).toBe("离线");
  70. fireEvent.click(trigger);
  71. const option = async (label: string) =>
  72. within(await screen.findByRole("option", { name: new RegExp(label) })).queryByTestId(
  73. "virtual-project-device-status",
  74. );
  75. expect(await option("此电脑")).toBeNull();
  76. const online = (await option("Online box"))!;
  77. expect(online.querySelector(".bg-success")).toBeTruthy();
  78. // 在线只保留圆点,文字只给读屏。
  79. expect(online.querySelector(".sr-only")?.textContent).toBe("在线");
  80. const connecting = (await option("Connecting box"))!;
  81. expect(connecting.querySelector(".animate-spin")).toBeTruthy();
  82. expect(connecting.textContent).toBe("连接中");
  83. expect((await option("Pending box"))!.querySelector(".bg-warning")).toBeTruthy();
  84. expect((await option("Pending box"))!.textContent).toBe("需要处理");
  85. expect((await option("Failed box"))!.querySelector(".bg-destructive")).toBeTruthy();
  86. expect((await option("Failed box"))!.textContent).toBe("连接失败");
  87. expect(await screen.findByRole("option", { name: "添加远程设备…" })).toBeTruthy();
  88. });
  89. it("remote existing-project entry returns focus after browsing", async () => {
  90. const { onBrowseExisting } = editor();
  91. const entry = screen.getByTestId("virtual-project-existing");
  92. expect(entry.textContent).toBe("添加到侧栏");
  93. expect(entry.closest("p")?.textContent).toContain("这台设备上已有项目?");
  94. expect(before(screen.getByTestId("virtual-project-device"), entry)).toBe(true);
  95. expect(before(screen.getByTestId("virtual-project-name"), entry)).toBe(true);
  96. expect(before(entry, screen.getByTestId("virtual-project-browse"))).toBe(true);
  97. fireEvent.click(entry);
  98. await waitFor(() => expect(onBrowseExisting).toHaveBeenCalledOnce());
  99. const back = await screen.findByTestId("remote-project-back");
  100. expect(back.getAttribute("aria-label")).toBe("返回新建项目");
  101. expect(screen.queryByText("返回新建项目")).toBeNull();
  102. fireEvent.click(back);
  103. await waitFor(() =>
  104. expect(document.activeElement).toBe(screen.getByTestId("virtual-project-existing")),
  105. );
  106. });
  107. it("hides the existing-project entry for this computer", () => {
  108. editor("local");
  109. expect(screen.queryByTestId("virtual-project-existing")).toBeNull();
  110. });
  111. it("explains connection for a device that is not online", () => {
  112. editor();
  113. const browse = screen.getByTestId("virtual-project-browse");
  114. expect(browse.getAttribute("aria-label")).toBe("连接并选择文件夹");
  115. expect(browse.textContent).toContain("设备当前离线,点击后先连接");
  116. });
  117. it("uses the choose-folder empty state for an online device", () => {
  118. editor("remote:online");
  119. const browse = screen.getByTestId("virtual-project-browse");
  120. expect(browse.getAttribute("aria-label")).toBe("选择文件夹");
  121. expect(browse.textContent).toContain("ZCode 可读取和编辑这些文件夹");
  122. });