Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

UsageProviderChart.browser.test.tsx

59 lines1.8 KB

1import "~/index.css";
2​
3import { formatDayShort } from "@t3tools/shared/usageFormat";
4import { expect, it } from "vite-plus/test";
5import { page } from "vite-plus/test/browser";
6​
7import { render } from "../../../test/render";
8import { UsageProviderChart } from "./UsageProviderChart";
9​
10const daily = Array.from({ length: 90 }, (_, index) => ({
11 day: new Date(Date.UTC(2026, 7, index + 1)).toISOString().slice(0, 10),
12 costUsd: index + 1,
13 totalTokens: 0,
14 byProvider: new Map([["codex" as const, { costUsd: index + 1, totalTokens: 0 }]]),
15}));
16​
17function chart(length: number) {
18 const visible = daily.slice(-length);
19 return (
20 <div style={{ width: 960 }}>
21 <UsageProviderChart
22 providers={["codex"]}
23 days={visible.map(({ day }) => day)}
24 daily={visible}
25 hours={[]}
26 hourly={[]}
27 metric="cost"
28 referenceTime={undefined}
29 resolution="day"
30 timeZone="UTC"
31 hour12={false}
32 />
33 </div>
34 );
36​
37it.each([
38 { from: 90, to: 7, before: 67, after: 5 },
39 { from: 7, to: 90, before: 5, after: 67 },
40])(
41 "keeps a stationary hover on the bucket under the pointer from $from to $to days",
42 async ({ from, to, before, after }) => {
43 await using screen = await render(chart(from));
44 const svg = screen.container.querySelector('svg[viewBox="0 0 960 260"]')!;
45 const plot = svg.parentElement!;
46 const tooltipLabel = () => plot.querySelector(":scope > div > div")?.textContent;
47 const labelAt = (length: number, index: number) =>
48 formatDayShort(daily.slice(-length)[index]!.day);
49​
50 await page.elementLocator(svg).hover({
51 position: { x: Math.ceil(plot.getBoundingClientRect().width * 0.75), y: 100 },
52 });
53 expect(tooltipLabel()).toBe(labelAt(from, before));
54​
55 await screen.rerender(chart(to));
56​
57 expect(tooltipLabel()).toBe(labelAt(to, after));
58 },
59);