v4ConversationUserInputBody.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { act, createElement } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import {
  6. COLLAPSED_USER_INPUT_CONTENT_MAX_HEIGHT_PX,
  7. ConversationUserInputBody,
  8. isUserInputContentOverflowing,
  9. resolveUserInputContentMaxHeight,
  10. } from "@/v4/ConversationUserInputBody.js";
  11. const buttonProps = vi.hoisted(() => [] as Array<Record<string, unknown>>);
  12. vi.mock("@/components/ui/button.js", async () => {
  13. const React = await import("react");
  14. return {
  15. Button: ({ size: _size, variant: _variant, ...props }: Record<string, unknown>) => {
  16. buttonProps.push(props);
  17. return React.createElement("button", props, props.children as React.ReactNode);
  18. },
  19. };
  20. });
  21. const layout = {
  22. scrollHeight: 80,
  23. };
  24. let resizeObserverCallback: ResizeObserverCallback | null = null;
  25. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  26. const element = {
  27. addEventListener: () => {},
  28. appendChild: (child: Node) => {
  29. Object.assign(child, { parentNode: element });
  30. element.childNodes.push(child);
  31. return child;
  32. },
  33. childNodes: [] as Node[],
  34. get scrollHeight() {
  35. return layout.scrollHeight;
  36. },
  37. getAttribute: (name: string) => element.attributes.get(name) ?? null,
  38. insertBefore: (child: Node, beforeChild?: Node | null) => {
  39. Object.assign(child, { parentNode: element });
  40. const beforeIndex = beforeChild ? element.childNodes.indexOf(beforeChild) : -1;
  41. if (beforeIndex >= 0) {
  42. element.childNodes.splice(beforeIndex, 0, child);
  43. } else {
  44. element.childNodes.push(child);
  45. }
  46. return child;
  47. },
  48. attributes: new Map<string, string>(),
  49. nodeName: tagName.toUpperCase(),
  50. nodeType: 1,
  51. ownerDocument,
  52. parentNode: null as Node | null,
  53. removeAttribute: (name: string) => {
  54. element.attributes.delete(name);
  55. },
  56. removeChild: (child: Node) => {
  57. element.childNodes = element.childNodes.filter((item) => item !== child);
  58. Object.assign(child, { parentNode: null });
  59. return child;
  60. },
  61. removeEventListener: () => {},
  62. setAttribute: (name: string, value: string) => {
  63. element.attributes.set(name, String(value));
  64. },
  65. style: {},
  66. tagName: tagName.toUpperCase(),
  67. };
  68. return element as unknown as Element;
  69. }
  70. function installMinimalDom() {
  71. const documentMock = {
  72. addEventListener: () => {},
  73. createComment: (nodeValue: string) => ({
  74. nodeType: 8,
  75. nodeValue,
  76. parentNode: null,
  77. }),
  78. createElement: (tagName: string) =>
  79. createMinimalElement(documentMock as unknown as Document, tagName),
  80. createElementNS: (_namespace: string, tagName: string) =>
  81. createMinimalElement(documentMock as unknown as Document, tagName),
  82. createTextNode: (nodeValue: string) => ({
  83. nodeType: 3,
  84. nodeValue,
  85. parentNode: null,
  86. }),
  87. nodeType: 9,
  88. removeEventListener: () => {},
  89. } as unknown as Document & { defaultView?: unknown };
  90. const windowMock = {
  91. addEventListener: () => {},
  92. cancelAnimationFrame: () => {},
  93. document: documentMock,
  94. HTMLIFrameElement: function HTMLIFrameElement() {},
  95. HTMLElement: function HTMLElement() {},
  96. Node: function Node() {},
  97. removeEventListener: () => {},
  98. requestAnimationFrame: (callback: FrameRequestCallback) => {
  99. callback(0);
  100. return 1;
  101. },
  102. };
  103. documentMock.defaultView = windowMock;
  104. Object.defineProperty(globalThis, "document", {
  105. configurable: true,
  106. value: documentMock,
  107. });
  108. Object.defineProperty(globalThis, "window", {
  109. configurable: true,
  110. value: windowMock,
  111. });
  112. Object.defineProperty(globalThis, "ResizeObserver", {
  113. configurable: true,
  114. value: class ResizeObserverMock {
  115. constructor(callback: ResizeObserverCallback) {
  116. resizeObserverCallback = callback;
  117. }
  118. disconnect() {}
  119. observe() {}
  120. },
  121. });
  122. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  123. configurable: true,
  124. value: true,
  125. });
  126. return createMinimalElement(documentMock, "div");
  127. }
  128. function latestButtonProps(): Record<string, unknown> | undefined {
  129. return buttonProps.at(-1);
  130. }
  131. async function renderBody(root: Root, rowId: number, contentText: string) {
  132. await act(async () => {
  133. root.render(
  134. createElement(
  135. ZCodeIntlProvider,
  136. { initialLocale: "zh-CN" },
  137. createElement(
  138. ConversationUserInputBody,
  139. { contentText, rowId },
  140. createElement("span", null, "一段很长的用户正文"),
  141. ),
  142. ),
  143. );
  144. });
  145. }
  146. afterEach(() => {
  147. buttonProps.length = 0;
  148. layout.scrollHeight = 80;
  149. resizeObserverCallback = null;
  150. delete (globalThis as { document?: unknown }).document;
  151. delete (globalThis as { window?: unknown }).window;
  152. delete (globalThis as { ResizeObserver?: unknown }).ResizeObserver;
  153. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  154. });
  155. describe("ConversationUserInputBody", () => {
  156. it("keeps short content uncollapsed and only marks real overflow as expandable", () => {
  157. expect(COLLAPSED_USER_INPUT_CONTENT_MAX_HEIGHT_PX).toBe(120);
  158. expect(isUserInputContentOverflowing(120)).toBe(false);
  159. expect(isUserInputContentOverflowing(121)).toBe(false);
  160. expect(isUserInputContentOverflowing(122)).toBe(true);
  161. expect(resolveUserInputContentMaxHeight(false, 320)).toBe("120px");
  162. expect(resolveUserInputContentMaxHeight(true, 320)).toBe("320px");
  163. expect(resolveUserInputContentMaxHeight(true, 80)).toBe("120px");
  164. });
  165. it("reacts to measured overflow and toggles between expand and collapse", async () => {
  166. layout.scrollHeight = 320;
  167. const root = createRoot(installMinimalDom());
  168. await renderBody(root, 1, "long");
  169. expect(latestButtonProps()?.["aria-expanded"]).toBe(false);
  170. expect(latestButtonProps()?.["aria-label"]).toBe("展开");
  171. await act(async () => {
  172. (latestButtonProps()?.onClick as (() => void) | undefined)?.();
  173. });
  174. expect(latestButtonProps()?.["aria-expanded"]).toBe(true);
  175. expect(latestButtonProps()?.["aria-label"]).toBe("收起");
  176. await act(async () => {
  177. root.unmount();
  178. });
  179. });
  180. it("remeasures resize changes and resets expansion when row content changes", async () => {
  181. const root = createRoot(installMinimalDom());
  182. await renderBody(root, 1, "short");
  183. expect(buttonProps).toHaveLength(0);
  184. layout.scrollHeight = 280;
  185. await act(async () => {
  186. resizeObserverCallback?.([], {} as ResizeObserver);
  187. });
  188. expect(latestButtonProps()?.["aria-expanded"]).toBe(false);
  189. await act(async () => {
  190. (latestButtonProps()?.onClick as (() => void) | undefined)?.();
  191. });
  192. expect(latestButtonProps()?.["aria-expanded"]).toBe(true);
  193. await renderBody(root, 2, "new-content");
  194. expect(latestButtonProps()?.["aria-expanded"]).toBe(false);
  195. await act(async () => {
  196. root.unmount();
  197. });
  198. });
  199. });