confirmDialogRequiredAcknowledgement.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // @vitest-environment jsdom
  2. import { createElement, type ReactNode } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ConfirmDialogHost } from "@/ConfirmDialog.js";
  6. import { useConfirmDialogStore } from "@/store/confirmDialogStore.js";
  7. import zhCN from "@/i18n/locales/zh-CN.js";
  8. vi.mock("@/i18n/IntlProvider.js", () => ({
  9. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  10. }));
  11. vi.mock("@/components/ui/button.js", () => ({
  12. Button: ({ children, ...props }: { children?: ReactNode; [key: string]: unknown }) =>
  13. createElement("button", props, children),
  14. }));
  15. vi.mock("@/components/ui/dialog.js", () => ({
  16. Dialog: ({
  17. children,
  18. onOpenChange,
  19. }: {
  20. children?: ReactNode;
  21. onOpenChange: (open: boolean) => void;
  22. }) =>
  23. createElement(
  24. "div",
  25. null,
  26. createElement(
  27. "button",
  28. { type: "button", onClick: () => onOpenChange(false) },
  29. "dismiss-dialog",
  30. ),
  31. children,
  32. ),
  33. DialogClose: ({ children }: { children?: ReactNode }) => createElement("div", null, children),
  34. DialogContent: ({ children, ...props }: { children?: ReactNode; [key: string]: unknown }) =>
  35. createElement("section", props, children),
  36. DialogDescription: ({ children }: { children?: ReactNode }) => createElement("p", null, children),
  37. DialogFooter: ({ children }: { children?: ReactNode }) => createElement("footer", null, children),
  38. DialogHeader: ({ children }: { children?: ReactNode }) => createElement("header", null, children),
  39. DialogTitle: ({ children }: { children?: ReactNode }) => createElement("h2", null, children),
  40. }));
  41. afterEach(() => {
  42. cleanup();
  43. useConfirmDialogStore.getState().settleConfirmation(false);
  44. });
  45. describe("ConfirmDialogHost required acknowledgement", () => {
  46. it("ignores overlay/Escape dismissal and renders only the acknowledgement action", async () => {
  47. const result = useConfirmDialogStore.getState().requestConfirmation({
  48. title: "Source Server ready",
  49. confirmLabel: "Got it",
  50. requiredAcknowledgement: true,
  51. });
  52. render(createElement(ConfirmDialogHost));
  53. expect(screen.queryByText("common.cancel")).toBeNull();
  54. fireEvent.click(screen.getByText("dismiss-dialog"));
  55. expect(useConfirmDialogStore.getState().pendingRequest).toBeDefined();
  56. fireEvent.click(screen.getByText("Got it"));
  57. await expect(result).resolves.toBe(true);
  58. });
  59. it("renders the remote continuity comparison with concise user-facing copy", () => {
  60. void useConfirmDialogStore.getState().requestConfirmation({
  61. presentation: "source-server-boundary",
  62. title: "本地离线,任务照常运行",
  63. description: zhCN["remote.sourceServerBoundaryDescription"],
  64. confirmLabel: "知道了",
  65. requiredAcknowledgement: true,
  66. showKeyboardHints: false,
  67. });
  68. render(createElement(ConfirmDialogHost));
  69. expect(screen.getByTestId("source-server-boundary-dialog")).toBeTruthy();
  70. expect(screen.getByText("本地离线,任务照常运行")).toBeTruthy();
  71. expect(screen.getByText("关闭 ZCode 或休眠本机, 远程任务继续运行。").textContent).toBe(
  72. "关闭 ZCode 或休眠本机,\n远程任务继续运行。",
  73. );
  74. expect(screen.getByTestId("source-server-boundary-local").textContent).toContain(
  75. "remote.sourceServerBoundaryLocalDevice",
  76. );
  77. expect(screen.getByTestId("source-server-boundary-remote").textContent).toContain(
  78. "remote.sourceServerBoundaryRemoteStatus",
  79. );
  80. expect(screen.getByRole("button", { name: "知道了" })).toBeTruthy();
  81. });
  82. });