codingPlanQuotaResetStatus.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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: ({ id }: { id: string }, values?: Record<string, string>) => {
  12. const messages: Record<string, string> = {
  13. "codingPlan.quotaReset.processing": "正在重置 5 小时额度…",
  14. "codingPlan.quotaReset.done": "5 小时额度已重置",
  15. "codingPlan.quotaReset.completed": "已重置",
  16. "codingPlan.quotaReset.completedAt": `${values?.time ?? ""} 已重置`,
  17. };
  18. return messages[id] ?? id;
  19. },
  20. },
  21. }),
  22. }));
  23. afterEach(() => {
  24. cleanup();
  25. });
  26. describe("CodingPlanQuotaResetStatusContent", () => {
  27. it("processing 渲染状态提示文案", () => {
  28. render(createElement(CodingPlanQuotaResetStatusContent, { status: "processing" }));
  29. expect(screen.getByRole("status").textContent).toContain("正在重置 5 小时额度…");
  30. });
  31. it("completed 渲染已重置状态文案", () => {
  32. render(createElement(CodingPlanQuotaResetStatusContent, { status: "completed" }));
  33. const status = screen.getByRole("status");
  34. expect(status.textContent).toContain("5 小时额度已重置");
  35. });
  36. });
  37. describe("burstCodingPlanQuotaResetConfetti", () => {
  38. let originalAnimate: typeof Element.prototype.animate;
  39. beforeEach(() => {
  40. originalAnimate = Element.prototype.animate;
  41. Element.prototype.animate = vi.fn().mockReturnValue({
  42. finished: Promise.resolve(),
  43. }) as unknown as typeof Element.prototype.animate;
  44. });
  45. afterEach(() => {
  46. Element.prototype.animate = originalAnimate;
  47. document.body.replaceChildren();
  48. vi.unstubAllGlobals();
  49. });
  50. it("prefers-reduced-motion 时不撒花", () => {
  51. vi.stubGlobal("matchMedia", vi.fn().mockReturnValue({ matches: true }));
  52. const origin = document.createElement("div");
  53. burstCodingPlanQuotaResetConfetti(origin);
  54. expect(document.body.childElementCount).toBe(0);
  55. expect(Element.prototype.animate).not.toHaveBeenCalled();
  56. });
  57. it("允许动效时从触发器播放粒子", () => {
  58. vi.stubGlobal("matchMedia", vi.fn().mockReturnValue({ matches: false }));
  59. const origin = document.createElement("div");
  60. burstCodingPlanQuotaResetConfetti(origin);
  61. expect(document.body.childElementCount).toBeGreaterThan(0);
  62. expect(Element.prototype.animate).toHaveBeenCalled();
  63. });
  64. });