groupedRenameFocus.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
  3. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  4. import { createElement, type ComponentProps, type ReactNode } from "react";
  5. import { GroupItem } from "@/project-sidebar/grouped/group-item.js";
  6. vi.mock("@/i18n/IntlProvider.js", () => ({
  7. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  8. }));
  9. vi.mock("@/ControlHintTooltip.js", async (importOriginal) => ({
  10. ...(await importOriginal<typeof import("@/ControlHintTooltip.js")>()),
  11. ControlHintTooltip: ({ children }: { children: ReactNode }) => children,
  12. }));
  13. const onRename = vi.fn();
  14. function mount() {
  15. const props: ComponentProps<typeof GroupItem> = {
  16. node: {
  17. type: "group",
  18. group: { id: "g", title: "New group", color: "gray", createdAt: 1, updatedAt: 1 },
  19. tasks: [],
  20. },
  21. groups: [],
  22. activeWorkspacePath: "/workspace",
  23. activeTaskId: null,
  24. getTaskRemoteSessionId: () => undefined,
  25. getTaskWorkspaceLabel: () => "workspace",
  26. onSelectTask: vi.fn(),
  27. onCloseTask: vi.fn(),
  28. onCreateTask: vi.fn(),
  29. onRenameGroup: onRename,
  30. onUpdateGroupColor: vi.fn(),
  31. onUngroupGroup: vi.fn(),
  32. onMoveTaskToGroup: vi.fn(),
  33. onMoveTaskToTop: vi.fn(),
  34. onStartRenameTask: vi.fn(),
  35. onArchiveTask: vi.fn(),
  36. onMarkTaskAsUnread: vi.fn(),
  37. newGroupSetup: true,
  38. onNewGroupSetupStarted: vi.fn(),
  39. collapsed: false,
  40. onToggleCollapsed: vi.fn(),
  41. };
  42. render(createElement(GroupItem, props));
  43. return screen.getByTestId("task-group-title-input") as HTMLInputElement;
  44. }
  45. beforeEach(() => {
  46. vi.useFakeTimers();
  47. vi.stubGlobal(
  48. "ResizeObserver",
  49. class {
  50. observe() {}
  51. disconnect() {}
  52. unobserve() {}
  53. },
  54. );
  55. });
  56. afterEach(() => {
  57. cleanup();
  58. vi.useRealTimers();
  59. vi.unstubAllGlobals();
  60. vi.clearAllMocks();
  61. });
  62. it("菜单收尾回焦不能再次全选已经输入的名称", () => {
  63. const input = mount();
  64. fireEvent.change(input, { target: { value: "E2E_GROUP" } });
  65. input.setSelectionRange(input.value.length, input.value.length);
  66. act(() => {
  67. vi.advanceTimersByTime(50);
  68. });
  69. expect(input.selectionStart).toBe(input.value.length);
  70. expect(input.selectionEnd).toBe(input.value.length);
  71. });
  72. it("改名只禁用拖拽,不能让输入框继承不可用状态", () => {
  73. const input = mount();
  74. expect(input.closest('[aria-disabled="true"]')).toBeNull();
  75. });
  76. it("快速 Enter 显式提交,不受菜单初始焦点保护影响;IME Enter 保持编辑", () => {
  77. const input = mount();
  78. act(() => {
  79. input.focus();
  80. });
  81. fireEvent.change(input, { target: { value: "E2E_GROUP" } });
  82. fireEvent.compositionStart(input);
  83. fireEvent.keyDown(input, { key: "Enter" });
  84. expect(onRename).not.toHaveBeenCalled();
  85. fireEvent.compositionEnd(input);
  86. fireEvent.keyDown(input, { key: "Enter" });
  87. expect(onRename).toHaveBeenCalledWith("g", "E2E_GROUP");
  88. });