conversationShareMenu.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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({ scope: "partial", view: "timeline", accessMode: "public_importable" });
  31. });
  32. it.each(["all", "partial", "configuration", "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.setScope("task-42", "partial");
  40. store.syncAvailableRowIds("task-42", [11, 22]);
  41. if (stage === "partial") store.toggleRow("task-42", 22);
  42. if (stage !== "all" && stage !== "partial") store.goToConfiguration("task-42");
  43. store.updateDockState("task-42", {
  44. title: "分享标题",
  45. publishedShareUrl: stage === "published" ? "https://example.com/share/test" : null,
  46. error:
  47. stage === "error"
  48. ? { issues: [], issueCount: 0, messageId: "conversationShare.issue.unknown" }
  49. : null,
  50. });
  51. store.setScope("task-other", "partial");
  52. store.updateDockState("task-other", { title: "另一会话" });
  53. const otherDraft = useConversationShareSelectionStore.getState().drafts["task-other"];
  54. const otherDock = useConversationShareSelectionStore.getState().dockStates["task-other"];
  55. render(
  56. createElement(
  57. TooltipProvider,
  58. { delayDuration: 0 },
  59. createElement(ConversationShareMenu, { taskId: "task-42" }),
  60. ),
  61. );
  62. fireEvent.click(screen.getByRole("button", { name: "conversationShare.trigger" }));
  63. const state = useConversationShareSelectionStore.getState();
  64. expect(getConversationShareDraft(state, "task-42").scope).toBe("all");
  65. expect(state.dockStates["task-42"]).toBeUndefined();
  66. expect(state.drafts["task-other"]).toBe(otherDraft);
  67. expect(state.dockStates["task-other"]).toBe(otherDock);
  68. expect(screen.getByRole("button").getAttribute("aria-pressed")).toBe("false");
  69. fireEvent.click(screen.getByRole("button"));
  70. expect(
  71. getConversationShareDraft(useConversationShareSelectionStore.getState(), "task-42"),
  72. ).toMatchObject({ scope: "partial", view: "timeline", excludedRowIds: [] });
  73. },
  74. );
  75. it("发布过程中禁止通过顶部入口取消", async () => {
  76. const { ConversationShareMenu } = await import("@/ConversationShareMenu.js");
  77. const { useConversationShareSelectionStore } =
  78. await import("@/store/conversationShareSelectionStore.js");
  79. const store = useConversationShareSelectionStore.getState();
  80. store.setScope("task-42", "partial");
  81. store.updateDockState("task-42", { publishing: true });
  82. render(
  83. createElement(
  84. TooltipProvider,
  85. { delayDuration: 0 },
  86. createElement(ConversationShareMenu, { taskId: "task-42" }),
  87. ),
  88. );
  89. const button = screen.getByRole("button") as HTMLButtonElement;
  90. expect(button.disabled).toBe(true);
  91. fireEvent.click(button);
  92. expect(useConversationShareSelectionStore.getState().drafts["task-42"]?.scope).toBe("partial");
  93. });
  94. it.each([false, true])(
  95. "与工具栏共用尺寸和图标,Windows caption=%s",
  96. async (useWindowsCaptionSpacing) => {
  97. const { ConversationShareMenu } = await import("@/ConversationShareMenu.js");
  98. render(
  99. createElement(
  100. TooltipProvider,
  101. { delayDuration: 0 },
  102. createElement(ConversationShareMenu, {
  103. taskId: "task-42",
  104. useWindowsCaptionSpacing,
  105. }),
  106. ),
  107. );
  108. const trigger = screen.getByRole("button", { name: "conversationShare.trigger" });
  109. expect(trigger.getAttribute("data-size")).toBe("icon-md");
  110. expect(trigger.className).toContain(useWindowsCaptionSpacing ? "ml-3" : "ml-2.5");
  111. expect(trigger.className.includes("h-full")).toBe(useWindowsCaptionSpacing);
  112. expect(trigger.querySelector("svg.size-4")).not.toBeNull();
  113. expect(trigger.querySelector("[style]")).toBeNull();
  114. },
  115. );
  116. });