pluginStoreGroups.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // @vitest-environment jsdom
  2. import { act, createElement } from "react";
  3. import { createRoot } from "react-dom/client";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID } from "@zcode/shared";
  6. import type { PluginStoreActions } from "../src/settings/PluginStoreCard.js";
  7. import type { StorePluginItem } from "../src/settings/pluginStoreListing.js";
  8. const state = vi.hoisted(() => ({ locale: "zh-CN" }));
  9. vi.mock("@/hooks/useInterfaceMode.js", () => ({ useIsOfficeMode: () => false }));
  10. vi.mock("@/i18n/IntlProvider.js", () => ({
  11. useZCodeIntl: () => ({
  12. locale: state.locale,
  13. intl: { formatMessage: ({ id }: { id: string }) => id },
  14. }),
  15. }));
  16. vi.mock("@/settings/PluginStoreCard.js", () => ({
  17. PluginStoreCard: ({ item }: { item: StorePluginItem }) =>
  18. createElement("div", { "data-testid": "card" }, item.id),
  19. }));
  20. vi.mock("@/settings/PluginStoreAvatar.js", () => ({ PluginStoreAvatar: () => null }));
  21. import { PluginStoreListView } from "../src/settings/PluginStoreListView.js";
  22. let root: ReturnType<typeof createRoot> | undefined;
  23. afterEach(async () => {
  24. if (root) await act(() => root?.unmount());
  25. root = undefined;
  26. });
  27. describe.each(["zh-CN", "en-US"])("plugin groups in %s", (locale) => {
  28. it.each(["public", "community", "personal"] as const)(
  29. "expands UI plugins inline in %s and preserves the group during search",
  30. async (segment) => {
  31. state.locale = locale;
  32. const marketplace = segment === "personal" ? "custom" : ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID;
  33. const items: StorePluginItem[] = Array.from({ length: 9 }, (_, index) => {
  34. const name = index < 8 ? `panel-${index}` : "panel-regular";
  35. return {
  36. id: `${name}@${marketplace}`,
  37. name,
  38. marketplace,
  39. installed: false,
  40. restorable: false,
  41. orphaned: false,
  42. listing: {
  43. category: "utilities",
  44. publisherType: segment === "community" ? "community" : "official",
  45. },
  46. summary: {
  47. id: `${name}@${marketplace}`,
  48. name,
  49. marketplace,
  50. installed: false,
  51. componentTypes: index < 8 ? ["ui"] : [],
  52. },
  53. };
  54. });
  55. const container = document.createElement("div");
  56. root = createRoot(container);
  57. const render = (query = "") =>
  58. act(() =>
  59. root?.render(
  60. createElement(PluginStoreListView, {
  61. items,
  62. marketplaces: [],
  63. actions: {} as PluginStoreActions,
  64. loading: false,
  65. query,
  66. segment,
  67. onQueryChange: vi.fn(),
  68. onSegmentChange: vi.fn(),
  69. }),
  70. ),
  71. );
  72. const uiSection = () => container.querySelector('[data-testid="plugin-store-ui-section"]')!;
  73. const toggle = () =>
  74. act(() =>
  75. uiSection()
  76. .querySelector<HTMLButtonElement>('[data-testid="plugin-store-group-toggle"]')!
  77. .click(),
  78. );
  79. await render();
  80. expect(uiSection().querySelectorAll('[data-testid="card"]')).toHaveLength(8);
  81. expect(container.querySelector('[data-testid="plugin-store-ui-all"]')).toBeNull();
  82. expect(container.textContent).toContain("panel-regular");
  83. await toggle();
  84. expect(uiSection().querySelectorAll('[data-testid="card"]')).toHaveLength(6);
  85. expect(container.textContent).toContain("panel-regular");
  86. await render("panel");
  87. expect(container.querySelectorAll('[data-testid="card"]')).toHaveLength(9);
  88. await render();
  89. expect(uiSection().querySelectorAll('[data-testid="card"]')).toHaveLength(6);
  90. await toggle();
  91. expect(uiSection().querySelectorAll('[data-testid="card"]')).toHaveLength(8);
  92. expect(container.querySelector('[data-testid="plugin-store-ui-back"]')).toBeNull();
  93. },
  94. );
  95. it.each(["public", "community", "personal"] as const)(
  96. "limits %s search to its publisher/source segment",
  97. async (segment) => {
  98. const items: StorePluginItem[] = ["public", "community", "personal"].map((source) => ({
  99. id: `${source}@${source === "personal" ? "custom" : ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID}`,
  100. name: source,
  101. marketplace: source === "personal" ? "custom" : ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID,
  102. installed: false,
  103. restorable: false,
  104. orphaned: false,
  105. listing: {
  106. category: "utilities",
  107. publisherType: source === "public" ? "official" : "community",
  108. displayName: "needle",
  109. },
  110. }));
  111. const container = document.createElement("div");
  112. root = createRoot(container);
  113. await act(() =>
  114. root?.render(
  115. createElement(PluginStoreListView, {
  116. items,
  117. marketplaces: [],
  118. actions: {} as PluginStoreActions,
  119. loading: false,
  120. query: "needle",
  121. segment,
  122. onQueryChange: vi.fn(),
  123. onSegmentChange: vi.fn(),
  124. }),
  125. ),
  126. );
  127. const cards = [...container.querySelectorAll('[data-testid="card"]')];
  128. expect(cards).toHaveLength(1);
  129. expect(cards[0]?.textContent).toBe(items.find((item) => item.name === segment)?.id);
  130. },
  131. );
  132. it.each(["public", "personal"] as const)(
  133. "shows all %s cards by default and retains manual toggling",
  134. async (segment) => {
  135. state.locale = locale;
  136. const marketplace = segment === "public" ? ZCODE_OFFICIAL_PLUGIN_MARKETPLACE_ID : "my-market";
  137. const items: StorePluginItem[] = Array.from({ length: 8 }, (_, index) => ({
  138. id: `item-${index}@${marketplace}`,
  139. name: `item-${index}`,
  140. marketplace,
  141. installed: false,
  142. restorable: false,
  143. orphaned: false,
  144. listing: { category: "utilities" },
  145. }));
  146. const container = document.createElement("div");
  147. root = createRoot(container);
  148. const render = (query = "") =>
  149. act(() =>
  150. root?.render(
  151. createElement(PluginStoreListView, {
  152. items,
  153. marketplaces: [],
  154. actions: {} as PluginStoreActions,
  155. loading: false,
  156. query,
  157. segment,
  158. onQueryChange: vi.fn(),
  159. onSegmentChange: vi.fn(),
  160. }),
  161. ),
  162. );
  163. const count = () => container.querySelectorAll('[data-testid="card"]').length;
  164. const toggle = () =>
  165. act(() =>
  166. container
  167. .querySelector<HTMLButtonElement>('[data-testid="plugin-store-group-toggle"]')!
  168. .click(),
  169. );
  170. await render();
  171. expect(count()).toBe(8);
  172. await toggle();
  173. expect(count()).toBe(6);
  174. await render("item-7");
  175. expect(count()).toBe(1);
  176. await render();
  177. expect(count()).toBe(6);
  178. await toggle();
  179. expect(count()).toBe(8);
  180. },
  181. );
  182. });