savedWorkflowMoveDialog.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // @vitest-environment jsdom
  2. // 「移到项目」窗(docs/dynamic-workflow/launch.md「Move to project」):项目选择器 + 提交,空候选禁用。
  3. import { createElement } from "react";
  4. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  5. import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
  6. import {
  7. TID_WORKFLOW_MOVE_DIALOG,
  8. TID_WORKFLOW_MOVE_DIALOG_SUBMIT,
  9. TID_WORKFLOW_MOVE_DIALOG_TARGET,
  10. } from "@zcode/shared";
  11. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  12. import {
  13. resolveAutomationWorkspaceSelectionKey,
  14. type AutomationWorkspaceOption,
  15. } from "@/settings/automationWorkspaceOptions.js";
  16. import { SavedWorkflowMoveDialog } from "@/settings/saved-workflows/SavedWorkflowMoveDialog.js";
  17. const ALPHA: AutomationWorkspaceOption = { workspacePath: "/repo/alpha", label: "Alpha" };
  18. const BETA: AutomationWorkspaceOption = { workspacePath: "/repo/beta", label: "Beta" };
  19. beforeAll(() => {
  20. (globalThis as { ResizeObserver?: unknown }).ResizeObserver ??= class {
  21. observe() {}
  22. unobserve() {}
  23. disconnect() {}
  24. };
  25. });
  26. afterEach(() => {
  27. cleanup();
  28. });
  29. function mount(props: Partial<Parameters<typeof SavedWorkflowMoveDialog>[0]> = {}) {
  30. const merged = {
  31. open: true,
  32. entryName: "deep-research",
  33. targets: [ALPHA, BETA] as readonly AutomationWorkspaceOption[],
  34. onOpenChange: vi.fn(),
  35. onSubmit: vi.fn(),
  36. ...props,
  37. };
  38. render(
  39. createElement(
  40. ZCodeIntlProvider,
  41. { initialLocale: "zh-CN" },
  42. createElement(SavedWorkflowMoveDialog, merged),
  43. ),
  44. );
  45. return merged;
  46. }
  47. describe("SavedWorkflowMoveDialog", () => {
  48. it("open 为 false 时不渲染", () => {
  49. mount({ open: false });
  50. expect(screen.queryByTestId(TID_WORKFLOW_MOVE_DIALOG)).toBeNull();
  51. });
  52. it("渲染标题与目标选择器,默认选中 defaultTargetKey", () => {
  53. mount({ defaultTargetKey: resolveAutomationWorkspaceSelectionKey(BETA) });
  54. expect(screen.getByTestId(TID_WORKFLOW_MOVE_DIALOG)).toBeTruthy();
  55. expect(screen.getByText("移到项目")).toBeTruthy();
  56. expect(screen.getByTestId(TID_WORKFLOW_MOVE_DIALOG_TARGET).textContent).toContain("Beta");
  57. });
  58. it("提交回带选中的项目", () => {
  59. const props = mount({ defaultTargetKey: resolveAutomationWorkspaceSelectionKey(ALPHA) });
  60. fireEvent.click(screen.getByTestId(TID_WORKFLOW_MOVE_DIALOG_SUBMIT));
  61. expect(props.onSubmit).toHaveBeenCalledTimes(1);
  62. expect(props.onSubmit).toHaveBeenCalledWith(ALPHA);
  63. });
  64. it("没有候选时提示并禁用提交", () => {
  65. mount({ targets: [] });
  66. expect(screen.queryByTestId(TID_WORKFLOW_MOVE_DIALOG_TARGET)).toBeNull();
  67. expect(screen.getByText("打开一个本地项目以移动")).toBeTruthy();
  68. expect(
  69. (screen.getByTestId(TID_WORKFLOW_MOVE_DIALOG_SUBMIT) as HTMLButtonElement).disabled,
  70. ).toBe(true);
  71. });
  72. it("busy 时禁用提交", () => {
  73. mount({ busy: true });
  74. expect(
  75. (screen.getByTestId(TID_WORKFLOW_MOVE_DIALOG_SUBMIT) as HTMLButtonElement).disabled,
  76. ).toBe(true);
  77. });
  78. });