import { describe, expect, it } from "vitest"; import { render } from "@testing-library/react"; import { ChartStat, ChartStatRow } from "./chart-stat"; // Load-bearing: without the app's Tailwind neither the row's flex-wrap nor the // inline grid columns exist, and every width would "fit". import "@/app/globals.css"; function GasRow({ width }: { width: number }) { return (
20% vs Oct 2025} > 18.2 L/min 12.4 L/min 31
); } function placement(container: HTMLElement) { return container .querySelector(".group\\/stats") ?.getAttribute("data-detail"); } function lines(container: HTMLElement) { const tops = Array.from( container.querySelectorAll("[data-chart-stat]"), (stat) => stat.offsetTop, ); return new Set(tops).size; } describe("ChartStatRow", () => { it("puts the detail beside the figure when the row has room", () => { const { container } = render(); expect(placement(container)).toBe("inline"); expect(lines(container)).toBe(1); }); it("stacks the detail when inline would push a stat to another line", () => { // Wide enough for all three stats with the detail stacked, not inline. const { container } = render(); expect(placement(container)).toBe("stacked"); expect(lines(container)).toBe(1); }); // Resized behind React's back, as a window resize is: no render happens, so // only the ResizeObserver can catch it. it("re-decides when the row's width changes", async () => { const { container } = render(); const box = container.firstElementChild as HTMLElement; expect(placement(container)).toBe("stacked"); box.style.width = "600px"; await expect.poll(() => placement(container)).toBe("inline"); box.style.width = "330px"; await expect.poll(() => placement(container)).toBe("stacked"); }); });