pluginStoreUpdateEntries.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { ZCodeInstalledPluginSummary } from "@zcode/shared";
  5. import type { StorePluginItem } from "../src/settings/pluginStoreListing.js";
  6. import type { PluginStoreActions } from "../src/settings/PluginStoreCard.js";
  7. // 复用 pluginStoreCardRestorable.test.ts 的「mock Button 捕获 onClick」套路。
  8. const { buttonProps } = vi.hoisted(() => ({
  9. buttonProps: [] as Array<Record<string, unknown>>,
  10. }));
  11. vi.mock("@/components/ui/button.js", () => ({
  12. Button: ({ children, ...props }: { children?: ReactNode }) => {
  13. buttonProps.push(props);
  14. return createElement("button", props as Record<string, unknown>, children);
  15. },
  16. }));
  17. vi.mock("@/ControlHintTooltip.js", () => ({
  18. ControlHintTooltip: ({ children }: { children: ReactNode }) =>
  19. createElement("span", {}, children),
  20. }));
  21. vi.mock("@/i18n/IntlProvider.js", () => ({
  22. useZCodeIntl: () => ({
  23. locale: "en-US",
  24. intl: {
  25. formatMessage: ({ id }: { id: string }) => id,
  26. },
  27. }),
  28. }));
  29. function installedMeta(
  30. overrides: Partial<ZCodeInstalledPluginSummary> = {},
  31. ): ZCodeInstalledPluginSummary {
  32. return {
  33. id: "demo@personal",
  34. name: "demo",
  35. marketplace: "personal",
  36. enabled: true,
  37. scope: "user",
  38. updateStatus: "update-available",
  39. ...overrides,
  40. };
  41. }
  42. function storeItem(overrides: Partial<StorePluginItem> = {}): StorePluginItem {
  43. return {
  44. id: "demo@personal",
  45. name: "demo",
  46. marketplace: "personal",
  47. installed: true,
  48. restorable: false,
  49. orphaned: false,
  50. installedMeta: installedMeta(),
  51. ...overrides,
  52. };
  53. }
  54. function makeActions(overrides: Partial<PluginStoreActions> = {}): PluginStoreActions {
  55. return {
  56. onOpenDetail: () => {},
  57. onInstall: () => {},
  58. onUninstall: () => {},
  59. onSetEnabled: () => {},
  60. onUpdate: () => {},
  61. operationId: null,
  62. togglingPluginId: null,
  63. ...overrides,
  64. };
  65. }
  66. async function renderCard(item: StorePluginItem, actions: PluginStoreActions): Promise<string> {
  67. const { PluginStoreCard } = await import("../src/settings/PluginStoreCard.js");
  68. return renderToStaticMarkup(createElement(PluginStoreCard, { item, actions, locale: "en-US" }));
  69. }
  70. async function renderListView(item: StorePluginItem, actions: PluginStoreActions): Promise<string> {
  71. const { PluginStoreListView } = await import("../src/settings/PluginStoreListView.js");
  72. return renderToStaticMarkup(
  73. createElement(PluginStoreListView, {
  74. items: [item],
  75. marketplaces: [],
  76. actions,
  77. loading: false,
  78. query: "",
  79. onQueryChange: () => {},
  80. segment: "public",
  81. onSegmentChange: () => {},
  82. onOpenManage: () => {},
  83. }),
  84. );
  85. }
  86. describe("plugin update badge and inline update entries", () => {
  87. afterEach(() => {
  88. buttonProps.length = 0;
  89. });
  90. it("shows the update badge and inline update button on an updatable installed card", async () => {
  91. const html = await renderCard(storeItem(), makeActions());
  92. expect(html).toContain('data-testid="plugin-store-update-badge"');
  93. expect(html).toContain('data-testid="plugin-store-card-update"');
  94. });
  95. it("hides both entries when the plugin has no pending update", async () => {
  96. const html = await renderCard(
  97. storeItem({ installedMeta: installedMeta({ updateStatus: "none" }) }),
  98. makeActions(),
  99. );
  100. expect(html).not.toContain('data-testid="plugin-store-update-badge"');
  101. expect(html).not.toContain('data-testid="plugin-store-card-update"');
  102. });
  103. it("hides both entries for an orphaned plugin even when a stale updateStatus remains", async () => {
  104. const html = await renderCard(storeItem({ orphaned: true }), makeActions());
  105. expect(html).not.toContain('data-testid="plugin-store-update-badge"');
  106. expect(html).not.toContain('data-testid="plugin-store-card-update"');
  107. });
  108. it("invokes onUpdate with the plugin id when the inline button is clicked", async () => {
  109. const onUpdate = vi.fn();
  110. await renderCard(storeItem(), makeActions({ onUpdate }));
  111. const updateButton = buttonProps.find(
  112. (props) => props["data-testid"] === "plugin-store-card-update",
  113. );
  114. const onClick = updateButton?.onClick as ((event: unknown) => void) | undefined;
  115. expect(onClick).toBeTypeOf("function");
  116. onClick?.({ stopPropagation: () => {} });
  117. expect(onUpdate).toHaveBeenCalledWith("demo@personal");
  118. });
  119. it("overlays an update entry on the installed strip avatar only when updatable", async () => {
  120. const pendingHtml = await renderListView(storeItem(), makeActions());
  121. expect(pendingHtml).toContain('data-testid="plugin-store-installed-item-update"');
  122. const noneHtml = await renderListView(
  123. storeItem({ installedMeta: installedMeta({ updateStatus: "none" }) }),
  124. makeActions(),
  125. );
  126. expect(noneHtml).not.toContain('data-testid="plugin-store-installed-item-update"');
  127. });
  128. });