Files
HealthLog/src/lib/insights/__tests__/chart-y-domain.test.ts
T
Marc-André Bombeck bcbb05c770 fix(charts,medications): hold the overdue dose on the card and floor non-negative axes
A dose past its slot time but still inside its catch-up window now stays
on the medication card as overdue and takeable instead of yielding to
the next occurrence — the next-due field can therefore carry a past open
slot, flagged with nextDueOverdue. Y-axis padding no longer dips below
zero for series that cannot be negative, so a steps chart stops
suggesting minus nine hundred steps.
2026-06-10 22:41:28 +02:00

58 lines
2.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { computePaddedYDomain } from "@/lib/insights/chart-y-domain";
describe("computePaddedYDomain", () => {
it("returns undefined for an empty series", () => {
expect(computePaddedYDomain([])).toBeUndefined();
});
it("ignores non-finite values", () => {
expect(computePaddedYDomain([NaN, Infinity, -Infinity])).toBeUndefined();
});
it("clamps the lower bound to 0 for a non-negative series", () => {
const domain = computePaddedYDomain([200, 450, 900]);
expect(domain).toBeDefined();
const [min, max] = domain!;
expect(min).toBeGreaterThanOrEqual(0);
expect(max).toBeGreaterThan(900);
});
it("clamps to 0 when bottom padding would dip below 0 (steps-like series)", () => {
// span 8800 → 8% bottom padding = 704 > min 200 → unclamped would be -504
const domain = computePaddedYDomain([200, 9000]);
expect(domain).toBeDefined();
expect(domain![0]).toBe(0);
});
it("keeps downward padding for a series with genuine negative values", () => {
const domain = computePaddedYDomain([-5, 3, 10]);
expect(domain).toBeDefined();
const [min, max] = domain!;
expect(min).toBeLessThan(-5);
expect(max).toBeGreaterThan(10);
});
it("pads a positive series without touching 0 when padding stays above it", () => {
// span 2 → bottom padding max(0.16, 0.5) = 0.5 → 79.5, no clamp needed
const domain = computePaddedYDomain([80, 82]);
expect(domain).toBeDefined();
expect(domain![0]).toBeCloseTo(79.5);
});
it("clamps a flat zero series to a [0, x] domain", () => {
const domain = computePaddedYDomain([0, 0, 0]);
expect(domain).toBeDefined();
const [min, max] = domain!;
expect(min).toBe(0);
expect(max).toBeGreaterThan(0);
});
it("keeps the symmetric flat-series padding for negative flat series", () => {
const domain = computePaddedYDomain([-10, -10]);
expect(domain).toBeDefined();
expect(domain![0]).toBeLessThan(-10);
});
});