pluginStoreUpdateEntries.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. activeOperations: [],
  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: "personal",
  81. onSegmentChange: () => {},
  82. }),
  83. );
  84. }
  85. describe("plugin update badge and inline update entries", () => {
  86. afterEach(() => {
  87. buttonProps.length = 0;
  88. });
  89. it("shows the update badge and inline update button on an updatable installed card", async () => {
  90. const html = await renderCard(storeItem(), makeActions());
  91. expect(html).toContain('data-testid="plugin-store-update-badge"');
  92. expect(html).toContain('data-testid="plugin-store-card-update"');
  93. });
  94. it("hides both entries when the plugin has no pending update", async () => {
  95. const html = await renderCard(
  96. storeItem({ installedMeta: installedMeta({ updateStatus: "none" }) }),
  97. makeActions(),
  98. );
  99. expect(html).not.toContain('data-testid="plugin-store-update-badge"');
  100. expect(html).not.toContain('data-testid="plugin-store-card-update"');
  101. });
  102. it("hides both entries for an orphaned plugin even when a stale updateStatus remains", async () => {
  103. const html = await renderCard(storeItem({ orphaned: true }), makeActions());
  104. expect(html).not.toContain('data-testid="plugin-store-update-badge"');
  105. expect(html).not.toContain('data-testid="plugin-store-card-update"');
  106. });
  107. it("invokes onUpdate with the plugin id when the inline button is clicked", async () => {
  108. const onUpdate = vi.fn();
  109. await renderCard(storeItem(), makeActions({ onUpdate }));
  110. const updateButton = buttonProps.find(
  111. (props) => props["data-testid"] === "plugin-store-card-update",
  112. );
  113. const onClick = updateButton?.onClick as ((event: unknown) => void) | undefined;
  114. expect(onClick).toBeTypeOf("function");
  115. onClick?.({ stopPropagation: () => {} });
  116. expect(onUpdate).toHaveBeenCalledWith("demo@personal");
  117. });
  118. it("keeps the inline update entry enabled while another plugin installs", async () => {
  119. const updateButton = () =>
  120. buttonProps.findLast((props) => props["data-testid"] === "plugin-store-card-update");
  121. await renderCard(
  122. storeItem(),
  123. makeActions({ activeOperations: [{ id: "plugin:install:other@personal" }] }),
  124. );
  125. expect(updateButton()?.disabled).toBe(false);
  126. await renderCard(
  127. storeItem(),
  128. makeActions({ activeOperations: [{ id: "plugin:update:demo@personal" }] }),
  129. );
  130. expect(updateButton()?.disabled).toBe(true);
  131. });
  132. it("disables the inline update entry while the same plugin saves its config", async () => {
  133. const updateButton = () =>
  134. buttonProps.findLast((props) => props["data-testid"] === "plugin-store-card-update");
  135. await renderCard(
  136. storeItem(),
  137. makeActions({ activeOperations: [{ id: "plugin:configure:demo@personal" }] }),
  138. );
  139. expect(updateButton()?.disabled).toBe(true);
  140. });
  141. it("keeps the inline update entry on the installed card after removing the strip", async () => {
  142. const pendingHtml = await renderListView(storeItem(), makeActions());
  143. expect(pendingHtml).toContain('data-testid="plugin-store-card-update"');
  144. const noneHtml = await renderListView(
  145. storeItem({ installedMeta: installedMeta({ updateStatus: "none" }) }),
  146. makeActions(),
  147. );
  148. expect(noneHtml).not.toContain('data-testid="plugin-store-card-update"');
  149. });
  150. });