pluginStoreItemMenuSeparator.test.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { StorePluginItem } from "../src/settings/pluginStoreListing.js";
  5. import type { PluginStoreActions } from "../src/settings/PluginStoreCard.js";
  6. // Radix 菜单关闭时不会渲染内容,这里把菜单原语替换为直出子节点,只验证菜单项结构。
  7. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  8. DropdownMenu: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  9. DropdownMenuTrigger: ({ children }: { children: ReactNode }) => children,
  10. DropdownMenuContent: ({ children }: { children: ReactNode }) =>
  11. createElement("div", { role: "menu" }, children),
  12. DropdownMenuItem: ({ children, ...props }: { children: ReactNode; [key: string]: unknown }) =>
  13. createElement("div", { role: "menuitem", "data-testid": props["data-testid"] }, children),
  14. DropdownMenuSeparator: () => createElement("hr", { "data-slot": "dropdown-menu-separator" }),
  15. }));
  16. vi.mock("@/i18n/IntlProvider.js", () => ({
  17. useZCodeIntl: () => ({
  18. locale: "zh-CN",
  19. intl: { formatMessage: ({ id }: { id: string }) => id },
  20. }),
  21. }));
  22. function storeItem(overrides: Partial<StorePluginItem> = {}): StorePluginItem {
  23. return {
  24. id: "skill-creator@zcode-plugins-official",
  25. name: "skill-creator",
  26. marketplace: "zcode-plugins-official",
  27. installed: true,
  28. restorable: false,
  29. orphaned: false,
  30. ...overrides,
  31. };
  32. }
  33. function makeActions(overrides: Partial<PluginStoreActions> = {}): PluginStoreActions {
  34. return {
  35. onOpenDetail: () => {},
  36. onInstall: () => {},
  37. onUninstall: () => {},
  38. onUpdate: () => {},
  39. operationId: null,
  40. togglingPluginId: null,
  41. ...overrides,
  42. };
  43. }
  44. async function renderMenu(item: StorePluginItem, actions: PluginStoreActions): Promise<string> {
  45. const { PluginStoreItemMenu } = await import("../src/settings/PluginStoreCard.js");
  46. return renderToStaticMarkup(createElement(PluginStoreItemMenu, { item, actions }));
  47. }
  48. describe("PluginStoreItemMenu separator", () => {
  49. // Bug:菜单只剩「卸载」一项时仍在其上方渲染分隔线,用户看到一条孤立横线。
  50. it("菜单只有卸载一项时不渲染分隔线", async () => {
  51. const html = await renderMenu(storeItem(), makeActions());
  52. expect(html).toContain('data-testid="plugin-store-menu-uninstall"');
  53. expect(html).not.toContain('data-slot="dropdown-menu-separator"');
  54. });
  55. it("卸载前只有「更新」一项时同样保留分隔线", async () => {
  56. const html = await renderMenu(
  57. storeItem({
  58. installedMeta: {
  59. id: "skill-creator@zcode-plugins-official",
  60. name: "skill-creator",
  61. marketplace: "zcode-plugins-official",
  62. enabled: true,
  63. scope: "user",
  64. updateStatus: "update-available",
  65. },
  66. }),
  67. makeActions(),
  68. );
  69. expect(html).toContain('data-testid="plugin-store-menu-update"');
  70. expect(html).toContain('data-slot="dropdown-menu-separator"');
  71. });
  72. it("卸载前有其他操作项时保留分隔线", async () => {
  73. const html = await renderMenu(
  74. storeItem({ info: { enabled: true } as StorePluginItem["info"] }),
  75. makeActions({ onSetEnabled: () => {} }),
  76. );
  77. expect(html).toContain('data-testid="plugin-store-menu-enabled"');
  78. expect(html).toContain('data-slot="dropdown-menu-separator"');
  79. });
  80. });