taskTitleOverflowText.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import { createElement } from "react";
  5. import { TaskTitleOverflowText } from "@/components/TaskTitleOverflowText.js";
  6. const resizeObserverState = vi.hoisted(() => ({
  7. callback: null as ResizeObserverCallback | null,
  8. }));
  9. const cancelAnimation = vi.fn();
  10. const animateTrack = vi.fn(() => ({ cancel: cancelAnimation }) as unknown as Animation);
  11. const originalAnimateDescriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "animate");
  12. class ResizeObserverMock {
  13. constructor(callback: ResizeObserverCallback) {
  14. resizeObserverState.callback = callback;
  15. }
  16. observe() {}
  17. unobserve() {}
  18. disconnect() {}
  19. }
  20. function setTitleWidths({
  21. clientWidth,
  22. contentWidth,
  23. }: {
  24. clientWidth: number;
  25. contentWidth: number;
  26. }) {
  27. const title = screen.getByTestId("task-title-overflow-text");
  28. const original = title.querySelector('[data-task-title-copy="original"]');
  29. Object.defineProperty(title, "clientWidth", {
  30. configurable: true,
  31. value: clientWidth,
  32. });
  33. Object.defineProperty(original, "scrollWidth", {
  34. configurable: true,
  35. value: contentWidth,
  36. });
  37. }
  38. function notifyResize() {
  39. act(() => {
  40. resizeObserverState.callback?.([], {} as ResizeObserver);
  41. });
  42. }
  43. beforeEach(() => {
  44. resizeObserverState.callback = null;
  45. animateTrack.mockClear();
  46. cancelAnimation.mockClear();
  47. vi.stubGlobal("ResizeObserver", ResizeObserverMock);
  48. Object.defineProperty(HTMLElement.prototype, "animate", {
  49. configurable: true,
  50. value: animateTrack,
  51. });
  52. });
  53. afterEach(() => {
  54. cleanup();
  55. vi.useRealTimers();
  56. vi.unstubAllGlobals();
  57. if (originalAnimateDescriptor) {
  58. Object.defineProperty(HTMLElement.prototype, "animate", originalAnimateDescriptor);
  59. } else {
  60. delete (HTMLElement.prototype as { animate?: unknown }).animate;
  61. }
  62. });
  63. describe("TaskTitleOverflowText", () => {
  64. it("does not render a native title tooltip", () => {
  65. render(
  66. createElement(
  67. TaskTitleOverflowText,
  68. { "data-testid": "task-title-overflow-text", title: "完整任务名称" },
  69. "很长的任务标题",
  70. ),
  71. );
  72. expect(screen.getByTestId("task-title-overflow-text").getAttribute("title")).toBeNull();
  73. });
  74. it("holds the aligned duplicate for two seconds before the seamless reset", () => {
  75. vi.useFakeTimers();
  76. render(
  77. createElement(
  78. "div",
  79. { "data-task-item-key": "task-1", "data-testid": "task-item" },
  80. createElement(
  81. TaskTitleOverflowText,
  82. { "data-testid": "task-title-overflow-text" },
  83. "很长的任务标题",
  84. ),
  85. ),
  86. );
  87. setTitleWidths({ clientWidth: 120, contentWidth: 240 });
  88. notifyResize();
  89. const taskItem = screen.getByTestId("task-item");
  90. fireEvent.mouseEnter(taskItem);
  91. expect(animateTrack).not.toHaveBeenCalled();
  92. act(() => vi.advanceTimersByTime(999));
  93. expect(animateTrack).not.toHaveBeenCalled();
  94. act(() => vi.advanceTimersByTime(1));
  95. expect(animateTrack).toHaveBeenCalledTimes(2);
  96. expect(animateTrack.mock.calls[0]?.[0]).toEqual([
  97. { offset: 0, transform: "translate3d(0, 0, 0)" },
  98. {
  99. offset: 6.6 / 8.6,
  100. transform: "translate3d(-264px, 0, 0)",
  101. },
  102. { offset: 1, transform: "translate3d(-264px, 0, 0)" },
  103. ]);
  104. expect(animateTrack.mock.calls[0]?.[1]).toEqual({
  105. duration: 8_600,
  106. easing: "linear",
  107. iterations: Infinity,
  108. });
  109. expect(animateTrack.mock.calls[1]?.[0]).toEqual([
  110. {
  111. maskImage:
  112. "linear-gradient(to right, black 0, black calc(100% - 1.5rem), transparent 100%)",
  113. offset: 0,
  114. webkitMaskImage:
  115. "linear-gradient(to right, black 0, black calc(100% - 1.5rem), transparent 100%)",
  116. },
  117. {
  118. maskImage:
  119. "linear-gradient(to right, transparent 0, black 1.5rem, black calc(100% - 1.5rem), transparent 100%)",
  120. offset: 0.15 / 8.6,
  121. webkitMaskImage:
  122. "linear-gradient(to right, transparent 0, black 1.5rem, black calc(100% - 1.5rem), transparent 100%)",
  123. },
  124. {
  125. maskImage:
  126. "linear-gradient(to right, transparent 0, black 1.5rem, black calc(100% - 1.5rem), transparent 100%)",
  127. offset: (6.6 * (240 / 264)) / 8.6,
  128. webkitMaskImage:
  129. "linear-gradient(to right, transparent 0, black 1.5rem, black calc(100% - 1.5rem), transparent 100%)",
  130. },
  131. {
  132. maskImage:
  133. "linear-gradient(to right, black 0, black calc(100% - 1.5rem), transparent 100%)",
  134. offset: (6.6 * (240 / 264)) / 8.6,
  135. webkitMaskImage:
  136. "linear-gradient(to right, black 0, black calc(100% - 1.5rem), transparent 100%)",
  137. },
  138. {
  139. maskImage:
  140. "linear-gradient(to right, black 0, black calc(100% - 1.5rem), transparent 100%)",
  141. offset: 1,
  142. webkitMaskImage:
  143. "linear-gradient(to right, black 0, black calc(100% - 1.5rem), transparent 100%)",
  144. },
  145. ]);
  146. expect(animateTrack.mock.calls[1]?.[1]).toEqual({
  147. duration: 8_600,
  148. easing: "linear",
  149. iterations: Infinity,
  150. });
  151. fireEvent.mouseLeave(taskItem);
  152. expect(cancelAnimation).toHaveBeenCalledTimes(2);
  153. });
  154. it("cancels the delayed start when hover ends before one second", () => {
  155. vi.useFakeTimers();
  156. render(
  157. createElement(
  158. "div",
  159. { "data-task-item-key": "task-1", "data-testid": "task-item" },
  160. createElement(
  161. TaskTitleOverflowText,
  162. { "data-testid": "task-title-overflow-text" },
  163. "很长的任务标题",
  164. ),
  165. ),
  166. );
  167. setTitleWidths({ clientWidth: 120, contentWidth: 240 });
  168. notifyResize();
  169. const taskItem = screen.getByTestId("task-item");
  170. fireEvent.mouseEnter(taskItem);
  171. act(() => vi.advanceTimersByTime(300));
  172. fireEvent.mouseLeave(taskItem);
  173. act(() => vi.advanceTimersByTime(1_000));
  174. expect(animateTrack).not.toHaveBeenCalled();
  175. });
  176. it("does not animate when reduced motion is requested", () => {
  177. vi.stubGlobal(
  178. "matchMedia",
  179. vi.fn(() => ({
  180. addEventListener: vi.fn(),
  181. matches: true,
  182. removeEventListener: vi.fn(),
  183. })),
  184. );
  185. render(
  186. createElement(
  187. TaskTitleOverflowText,
  188. { "data-testid": "task-title-overflow-text" },
  189. "很长的任务标题",
  190. ),
  191. );
  192. setTitleWidths({ clientWidth: 120, contentWidth: 240 });
  193. notifyResize();
  194. fireEvent.mouseEnter(screen.getByTestId("task-title-overflow-text"));
  195. expect(animateTrack).not.toHaveBeenCalled();
  196. });
  197. it("does not apply a fade mask while the title fits", () => {
  198. render(
  199. createElement(TaskTitleOverflowText, { "data-testid": "task-title-overflow-text" }, "短标题"),
  200. );
  201. setTitleWidths({ clientWidth: 160, contentWidth: 120 });
  202. notifyResize();
  203. const title = screen.getByTestId("task-title-overflow-text");
  204. expect(title.className).not.toContain("mask-image");
  205. expect(title.querySelectorAll("[data-task-title-copy]")).toHaveLength(1);
  206. });
  207. it("renders an original title and an aria-hidden copy only while overflowing", () => {
  208. render(
  209. createElement(
  210. TaskTitleOverflowText,
  211. { "data-testid": "task-title-overflow-text" },
  212. "很长的任务标题",
  213. ),
  214. );
  215. setTitleWidths({ clientWidth: 120, contentWidth: 240 });
  216. notifyResize();
  217. const title = screen.getByTestId("task-title-overflow-text");
  218. const copies = title.querySelectorAll("[data-task-title-copy]");
  219. const duplicate = title.querySelector('[data-task-title-copy="duplicate"]');
  220. const track = title.querySelector<HTMLElement>("[data-task-title-marquee-track]");
  221. expect(title.className).toContain("mask-image:linear-gradient");
  222. expect(copies).toHaveLength(2);
  223. expect(copies[0]?.textContent).toBe("很长的任务标题");
  224. expect(copies[1]?.textContent).toBe("很长的任务标题");
  225. expect(duplicate?.getAttribute("aria-hidden")).toBe("true");
  226. expect(track).not.toBeNull();
  227. setTitleWidths({ clientWidth: 240, contentWidth: 240 });
  228. notifyResize();
  229. expect(title.className).not.toContain("mask-image");
  230. expect(title.querySelectorAll("[data-task-title-copy]")).toHaveLength(1);
  231. });
  232. it("supports the span element used by grouped task rows", () => {
  233. render(
  234. createElement(
  235. TaskTitleOverflowText,
  236. { as: "span", "data-testid": "task-title-overflow-text" },
  237. "Grouped task title",
  238. ),
  239. );
  240. expect(screen.getByTestId("task-title-overflow-text").tagName).toBe("SPAN");
  241. });
  242. });