conversationShareMenu.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { TooltipProvider } from "@/components/ui/tooltip.js";
  6. vi.mock("@/i18n/IntlProvider.js", () => ({
  7. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  8. }));
  9. afterEach(async () => {
  10. cleanup();
  11. const { useConversationShareSelectionStore } =
  12. await import("@/store/conversationShareSelectionStore.js");
  13. useConversationShareSelectionStore.getState().resetForTests();
  14. });
  15. describe("ConversationShareMenu", () => {
  16. it("点击入口创建默认全选的分享草稿并收起选择面板", async () => {
  17. const { ConversationShareMenu } = await import("@/ConversationShareMenu.js");
  18. const { getConversationShareDraft, useConversationShareSelectionStore } =
  19. await import("@/store/conversationShareSelectionStore.js");
  20. render(
  21. createElement(
  22. TooltipProvider,
  23. { delayDuration: 0 },
  24. createElement(ConversationShareMenu, { taskId: "task-42" }),
  25. ),
  26. );
  27. fireEvent.click(screen.getByRole("button", { name: "conversationShare.trigger" }));
  28. expect(
  29. getConversationShareDraft(useConversationShareSelectionStore.getState(), "task-42"),
  30. ).toMatchObject({ open: true, pendingExcludedRowIds: null, accessMode: "public_importable" });
  31. });
  32. it.each(["all", "partial", "published", "error"])(
  33. "分享已打开时再次点击收起并保留当前会话:%s",
  34. async (stage) => {
  35. const { ConversationShareMenu } = await import("@/ConversationShareMenu.js");
  36. const { getConversationShareDraft, useConversationShareSelectionStore } =
  37. await import("@/store/conversationShareSelectionStore.js");
  38. const store = useConversationShareSelectionStore.getState();
  39. store.openShare("task-42");
  40. store.syncAvailableTurns(
  41. "task-42",
  42. [11, 22].map((rowId) => ({ rowId, productTurnId: String(rowId) })),
  43. );
  44. if (stage === "partial") {
  45. store.beginSelectionEdit("task-42");
  46. store.togglePendingRow("task-42", 22);
  47. store.confirmSelectionEdit("task-42");
  48. }
  49. store.updateDockState("task-42", {
  50. title: "分享标题",
  51. publishedShareUrl: stage === "published" ? "https://example.com/share/test" : null,
  52. error:
  53. stage === "error"
  54. ? { issues: [], issueCount: 0, messageId: "conversationShare.issue.unknown" }
  55. : null,
  56. });
  57. store.openShare("task-other");
  58. store.updateDockState("task-other", { title: "另一会话" });
  59. const otherDraft = useConversationShareSelectionStore.getState().drafts["task-other"];
  60. const otherDock = useConversationShareSelectionStore.getState().dockStates["task-other"];
  61. render(
  62. createElement(
  63. TooltipProvider,
  64. { delayDuration: 0 },
  65. createElement(ConversationShareMenu, { taskId: "task-42" }),
  66. ),
  67. );
  68. fireEvent.click(screen.getByRole("button", { name: "conversationShare.trigger" }));
  69. const state = useConversationShareSelectionStore.getState();
  70. expect(getConversationShareDraft(state, "task-42").open).toBe(false);
  71. expect(state.dockStates["task-42"].title).toBe("分享标题");
  72. expect(state.drafts["task-other"]).toBe(otherDraft);
  73. expect(state.dockStates["task-other"]).toBe(otherDock);
  74. expect(screen.getByRole("button").getAttribute("aria-pressed")).toBe("false");
  75. fireEvent.click(screen.getByRole("button"));
  76. expect(
  77. getConversationShareDraft(useConversationShareSelectionStore.getState(), "task-42"),
  78. ).toMatchObject({
  79. open: true,
  80. pendingExcludedRowIds: null,
  81. excludedRowIds: stage === "partial" ? [22] : [],
  82. });
  83. },
  84. );
  85. it("发布过程中禁止通过顶部入口取消", async () => {
  86. const { ConversationShareMenu } = await import("@/ConversationShareMenu.js");
  87. const { useConversationShareSelectionStore } =
  88. await import("@/store/conversationShareSelectionStore.js");
  89. const store = useConversationShareSelectionStore.getState();
  90. store.openShare("task-42");
  91. store.updateDockState("task-42", { publishing: true });
  92. render(
  93. createElement(
  94. TooltipProvider,
  95. { delayDuration: 0 },
  96. createElement(ConversationShareMenu, { taskId: "task-42" }),
  97. ),
  98. );
  99. const button = screen.getByRole("button") as HTMLButtonElement;
  100. expect(button.disabled).toBe(true);
  101. fireEvent.click(button);
  102. expect(useConversationShareSelectionStore.getState().drafts["task-42"]?.open).toBe(true);
  103. });
  104. });