codingPlanQuotaResetAction.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // @vitest-environment jsdom
  2. import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { CodingPlanQuotaResetAction } from "@/components/coding-plan-quota-reset/CodingPlanQuotaResetAction.js";
  6. import { TooltipProvider } from "@/components/ui/tooltip.js";
  7. function installAnimationFrameController() {
  8. let nextFrameId = 1;
  9. const callbacks = new Map<number, FrameRequestCallback>();
  10. const requestAnimationFrame = vi.fn((callback: FrameRequestCallback) => {
  11. const frameId = nextFrameId;
  12. nextFrameId += 1;
  13. callbacks.set(frameId, callback);
  14. return frameId;
  15. });
  16. const cancelAnimationFrame = vi.fn((frameId: number) => {
  17. callbacks.delete(frameId);
  18. });
  19. vi.stubGlobal("requestAnimationFrame", requestAnimationFrame);
  20. vi.stubGlobal("cancelAnimationFrame", cancelAnimationFrame);
  21. return {
  22. cancelAnimationFrame,
  23. flush() {
  24. const pendingCallbacks = [...callbacks.values()];
  25. callbacks.clear();
  26. for (const callback of pendingCallbacks) {
  27. callback(performance.now());
  28. }
  29. },
  30. };
  31. }
  32. describe("CodingPlanQuotaResetAction", () => {
  33. afterEach(() => {
  34. vi.useRealTimers();
  35. vi.unstubAllGlobals();
  36. cleanup();
  37. });
  38. it("重置按钮复用等待请求的确认色", () => {
  39. render(
  40. createElement(CodingPlanQuotaResetAction, {
  41. ariaLabel: "重置 5 小时额度",
  42. completedAt: null,
  43. completedLabel: "已重置",
  44. processing: false,
  45. processingLabel: "正在重置",
  46. resetLabel: "重置",
  47. successLabel: "重置成功",
  48. onReset: vi.fn(async () => {}),
  49. }),
  50. );
  51. const button = screen.getByRole("button", { name: "重置 5 小时额度" });
  52. expect(button.className).toContain("bg-interaction-confirmation-surface");
  53. expect(button.className).toContain("text-interaction-confirmation-foreground");
  54. });
  55. it("页面重置入口只打开统一弹窗,不直接核销", () => {
  56. const onOpenDialog = vi.fn();
  57. render(
  58. createElement(CodingPlanQuotaResetAction, {
  59. ariaLabel: "重置 5 小时额度",
  60. completedAt: null,
  61. completedLabel: "已重置",
  62. processing: false,
  63. processingLabel: "正在重置",
  64. resetLabel: "重置",
  65. successLabel: "重置成功",
  66. onOpenDialog,
  67. }),
  68. );
  69. fireEvent.click(screen.getByRole("button", { name: "重置 5 小时额度" }));
  70. expect(onOpenDialog).toHaveBeenCalledTimes(1);
  71. });
  72. it("服务端重置成功后展示短时成功勾选并撒花,再进入已重置状态", async () => {
  73. vi.useFakeTimers();
  74. let resolveReset!: () => void;
  75. const onReset = vi.fn(
  76. () =>
  77. new Promise<void>((resolve) => {
  78. resolveReset = resolve;
  79. }),
  80. );
  81. const celebrate = vi.fn();
  82. const view = render(
  83. createElement(CodingPlanQuotaResetAction, {
  84. ariaLabel: "重置 5 小时额度",
  85. completedAt: null,
  86. completedLabel: "已重置",
  87. processing: false,
  88. processingLabel: "正在重置",
  89. resetLabel: "重置",
  90. successLabel: "重置成功",
  91. onReset,
  92. onCelebrate: celebrate,
  93. }),
  94. );
  95. fireEvent.click(screen.getByRole("button", { name: "重置 5 小时额度" }));
  96. expect(onReset).toHaveBeenCalledTimes(1);
  97. expect(celebrate).not.toHaveBeenCalled();
  98. expect((screen.getByRole("button", { name: "正在重置" }) as HTMLButtonElement).disabled).toBe(
  99. true,
  100. );
  101. await act(async () => resolveReset());
  102. view.rerender(
  103. createElement(CodingPlanQuotaResetAction, {
  104. ariaLabel: "重置 5 小时额度",
  105. completedAt: new Date("2026-08-07T19:08:01+08:00").getTime(),
  106. completedLabel: "已重置",
  107. processing: false,
  108. processingLabel: "正在重置",
  109. resetLabel: "重置",
  110. successLabel: "重置成功",
  111. onReset,
  112. onCelebrate: celebrate,
  113. }),
  114. );
  115. expect(celebrate).toHaveBeenCalledTimes(1);
  116. expect(screen.getByRole("button", { name: "重置成功" })).toBeTruthy();
  117. expect(screen.queryByText("已重置")).toBeNull();
  118. act(() => vi.advanceTimersByTime(599));
  119. expect(screen.getByRole("button", { name: "重置成功" })).toBeTruthy();
  120. act(() => vi.advanceTimersByTime(1));
  121. expect(screen.getByText("已重置")).toBeTruthy();
  122. });
  123. it("请求失败后恢复可点击,不显示成功态也不撒花", async () => {
  124. const onReset = vi.fn(async () => {
  125. throw new Error("reset failed");
  126. });
  127. const celebrate = vi.fn();
  128. render(
  129. createElement(CodingPlanQuotaResetAction, {
  130. ariaLabel: "重置 5 小时额度",
  131. completedAt: null,
  132. completedLabel: "已重置",
  133. processing: false,
  134. processingLabel: "正在重置",
  135. resetLabel: "重置",
  136. successLabel: "重置成功",
  137. onReset,
  138. onCelebrate: celebrate,
  139. }),
  140. );
  141. await act(async () => {
  142. fireEvent.click(screen.getByRole("button", { name: "重置 5 小时额度" }));
  143. });
  144. expect(
  145. (screen.getByRole("button", { name: "重置 5 小时额度" }) as HTMLButtonElement).disabled,
  146. ).toBe(false);
  147. expect(screen.queryByRole("status")).toBeNull();
  148. expect(celebrate).not.toHaveBeenCalled();
  149. });
  150. it("已完成状态不会再次提供消费按钮", () => {
  151. render(
  152. createElement(CodingPlanQuotaResetAction, {
  153. ariaLabel: "重置 5 小时额度",
  154. completedAt: new Date("2026-08-07T19:08:00+08:00").getTime(),
  155. completedLabel: "已重置",
  156. processing: false,
  157. processingLabel: "正在重置",
  158. resetLabel: "重置",
  159. successLabel: "重置成功",
  160. onReset: vi.fn(async () => undefined),
  161. }),
  162. );
  163. expect(screen.queryByRole("button", { name: "重置 5 小时额度" })).toBeNull();
  164. expect(screen.getByText("已重置")).toBeTruthy();
  165. // 回归:已重置文案必须与「重置」按钮的 xs 尺寸(h-5)等高。曾经的 min-h-6 会把
  166. // 所在额度条的标签行撑高,导致同排没有动作的额度条数值与进度条错位。
  167. expect(screen.getByText("已重置").className).toContain("min-h-5");
  168. expect(screen.getByText("已重置").className).not.toContain("min-h-6");
  169. });
  170. it("自动/运营完成收到补播信号时,等浮层锚点挂载一帧后补播一次撒花且不重播", () => {
  171. const animationFrame = installAnimationFrameController();
  172. const completedAt = new Date("2026-08-07T19:08:00+08:00").getTime();
  173. const celebrate = vi.fn();
  174. const onAutoCelebrated = vi.fn();
  175. const props = {
  176. ariaLabel: "重置 5 小时额度",
  177. autoCelebrateCompletedAt: completedAt,
  178. completedAt,
  179. completedLabel: "已重置",
  180. processing: false,
  181. processingLabel: "正在重置",
  182. resetLabel: "重置",
  183. successLabel: "重置成功",
  184. onAutoCelebrated,
  185. onReset: vi.fn(async () => undefined),
  186. onCelebrate: celebrate,
  187. };
  188. const view = render(createElement(CodingPlanQuotaResetAction, props));
  189. // 回归:浮层刚打开的提交阶段不能提前消费 arm,否则撒花会在面板可见前结束。
  190. expect(celebrate).not.toHaveBeenCalled();
  191. expect(onAutoCelebrated).not.toHaveBeenCalled();
  192. act(() => animationFrame.flush());
  193. expect(celebrate).toHaveBeenCalledTimes(1);
  194. expect(celebrate.mock.calls[0]?.[0]).toBeInstanceOf(HTMLElement);
  195. expect(onAutoCelebrated).toHaveBeenCalledTimes(1);
  196. expect(onAutoCelebrated).toHaveBeenCalledWith(completedAt);
  197. // 同一 used_at 重复渲染不重播。
  198. view.rerender(createElement(CodingPlanQuotaResetAction, props));
  199. expect(celebrate).toHaveBeenCalledTimes(1);
  200. });
  201. it("已重置文案带时间 Tooltip 时也必须等锚点挂载后补播撒花", () => {
  202. const animationFrame = installAnimationFrameController();
  203. const completedAt = new Date("2026-08-17T12:00:00+08:00").getTime();
  204. const celebrate = vi.fn();
  205. const onAutoCelebrated = vi.fn();
  206. render(
  207. createElement(
  208. TooltipProvider,
  209. null,
  210. createElement(CodingPlanQuotaResetAction, {
  211. ariaLabel: "重置 5 小时额度",
  212. autoCelebrateCompletedAt: completedAt,
  213. completedAt,
  214. completedLabel: "已重置",
  215. completedTooltipLabel: "重置时间 12:00",
  216. processing: false,
  217. processingLabel: "正在重置",
  218. resetLabel: "重置",
  219. successLabel: "重置成功",
  220. onAutoCelebrated,
  221. onReset: vi.fn(async () => undefined),
  222. onCelebrate: celebrate,
  223. }),
  224. ),
  225. );
  226. expect(celebrate).not.toHaveBeenCalled();
  227. expect(onAutoCelebrated).not.toHaveBeenCalled();
  228. act(() => animationFrame.flush());
  229. expect(celebrate).toHaveBeenCalledTimes(1);
  230. expect(celebrate.mock.calls[0]?.[0]).toBe(screen.getByText("已重置"));
  231. expect(onAutoCelebrated).toHaveBeenCalledWith(completedAt);
  232. });
  233. it("浮层在补播帧前关闭时不消费自动撒花机会", () => {
  234. const animationFrame = installAnimationFrameController();
  235. const completedAt = new Date("2026-08-17T12:00:00+08:00").getTime();
  236. const celebrate = vi.fn();
  237. const onAutoCelebrated = vi.fn();
  238. const view = render(
  239. createElement(CodingPlanQuotaResetAction, {
  240. ariaLabel: "重置 5 小时额度",
  241. autoCelebrateCompletedAt: completedAt,
  242. completedAt,
  243. completedLabel: "已重置",
  244. processing: false,
  245. processingLabel: "正在重置",
  246. resetLabel: "重置",
  247. successLabel: "重置成功",
  248. onAutoCelebrated,
  249. onReset: vi.fn(async () => undefined),
  250. onCelebrate: celebrate,
  251. }),
  252. );
  253. view.unmount();
  254. act(() => animationFrame.flush());
  255. expect(animationFrame.cancelAnimationFrame).toHaveBeenCalledTimes(1);
  256. expect(celebrate).not.toHaveBeenCalled();
  257. expect(onAutoCelebrated).not.toHaveBeenCalled();
  258. });
  259. it("补播信号与当前 completedAt 不一致或未 arm 时不补播撒花", () => {
  260. const celebrate = vi.fn();
  261. const onAutoCelebrated = vi.fn();
  262. render(
  263. createElement(CodingPlanQuotaResetAction, {
  264. ariaLabel: "重置 5 小时额度",
  265. autoCelebrateCompletedAt: 111,
  266. completedAt: 222,
  267. completedLabel: "已重置",
  268. processing: false,
  269. processingLabel: "正在重置",
  270. resetLabel: "重置",
  271. successLabel: "重置成功",
  272. onAutoCelebrated,
  273. onReset: vi.fn(async () => undefined),
  274. onCelebrate: celebrate,
  275. }),
  276. );
  277. expect(celebrate).not.toHaveBeenCalled();
  278. expect(onAutoCelebrated).not.toHaveBeenCalled();
  279. });
  280. });