Files
HealthLog/src/components/day/__tests__/day-view.test.tsx
T
Marc-André Bombeck bf83d414e2 feat: open any past day as a layer over the page
A date is now a door to its day (#613). `?day=YYYY-MM-DD` opens the day
over whichever page is open: docked beside it from 1280 px, the way the
Coach's conversations panel sits, a sheet from the right below that, and a
bottom sheet on a phone that opens at a little over half the height and
grows to full height from its handle. On /coach it is always a sheet, so
there is never more than one docked panel. Back and Escape close it, the
previous and next day replace the history entry instead of adding one,
Alt with an arrow steps a day, focus moves to the day's heading on open and
returns to whatever opened it on close, and a polite region names the day.
A malformed or future date is dropped from the URL.

The day shows the value it was opened from with its comparison, what ran
through the day, the readings against the person's own usual range on a
short number line (eight curated, the rest behind "All values"), and what
happened in order of the clock, each row linking to its record. Loading,
failure, an empty day and areas a grant does not cover each read as such.
The footer offers the timeline when that module is on and capture for the
day, with the capture forms starting on that date.

Doors into it: a click on a daily point of a metric chart (a tap shows the
value and the tooltip offers the day on touch screens), the dates in the
chart's data table, a quiet row of dots under the axis for days with
entries and a ring for notable ones, the lab chart and lab readings, the
measurement, mood and intake lists, symptom and illness day headings,
episode onset and recovery, document dates, workout dates, the workout
detail, the mood and adherence heatmaps, and the days a Coach answer's
get_day calls read, shown as chips. The next planned visit carries what
happened since the last one, each date opening its day, with a doctor
report for that window.

Not here, and frozen by a guard: the dashboard's today area, mini charts
in tiles, the cycle calendar, model-written text, future dates, the
clinician share link and the doctor-report PDF.
2026-10-08 14:20:01 +02:00

286 lines
8.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import enMessages from "../../../../messages/en.json";
import type { DayResponse } from "@/lib/day/contract";
/**
* The day's content in every shell: header, sections in the mockup's order,
* the footer's two ways onward, and the honest states (loading, failed,
* empty, partly shared). Rendered through SSR against a `GET /api/day/{date}`
* answer shaped by the contract, as the server will send it.
*/
const DAY: DayResponse = {
date: "2026-01-03",
tz: "Europe/Berlin",
counts: { values: 3, entries: 2 },
running: [
{
kind: "illness",
section: "illness",
id: "ep1",
title: "Common cold",
sub: null,
since: "2025-12-31",
until: null,
dayIndex: 4,
dayCount: null,
href: "/illness/ep1",
},
{
kind: "medicationCourse",
section: "medications",
id: "c1",
title: "Vitamin D 1,000 IU",
sub: "Winter course",
since: "2025-11-01",
until: "2026-03-31",
dayIndex: 64,
dayCount: 151,
href: "/medications/m1?tab=verlauf",
},
],
values: [
{
type: "BLOOD_PRESSURE_DIA",
value: 88,
unit: "mmHg",
at: "2026-01-03T06:12:00.000Z",
source: "MANUAL",
band: { lo: 78, hi: 86, n: 28 },
},
{
type: "BLOOD_PRESSURE_SYS",
value: 138,
unit: "mmHg",
at: "2026-01-03T06:12:00.000Z",
source: "MANUAL",
band: { lo: 121, hi: 134, n: 28 },
},
{
type: "WEIGHT",
value: 82.6,
unit: "kg",
at: "2026-01-03T06:20:00.000Z",
source: "WITHINGS",
band: null,
},
],
events: [
{
at: "2026-01-03T10:20:00.000Z",
kind: "visit",
section: "visits",
id: "v1",
title: "GP practice",
meta: "Acute appointment",
note: null,
docs: [{ id: "d1", name: "sick-note.pdf" }],
href: "/checkups?visit=v1",
},
{
at: "2026-01-03T20:10:00.000Z",
kind: "mood",
section: "mood",
id: "m1",
title: "Mood: rather low",
meta: "tired",
note: "Fever gone by the evening.",
docs: [],
href: "/mood",
},
],
notable: [
{
kind: "extremeHigh",
type: "BLOOD_PRESSURE_SYS",
params: { since: "2025-03-01", value: 138 },
},
],
sections: {},
};
let dayQuery: { data?: DayResponse; isError?: boolean } = { data: DAY };
let timelineOn = true;
let pathname = "/insights/blood-pressure";
let canWrite = true;
vi.mock("../use-day", () => ({
useDay: () => ({ ...dayQuery, refetch: () => undefined }),
}));
vi.mock("next/navigation", () => ({ usePathname: () => pathname }));
vi.mock("@/hooks/use-auth", () => ({
useAuth: () => ({ isAuthenticated: true, user: { modules: {} } }),
}));
vi.mock("@/hooks/use-module-enabled", () => ({
useModuleEnabled: (key: string) => (key === "timeline" ? timelineOn : true),
}));
vi.mock("@/hooks/use-ai-capability", () => ({
useAiCapability: () => ({ available: false, reason: "check_failed" }),
}));
vi.mock("@/hooks/use-unit-display", () => ({
useUnitDisplay: () => ({
toDisplay: (_t: string, v: number) => v,
unitFor: (t: string) => (t === "WEIGHT" ? "kg" : "mmHg"),
decimalsFor: () => 1,
isTransformed: (t: string) => t === "WEIGHT",
}),
}));
vi.mock("@/hooks/use-record-capabilities", () => ({
useRecordCapabilities: () => ({
inSharedRecord: false,
canWriteDomain: () => canWrite,
}),
}));
vi.mock("@/components/layout/capture-picker", () => ({
CAPTURE_KIND_ORDER: ["measurement", "mood"],
CapturePicker: () => null,
visibleCaptureKinds: (caps: { canWriteDomain: () => boolean }) =>
caps.canWriteDomain() ? ["measurement"] : [],
}));
async function render(
props: Partial<{
date: string;
today: string;
focus: unknown;
}> = {},
) {
const { I18nProvider } = await import("@/lib/i18n/context");
const { DayView } = await import("../day-view");
return renderToStaticMarkup(
<I18nProvider initialLocale="en" initialMessages={enMessages}>
<DayView
date={props.date ?? "2026-01-03"}
today={props.today ?? "2026-10-08"}
focus={(props.focus as never) ?? null}
shell="docked"
onClose={() => undefined}
onStep={() => undefined}
Title={(p) => <h2 {...p} />}
titleId="t"
/>
</I18nProvider>,
);
}
beforeEach(() => {
vi.resetModules();
dayQuery = { data: DAY };
timelineOn = true;
pathname = "/insights/blood-pressure";
canWrite = true;
});
describe("<DayView>", () => {
it("names the day and counts what it holds", async () => {
const html = await render();
expect(html).toContain("Saturday, January 3, 2026");
expect(html).toContain("3 values · 2 entries");
});
it("lists what ran through the day with its running count", async () => {
const html = await render();
expect(html).toContain("Running that day");
expect(html).toContain("Common cold");
expect(html).toContain("Day 4 · since");
expect(html).toContain("Winter course · Day 64 of 151");
expect(html).toContain('href="/illness/ep1"');
});
it("folds the blood pressure into one tile on its number line", async () => {
const html = await render();
expect(html).toContain('data-type="BLOOD_PRESSURE"');
expect(html).toContain("138/88");
expect(html).toContain('data-slot="day-number-line"');
// A value without a band has no line, never an invented one.
const weightTile = html.split('data-type="WEIGHT"')[1] ?? "";
expect(weightTile.split("</li>")[0]).not.toContain("day-number-line");
});
it("lists what happened, each entry linking to its source", async () => {
const html = await render();
expect(html).toContain("What happened");
expect(html).toContain('href="/checkups?visit=v1"');
expect(html).toContain('href="/documents?doc=d1"');
// The person's own note, in the content colour.
expect(html).toContain("Fever gone by the evening.");
});
it("puts the value the person came from on top and marks its tile", async () => {
const html = await render({
focus: {
date: "2026-01-03",
types: ["BLOOD_PRESSURE_SYS", "BLOOD_PRESSURE_DIA"],
label: "Blood pressure",
value: "138/88",
unit: "mmHg",
},
});
expect(html).toContain('data-slot="day-focus"');
expect(html).toContain("Your 30 days before");
expect(html).toContain("121–134/78–86");
expect(html).toContain('data-selected="true"');
expect(html).toContain("Highest daily value since March 2025");
});
it("offers the timeline only with its module on, and not on the timeline", async () => {
expect(await render()).toContain('data-slot="day-in-timeline"');
timelineOn = false;
expect(await render()).not.toContain('data-slot="day-in-timeline"');
timelineOn = true;
pathname = "/timeline";
expect(await render()).not.toContain('data-slot="day-in-timeline"');
});
it("offers capture only to someone who may write", async () => {
expect(await render()).toContain('data-slot="day-capture"');
canWrite = false;
expect(await render()).not.toContain('data-slot="day-capture"');
});
it("never steps past today", async () => {
const html = await render({ date: "2026-10-08", today: "2026-10-08" });
expect(html).toMatch(/data-slot="day-next"[^>]*disabled=""/);
});
it("keeps a failed read apart from an empty day", async () => {
dayQuery = { isError: true };
const failed = await render();
expect(failed).toContain('data-slot="day-error"');
expect(failed).not.toContain('data-slot="day-empty"');
dayQuery = {
data: {
...DAY,
counts: { values: 0, entries: 0 },
running: [],
values: [],
events: [],
notable: [],
},
};
const empty = await render();
expect(empty).toContain("There are no entries for this day.");
expect(empty).not.toContain('data-slot="day-error"');
});
it("paints the frame while loading", async () => {
dayQuery = {};
const html = await render();
expect(html).toContain('data-slot="day-loading"');
expect(html).toContain("Saturday, January 3, 2026");
});
it("says once that some areas are not shared", async () => {
dayQuery = {
data: {
...DAY,
sections: { labs: { available: false, reason: "not_shared" } },
},
};
expect(await render()).toContain("Some areas are not shared with you.");
});
});