pluginStorePaidPlanBadge.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // 与 pluginStoreInstalledStripTooltip.test.ts 同款:ControlHintTooltip 依赖 Root 的
  7. // TooltipProvider,SSR 单测里桩成透传 span,把 title 落到属性上便于断言 hover 文案。
  8. vi.mock("@/ControlHintTooltip.js", () => ({
  9. ControlHintTooltip: ({ children, title }: { children: ReactNode; title: string }) =>
  10. createElement("span", { "data-tooltip-title": title }, children),
  11. }));
  12. // 付费套餐提示:市场目录条目的 `listing.requiresPaidPlan` 决定商店卡片与详情页标题
  13. // 右侧是否出现渐变徽标。缺字段(无需套餐/老目录)时整个标记不渲染,
  14. // 见 docs/plugin-marketplace-ui-ux.md「降级矩阵」。
  15. function makeItem(requiresPaidPlan: boolean | undefined): StorePluginItem {
  16. return {
  17. id: "pro-plugin@zcode-plugins-official",
  18. name: "pro-plugin",
  19. marketplace: "zcode-plugins-official",
  20. installed: false,
  21. restorable: false,
  22. orphaned: false,
  23. listing: {
  24. displayName: "Pro Plugin",
  25. ...(requiresPaidPlan === undefined ? {} : { requiresPaidPlan }),
  26. },
  27. };
  28. }
  29. const actions: PluginStoreActions = {
  30. onOpenDetail: () => {},
  31. onInstall: () => {},
  32. onUninstall: () => {},
  33. onSetEnabled: () => {},
  34. onUpdate: () => {},
  35. activeOperations: [],
  36. togglingPluginId: null,
  37. };
  38. async function renderCard(item: StorePluginItem): Promise<string> {
  39. const { PluginStoreCard } = await import("../src/settings/PluginStoreCard.js");
  40. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  41. return renderToStaticMarkup(
  42. createElement(
  43. ZCodeIntlProvider,
  44. { initialLocale: "en-US" },
  45. createElement(PluginStoreCard, { item, actions, locale: "en-US" }),
  46. ),
  47. );
  48. }
  49. async function renderDetail(item: StorePluginItem): Promise<string> {
  50. const { PluginStoreDetailView } = await import("../src/settings/PluginStoreDetailView.js");
  51. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  52. return renderToStaticMarkup(
  53. createElement(
  54. ZCodeIntlProvider,
  55. { initialLocale: "en-US" },
  56. createElement(PluginStoreDetailView, {
  57. item,
  58. actions,
  59. onRetryDescribe: () => {},
  60. onUsePrompt: () => {},
  61. }),
  62. ),
  63. );
  64. }
  65. describe("plugin store paid plan badge", () => {
  66. it("marks a plugin that needs a paid plan on the store card title", async () => {
  67. const html = await renderCard(makeItem(true));
  68. expect(html).toContain('data-testid="plugin-store-paid-plan-badge"');
  69. expect(html).toContain("Coding Plan");
  70. expect(html).toContain('data-tooltip-title="This plugin works better with a Coding Plan"');
  71. });
  72. it("marks a plugin that needs a paid plan on the detail title", async () => {
  73. const html = await renderDetail(makeItem(true));
  74. expect(html).toContain('data-testid="plugin-store-paid-plan-badge"');
  75. expect(html).toContain("Coding Plan");
  76. expect(html).toContain('data-tooltip-title="This plugin works better with a Coding Plan"');
  77. });
  78. it("omits the badge when the plan is not required and when the field is absent", async () => {
  79. for (const requiresPaidPlan of [false, undefined] as const) {
  80. expect(await renderCard(makeItem(requiresPaidPlan))).not.toContain(
  81. "plugin-store-paid-plan-badge",
  82. );
  83. expect(await renderDetail(makeItem(requiresPaidPlan))).not.toContain(
  84. "plugin-store-paid-plan-badge",
  85. );
  86. }
  87. });
  88. });