shortcutSettingsSection.test.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // @vitest-environment jsdom
  2. import { act, createElement } from "react";
  3. import { createRoot, type Root } from "react-dom/client";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. const mocks = vi.hoisted(() => ({
  6. update: vi.fn(),
  7. error: vi.fn(),
  8. settings: { shortcutBindings: { toggleSidebar: ["CmdOrCtrl+e"] } },
  9. }));
  10. vi.mock("@/hooks/useSettingService.js", () => ({
  11. useSettings: () => ({ settings: mocks.settings, update: mocks.update }),
  12. }));
  13. vi.mock("@/hooks/usePlatform.js", () => ({ usePlatform: () => ({}) }));
  14. vi.mock("@/i18n/IntlProvider.js", () => ({
  15. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  16. }));
  17. vi.mock("@/logger.js", () => ({ logger: { error: mocks.error } }));
  18. vi.mock("@/components/ui/toast.js", () => ({ toast: vi.fn() }));
  19. import { ShortcutSettingsSection } from "@/settings/ShortcutSettingsSection.js";
  20. globalThis.IS_REACT_ACT_ENVIRONMENT = true;
  21. let root: Root;
  22. let container: HTMLDivElement;
  23. afterEach(async () => {
  24. if (root) await act(() => root.unmount());
  25. vi.clearAllMocks();
  26. });
  27. async function mount(isDesktop = true) {
  28. container = document.createElement("div");
  29. root = createRoot(container);
  30. await act(() => root.render(createElement(ShortcutSettingsSection, { isDesktop })));
  31. }
  32. function button(id: string) {
  33. return container.querySelector<HTMLButtonElement>(`[data-testid="${id}"]`)!;
  34. }
  35. describe("shortcut settings save admission", () => {
  36. it("blocks edits until the settings update finishes, then admits the next edit", async () => {
  37. let finish!: () => void;
  38. mocks.update
  39. .mockImplementationOnce(
  40. () =>
  41. new Promise<void>((resolve) => {
  42. finish = resolve;
  43. }),
  44. )
  45. .mockResolvedValue(undefined);
  46. await mount();
  47. expect(button("settings-shortcut-reset-all").disabled).toBe(false);
  48. await act(() => button("settings-shortcut-clear-toggleSidebar").click());
  49. expect(button("settings-shortcut-edit-openCommandCenter-0").disabled).toBe(true);
  50. expect(button("settings-shortcut-clear-openCommandCenter").disabled).toBe(true);
  51. expect(button("settings-shortcut-reset-all").disabled).toBe(true);
  52. await act(() => button("settings-shortcut-clear-openCommandCenter").click());
  53. expect(mocks.update).toHaveBeenCalledTimes(1);
  54. await act(async () => {
  55. finish();
  56. });
  57. expect(button("settings-shortcut-edit-openCommandCenter-0").disabled).toBe(false);
  58. await act(() => button("settings-shortcut-clear-openCommandCenter").click());
  59. expect(mocks.update).toHaveBeenCalledTimes(2);
  60. });
  61. it("shows a save failure, preserves settings and permits a user retry", async () => {
  62. mocks.update.mockRejectedValueOnce(new Error("disk unavailable")).mockResolvedValue(undefined);
  63. await mount();
  64. await act(() => button("settings-shortcut-clear-toggleSidebar").click());
  65. expect(container.querySelector('[role="alert"]')?.textContent).toBe(
  66. "settings.shortcuts.saveFailed",
  67. );
  68. expect(button("settings-shortcut-bind-toggleSidebar-0")).toBeTruthy();
  69. expect(button("settings-shortcut-clear-toggleSidebar").disabled).toBe(false);
  70. expect(mocks.error).toHaveBeenCalledTimes(1);
  71. await act(() => button("settings-shortcut-clear-toggleSidebar").click());
  72. expect(mocks.update).toHaveBeenCalledTimes(2);
  73. expect(container.querySelector('[role="alert"]')).toBeNull();
  74. });
  75. // 完整视图只在桌面端提供(docs/ui/side-pane-full-view.md),Web 设置页不展示该命令。
  76. it("lists the side pane full view shortcut only on desktop", async () => {
  77. await mount(true);
  78. expect(button("settings-shortcut-clear-toggleSidePaneFullView")).toBeTruthy();
  79. await act(() => root.unmount());
  80. await mount(false);
  81. expect(
  82. container.querySelector('[data-testid="settings-shortcut-clear-toggleSidePaneFullView"]'),
  83. ).toBeNull();
  84. });
  85. });