automationScheduleBadge.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // @vitest-environment jsdom
  2. import { Fragment, createElement, type ReactNode } from "react";
  3. import { act, cleanup, render, screen } from "@testing-library/react";
  4. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  5. const resizeObserverState = vi.hoisted(() => ({
  6. callback: null as ResizeObserverCallback | null,
  7. }));
  8. vi.mock("@/components/ui/tooltip.js", () => ({
  9. TooltipProvider: ({ children }: { children: ReactNode }) =>
  10. createElement(Fragment, null, children),
  11. Tooltip: ({ children }: { children: ReactNode }) => createElement(Fragment, null, children),
  12. TooltipTrigger: ({ children }: { children: ReactNode }) =>
  13. createElement(Fragment, null, children),
  14. TooltipContent: ({ children }: { children: ReactNode }) =>
  15. createElement("div", { "data-testid": "automation-schedule-tooltip" }, children),
  16. }));
  17. import { AutomationScheduleBadge } from "@/settings/AutomationScheduleBadge.js";
  18. class ResizeObserverMock {
  19. constructor(callback: ResizeObserverCallback) {
  20. resizeObserverState.callback = callback;
  21. }
  22. observe() {}
  23. unobserve() {}
  24. disconnect() {}
  25. }
  26. function renderBadge() {
  27. render(
  28. createElement(AutomationScheduleBadge, {
  29. text: "每小时的第 05 分 · 下次运行 55 分钟后",
  30. icon: createElement("svg", { "aria-hidden": "true" }),
  31. }),
  32. );
  33. }
  34. function setTextWidths({ clientWidth, scrollWidth }: { clientWidth: number; scrollWidth: number }) {
  35. const text = screen.getByTestId("automation-schedule-text");
  36. Object.defineProperties(text, {
  37. clientWidth: { configurable: true, value: clientWidth },
  38. scrollWidth: { configurable: true, value: scrollWidth },
  39. });
  40. }
  41. function notifyResize() {
  42. act(() => {
  43. resizeObserverState.callback?.([], {} as ResizeObserver);
  44. });
  45. }
  46. beforeEach(() => {
  47. resizeObserverState.callback = null;
  48. vi.stubGlobal("ResizeObserver", ResizeObserverMock);
  49. });
  50. afterEach(() => {
  51. cleanup();
  52. vi.unstubAllGlobals();
  53. });
  54. describe("AutomationScheduleBadge", () => {
  55. it("does not render a tooltip when the schedule text fits", () => {
  56. renderBadge();
  57. setTextWidths({ clientWidth: 240, scrollWidth: 240 });
  58. notifyResize();
  59. expect(screen.queryByTestId("automation-schedule-tooltip")).toBeNull();
  60. });
  61. it("renders the tooltip only while the schedule text is truncated", () => {
  62. renderBadge();
  63. setTextWidths({ clientWidth: 160, scrollWidth: 240 });
  64. notifyResize();
  65. expect(screen.getByTestId("automation-schedule-tooltip").textContent).toBe(
  66. "每小时的第 05 分 · 下次运行 55 分钟后",
  67. );
  68. setTextWidths({ clientWidth: 240, scrollWidth: 240 });
  69. notifyResize();
  70. expect(screen.queryByTestId("automation-schedule-tooltip")).toBeNull();
  71. });
  72. });