botsProjectAccess.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, expect, it, vi } from "vitest";
  5. import { WorkspaceAccessCard } from "@/BotsDialog/WorkspaceAccessCard.js";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import type { BotConfig } from "@zcode/shared";
  8. vi.mock("@/components/ui/select.js", () => ({
  9. Select: ({
  10. onValueChange,
  11. disabled,
  12. children,
  13. }: {
  14. onValueChange: (value: string) => void;
  15. disabled: boolean;
  16. children: unknown;
  17. }) =>
  18. createElement(
  19. "div",
  20. null,
  21. createElement(
  22. "button",
  23. { disabled, onClick: () => onValueChange("selected") },
  24. "select-projects",
  25. ),
  26. children,
  27. ),
  28. SelectContent: () => null,
  29. SelectItem: () => null,
  30. SelectTrigger: () => null,
  31. SelectValue: () => null,
  32. }));
  33. afterEach(cleanup);
  34. it("切换指定项目时保存真实项目 ID,不能保存当前 tab cwd", () => {
  35. const patch = vi.fn(async () => undefined);
  36. const props = {
  37. bot: { allowedWorkspaces: ["*"] } as BotConfig,
  38. workspaceRefs: [
  39. { id: '["local","p1"]', label: "P1", workspacePath: "/same" },
  40. { id: '["local","p2"]', label: "P2", workspacePath: "/same" },
  41. ],
  42. currentWorkspace: { id: "/unopened", label: "old tab", workspacePath: "/unopened" },
  43. loading: false,
  44. onPatchAllowedWorkspaces: patch,
  45. onToggleWorkspaceAccess: vi.fn(async () => undefined),
  46. };
  47. render(
  48. createElement(
  49. ZCodeIntlProvider,
  50. { initialLocale: "en-US" },
  51. createElement(WorkspaceAccessCard, props),
  52. ),
  53. );
  54. fireEvent.click(screen.getByText("select-projects"));
  55. expect(patch).toHaveBeenCalledWith(['["local","p1"]']);
  56. });