confirmDialogKeyboard.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { PluginUninstallConfirmDialog } from "@/settings/PluginUninstallConfirmDialog.js";
  6. import { StorageCleanConfirmDialog } from "@/resource-manager/storage/StorageCleanConfirmDialog.js";
  7. import { ConfirmDialogHost } from "@/ConfirmDialog.js";
  8. import { useConfirmDialogStore } from "@/store/confirmDialogStore.js";
  9. vi.mock("@/i18n/IntlProvider.js", () => ({
  10. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  11. }));
  12. afterEach(() => {
  13. cleanup();
  14. useConfirmDialogStore.getState().settleConfirmation(false);
  15. });
  16. describe("破坏性确认键盘防护", () => {
  17. it.each([undefined, "automation-confirmation"] as const)(
  18. "%s 默认聚焦取消,回车不能确认,显式点击才确认",
  19. async (presentation) => {
  20. render(createElement(ConfirmDialogHost));
  21. let result!: Promise<boolean>;
  22. act(() => {
  23. result = useConfirmDialogStore.getState().requestConfirmation({
  24. title: "删除",
  25. description: "删除操作不可恢复",
  26. presentation,
  27. confirmVariant: "destructive",
  28. confirmLabel: "删除",
  29. cancelLabel: "取消",
  30. });
  31. });
  32. expect(document.activeElement).toBe(screen.getByRole("button", { name: /取消/ }));
  33. fireEvent.keyDown(document.activeElement!, { key: "Enter" });
  34. expect(useConfirmDialogStore.getState().pendingRequest).toBeDefined();
  35. fireEvent.click(screen.getByRole("button", { name: "删除" }));
  36. await expect(result).resolves.toBe(true);
  37. },
  38. );
  39. it.each(["plugin", "storage"] as const)("%s 独立破坏性弹窗也默认聚焦取消", (kind) => {
  40. const confirm = vi.fn();
  41. render(
  42. kind === "plugin"
  43. ? createElement(PluginUninstallConfirmDialog, {
  44. open: true,
  45. pluginName: "test",
  46. pending: false,
  47. onCancel: vi.fn(),
  48. onConfirm: confirm,
  49. })
  50. : createElement(StorageCleanConfirmDialog, {
  51. target: { categoryId: "logs", bytes: 1024 },
  52. pending: false,
  53. onCancel: vi.fn(),
  54. onConfirm: confirm,
  55. }),
  56. );
  57. expect(document.activeElement).toBe(screen.getByRole("button", { name: "common.cancel" }));
  58. fireEvent.keyDown(document.activeElement!, { key: "Enter" });
  59. expect(confirm).not.toHaveBeenCalled();
  60. });
  61. it("普通确认保留回车快捷键", async () => {
  62. render(createElement(ConfirmDialogHost));
  63. let result!: Promise<boolean>;
  64. act(() => {
  65. result = useConfirmDialogStore
  66. .getState()
  67. .requestConfirmation({ title: "连接", description: "连接设备" });
  68. });
  69. fireEvent.keyDown(window, { key: "Enter" });
  70. await expect(result).resolves.toBe(true);
  71. });
  72. });