mentionPanel.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { describe, expect, it } from "vitest";
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import {
  5. buildVirtualRows,
  6. MentionPanel,
  7. type MentionPanelSection,
  8. } from "../src/mentions/components/MentionPanel.js";
  9. import { resolveVerticalScrollMaskState } from "../src/mentions/components/scrollMask.js";
  10. function makeOptions(count: number) {
  11. return Array.from({ length: count }, (_, i) => ({
  12. id: `opt-${i}`,
  13. label: `Option ${i}`,
  14. description: `Description ${i}`,
  15. }));
  16. }
  17. describe("buildVirtualRows", () => {
  18. it("keeps the project heading when # has only one matching group", () => {
  19. const rows = buildVirtualRows([
  20. {
  21. id: "project-p",
  22. title: "Project p",
  23. options: makeOptions(1),
  24. emptyText: "Empty",
  25. showHeader: true,
  26. },
  27. ]);
  28. expect(rows[0]).toEqual({ kind: "section_header", sectionId: "project-p", title: "Project p" });
  29. });
  30. it("returns empty array for empty sections", () => {
  31. expect(buildVirtualRows([])).toEqual([]);
  32. });
  33. it("produces one option row per option with correct flatOptionIndex", () => {
  34. const sections: MentionPanelSection[] = [
  35. { id: "files", title: "Files", options: makeOptions(3), emptyText: "No files" },
  36. ];
  37. const rows = buildVirtualRows(sections);
  38. expect(rows).toHaveLength(3);
  39. expect(rows.every((r) => r.kind === "option")).toBe(true);
  40. const indices = rows
  41. .filter((r): r is Extract<typeof r, { kind: "option" }> => r.kind === "option")
  42. .map((r) => r.flatOptionIndex);
  43. expect(indices).toEqual([0, 1, 2]);
  44. });
  45. it("shows loading status row when section is loading", () => {
  46. const sections: MentionPanelSection[] = [
  47. {
  48. id: "files",
  49. title: "Files",
  50. options: [],
  51. emptyText: "No files",
  52. loading: true,
  53. loadingText: "Loading...",
  54. },
  55. ];
  56. const rows = buildVirtualRows(sections);
  57. expect(rows).toHaveLength(1);
  58. expect(rows[0]).toMatchObject({ kind: "status", content: "loading", text: "Loading..." });
  59. });
  60. it("shows error status row when section has error", () => {
  61. const sections: MentionPanelSection[] = [
  62. {
  63. id: "files",
  64. title: "Files",
  65. options: [],
  66. emptyText: "No files",
  67. errorText: "Failed to load",
  68. },
  69. ];
  70. const rows = buildVirtualRows(sections);
  71. expect(rows).toHaveLength(1);
  72. expect(rows[0]).toMatchObject({ kind: "status", content: "error", text: "Failed to load" });
  73. });
  74. it("shows empty status row when section has no options and is not loading", () => {
  75. const sections: MentionPanelSection[] = [
  76. { id: "files", title: "Files", options: [], emptyText: "No files found" },
  77. ];
  78. const rows = buildVirtualRows(sections);
  79. expect(rows).toHaveLength(1);
  80. expect(rows[0]).toMatchObject({ kind: "status", content: "empty", text: "No files found" });
  81. });
  82. it("assigns continuous flatOptionIndex across multiple sections", () => {
  83. const sections: MentionPanelSection[] = [
  84. { id: "files", title: "Files", options: makeOptions(2), emptyText: "" },
  85. { id: "symbols", title: "Symbols", options: makeOptions(3), emptyText: "" },
  86. ];
  87. const rows = buildVirtualRows(sections);
  88. const optionRows = rows.filter(
  89. (r): r is Extract<typeof r, { kind: "option" }> => r.kind === "option",
  90. );
  91. expect(optionRows.map((r) => r.flatOptionIndex)).toEqual([0, 1, 2, 3, 4]);
  92. });
  93. it("handles large option sets efficiently", () => {
  94. const sections: MentionPanelSection[] = [
  95. { id: "files", title: "Files", options: makeOptions(1000), emptyText: "" },
  96. ];
  97. const start = performance.now();
  98. const rows = buildVirtualRows(sections);
  99. const elapsed = performance.now() - start;
  100. expect(rows).toHaveLength(1000);
  101. expect(elapsed).toBeLessThan(50);
  102. });
  103. it("renders the optional description after the virtualized list", () => {
  104. const html = renderToStaticMarkup(
  105. createElement(MentionPanel, {
  106. title: "Mentions",
  107. description: "Type to search",
  108. trigger: "@",
  109. sections: [
  110. {
  111. id: "files",
  112. title: "Files",
  113. options: makeOptions(1),
  114. emptyText: "No files",
  115. },
  116. ],
  117. emptyText: "",
  118. selectedIndex: 0,
  119. hasActiveQuery: false,
  120. onSelect: () => undefined,
  121. }),
  122. );
  123. expect(html.indexOf('role="listbox"')).toBeGreaterThanOrEqual(0);
  124. expect(html.indexOf("Type to search")).toBeGreaterThanOrEqual(0);
  125. expect(html.indexOf('role="listbox"')).toBeLessThan(html.indexOf("Type to search"));
  126. });
  127. });
  128. describe("resolveVerticalScrollMaskState", () => {
  129. it("does not show masks when content does not overflow", () => {
  130. expect(
  131. resolveVerticalScrollMaskState({
  132. clientHeight: 100,
  133. scrollHeight: 100,
  134. scrollTop: 0,
  135. }),
  136. ).toEqual({ showBottom: false, showTop: false });
  137. });
  138. it("shows only the bottom mask at the top", () => {
  139. expect(
  140. resolveVerticalScrollMaskState({
  141. clientHeight: 100,
  142. scrollHeight: 240,
  143. scrollTop: 0,
  144. }),
  145. ).toEqual({ showBottom: true, showTop: false });
  146. });
  147. it("shows both masks in the middle", () => {
  148. expect(
  149. resolveVerticalScrollMaskState({
  150. clientHeight: 100,
  151. scrollHeight: 240,
  152. scrollTop: 64,
  153. }),
  154. ).toEqual({ showBottom: true, showTop: true });
  155. });
  156. it("shows only the top mask at the bottom", () => {
  157. expect(
  158. resolveVerticalScrollMaskState({
  159. clientHeight: 100,
  160. scrollHeight: 240,
  161. scrollTop: 140,
  162. }),
  163. ).toEqual({ showBottom: false, showTop: true });
  164. });
  165. });