diff --git a/src/components/custom-metrics/__tests__/custom-metric-chart-color.browser.test.tsx b/src/components/custom-metrics/__tests__/custom-metric-chart-color.browser.test.tsx deleted file mode 100644 index 64828318a..000000000 --- a/src/components/custom-metrics/__tests__/custom-metric-chart-color.browser.test.tsx +++ /dev/null @@ -1,51 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { renderWithApp } from "../../../../tests/browser/render"; -import { CustomMetricChart } from "../custom-metric-chart"; -import type { CustomMetricEntryDto } from "../types"; - -/** - * The line takes the series colour it is given (the GKI draws in a chart - * token beside the ketone series) and keeps primary when it is given none, - * so a custom metric's chart looks as it always has. - */ - -const NOW = Date.now(); -const ENTRIES: CustomMetricEntryDto[] = [3, 2, 1].map((daysAgo, i) => ({ - id: `e${i}`, - customMetricId: "", - value: 2 + i, - unit: "", - measuredAt: new Date(NOW - daysAgo * 86_400_000).toISOString(), - note: null, - createdAt: new Date(NOW).toISOString(), -})); - -async function lineStroke(color?: string): Promise { - const { screen } = await renderWithApp( -
- -
, - ); - const curve = () => screen.container.querySelector(".recharts-line-curve"); - await expect.poll(curve).not.toBeNull(); - return curve()!.getAttribute("stroke"); -} - -describe(" series colour", () => { - it("draws the line in the colour it is given", async () => { - expect(await lineStroke("var(--chart-4)")).toBe("var(--chart-4)"); - }); - - it("keeps primary by default", async () => { - expect(await lineStroke()).toBe("var(--primary)"); - }); -}); diff --git a/src/components/custom-metrics/__tests__/custom-metric-chart-color.test.tsx b/src/components/custom-metrics/__tests__/custom-metric-chart-color.test.tsx new file mode 100644 index 000000000..023b19597 --- /dev/null +++ b/src/components/custom-metrics/__tests__/custom-metric-chart-color.test.tsx @@ -0,0 +1,81 @@ +import type { ReactNode } from "react"; +import { renderToStaticMarkup } from "react-dom/server"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +/** + * The line takes the series colour it is given (the GKI draws in a chart + * token beside the ketone series) and keeps primary when it is given none, + * so a custom metric's chart looks as it always has. + */ + +const lineProps = vi.hoisted(() => [] as Array>); + +vi.mock("recharts", () => { + const Pass = ({ children }: { children?: ReactNode }) => <>{children}; + const Leaf = () => null; + return { + ResponsiveContainer: Pass, + ComposedChart: Pass, + CartesianGrid: Leaf, + ReferenceArea: Leaf, + ReferenceLine: Leaf, + Tooltip: Leaf, + XAxis: Leaf, + YAxis: Leaf, + Line: (props: Record) => { + lineProps.push(props); + return null; + }, + }; +}); + +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; + +import { I18nProvider } from "@/lib/i18n/context"; + +import { CustomMetricChart } from "../custom-metric-chart"; +import type { CustomMetricEntryDto } from "../types"; + +const NOW = Date.now(); +const ENTRIES: CustomMetricEntryDto[] = [3, 2, 1].map((daysAgo, i) => ({ + id: `e${i}`, + customMetricId: "", + value: 2 + i, + unit: "", + measuredAt: new Date(NOW - daysAgo * 86_400_000).toISOString(), + note: null, + createdAt: new Date(NOW).toISOString(), +})); + +function lineStroke(color?: string): unknown { + renderToStaticMarkup( + + + + + , + ); + return lineProps.at(-1)?.stroke; +} + +beforeEach(() => { + lineProps.length = 0; +}); + +describe(" series colour", () => { + it("draws the line in the colour it is given", () => { + expect(lineStroke("var(--chart-4)")).toBe("var(--chart-4)"); + }); + + it("keeps primary by default", () => { + expect(lineStroke()).toBe("var(--primary)"); + }); +});