codingPlanQuotaResetStatus.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // @vitest-environment jsdom
  2. import { cleanup, render, screen } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  5. import { CodingPlanQuotaResetStatusContent } from "@/components/coding-plan-quota-reset/CodingPlanQuotaResetStatus.js";
  6. import { burstCodingPlanQuotaResetConfetti } from "@/lib/codingPlanQuotaResetConfetti.js";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. locale: "zh-CN",
  10. intl: {
  11. formatMessage: (
  12. { id }: { id: string },
  13. values?: Record<string, string>,
  14. ) => {
  15. const messages: Record<string, string> = {
  16. "codingPlan.quotaReset.processing": "正在重置 5 小时额度…",
  17. "codingPlan.quotaReset.done": "5 小时额度已重置",
  18. "codingPlan.quotaReset.completed": "已重置",
  19. "codingPlan.quotaReset.completedAt": `${values?.time ?? ""} 已重置`,
  20. };
  21. return messages[id] ?? id;
  22. },
  23. },
  24. }),
  25. }));
  26. afterEach(() => {
  27. cleanup();
  28. });
  29. describe("CodingPlanQuotaResetStatusContent", () => {
  30. it("processing 渲染转圈与提示文案", () => {
  31. const { container } = render(
  32. createElement(CodingPlanQuotaResetStatusContent, { status: "processing" }),
  33. );
  34. expect(screen.getByRole("status").textContent).toContain("正在重置 5 小时额度…");
  35. const spinner = container.querySelector(".animate-spin");
  36. expect(spinner).not.toBeNull();
  37. expect(spinner?.classList.contains("motion-reduce:animate-none")).toBe(true);
  38. });
  39. it("completed 渲染绿色勾选与已重置文案", () => {
  40. const { container } = render(
  41. createElement(CodingPlanQuotaResetStatusContent, { status: "completed" }),
  42. );
  43. const status = screen.getByRole("status");
  44. expect(status.textContent).toContain("5 小时额度已重置");
  45. expect(status.className).toContain("text-success");
  46. expect(container.querySelector(".zoom-in-75")).not.toBeNull();
  47. });
  48. });
  49. describe("burstCodingPlanQuotaResetConfetti", () => {
  50. let originalAnimate: typeof Element.prototype.animate;
  51. beforeEach(() => {
  52. originalAnimate = Element.prototype.animate;
  53. Element.prototype.animate = vi.fn().mockReturnValue({
  54. finished: Promise.resolve(),
  55. }) as unknown as typeof Element.prototype.animate;
  56. });
  57. afterEach(() => {
  58. Element.prototype.animate = originalAnimate;
  59. document.body.replaceChildren();
  60. vi.unstubAllGlobals();
  61. });
  62. it("prefers-reduced-motion 时不撒花", () => {
  63. vi.stubGlobal(
  64. "matchMedia",
  65. vi.fn().mockReturnValue({ matches: true }),
  66. );
  67. const origin = document.createElement("div");
  68. burstCodingPlanQuotaResetConfetti(origin);
  69. expect(document.body.childElementCount).toBe(0);
  70. expect(Element.prototype.animate).not.toHaveBeenCalled();
  71. });
  72. it("允许动效时从触发器播放粒子", () => {
  73. vi.stubGlobal(
  74. "matchMedia",
  75. vi.fn().mockReturnValue({ matches: false }),
  76. );
  77. const origin = document.createElement("div");
  78. burstCodingPlanQuotaResetConfetti(origin);
  79. expect(document.body.childElementCount).toBeGreaterThan(0);
  80. expect(Element.prototype.animate).toHaveBeenCalled();
  81. });
  82. });