pluginStorePaidPlanBadge.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. operationId: null,
  36. togglingPluginId: null,
  37. };
  38. async function renderCard(
  39. item: StorePluginItem,
  40. locale: "en-US" | "zh-CN" = "en-US",
  41. ): Promise<string> {
  42. const { PluginStoreCard } = await import("../src/settings/PluginStoreCard.js");
  43. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  44. return renderToStaticMarkup(
  45. createElement(
  46. ZCodeIntlProvider,
  47. { initialLocale: locale },
  48. createElement(PluginStoreCard, { item, actions, locale }),
  49. ),
  50. );
  51. }
  52. async function renderDetail(item: StorePluginItem): Promise<string> {
  53. const { PluginStoreDetailView } = await import("../src/settings/PluginStoreDetailView.js");
  54. const { ZCodeIntlProvider } = await import("@/i18n/IntlProvider.js");
  55. return renderToStaticMarkup(
  56. createElement(
  57. ZCodeIntlProvider,
  58. { initialLocale: "en-US" },
  59. createElement(PluginStoreDetailView, {
  60. item,
  61. actions,
  62. onRetryDescribe: () => {},
  63. onUsePrompt: () => {},
  64. }),
  65. ),
  66. );
  67. }
  68. describe("plugin store paid plan badge", () => {
  69. it("marks a plugin that needs a paid plan on the store card title", async () => {
  70. const html = await renderCard(makeItem(true));
  71. expect(html).toContain('data-testid="plugin-store-paid-plan-badge"');
  72. expect(html).toContain("bg-[var(--color-plugin-paid-plan-badge)]");
  73. expect(html).toContain("text-[var(--color-plugin-paid-plan-badge-foreground)]");
  74. expect(html).not.toContain("button-gradient");
  75. expect(html).toContain("text-ui-sm");
  76. expect(html).toContain("lucide-crown");
  77. expect(html).toContain("Coding Plan");
  78. expect(html).toContain("lucide-crown");
  79. expect(html).not.toContain("lucide-trending-up");
  80. expect(html).toContain('data-tooltip-title="This plugin works better with a Coding Plan"');
  81. });
  82. it("marks a plugin that needs a paid plan on the detail title", async () => {
  83. const html = await renderDetail(makeItem(true));
  84. expect(html).toContain('data-testid="plugin-store-paid-plan-badge"');
  85. expect(html).toContain("bg-[var(--color-plugin-paid-plan-badge)]");
  86. expect(html).toContain("Coding Plan");
  87. expect(html).toContain('data-tooltip-title="This plugin works better with a Coding Plan"');
  88. });
  89. it("uses the concise coding plan label in Chinese", async () => {
  90. const html = await renderCard(makeItem(true), "zh-CN");
  91. expect(html).toContain("编程套餐");
  92. expect(html).not.toContain("支持编程套餐");
  93. });
  94. it("omits the badge when the plan is not required and when the field is absent", async () => {
  95. for (const requiresPaidPlan of [false, undefined] as const) {
  96. expect(await renderCard(makeItem(requiresPaidPlan))).not.toContain(
  97. "plugin-store-paid-plan-badge",
  98. );
  99. expect(await renderDetail(makeItem(requiresPaidPlan))).not.toContain(
  100. "plugin-store-paid-plan-badge",
  101. );
  102. }
  103. });
  104. });