diff --git a/src/components/day/__tests__/day-events-dose.test.tsx b/src/components/day/__tests__/day-events-dose.test.tsx new file mode 100644 index 000000000..9f27d1f21 --- /dev/null +++ b/src/components/day/__tests__/day-events-dose.test.tsx @@ -0,0 +1,63 @@ +/** + * An intake under "What happened" reads as its name and the dose taken on + * one line, "Ramipril 5mg", as the same medication does under Running that + * day. The dose on a muted line of its own under the name read as + * "Ramipril / 5mg". + */ +import { describe, expect, it } from "vitest"; +import { renderToStaticMarkup } from "react-dom/server"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; + +import { I18nProvider } from "@/lib/i18n/context"; +import type { DayEvent } from "@/lib/day/contract"; + +import { DayEvents } from "../day-sections"; + +function event( + kind: DayEvent["kind"], + title: string, + meta: string | null, +): DayEvent { + return { + at: null, + kind, + section: "medications", + id: `${kind}-1`, + title, + meta, + note: null, + docs: [], + href: null, + }; +} + +/** The text of each line under the event's time and icon: its

s. */ +function lines(events: DayEvent[]): string[] { + const html = renderToStaticMarkup( + + + + + , + ); + return [...html.matchAll(/]*>(.*?)<\/p>/g)].map((m) => m[1]); +} + +describe("DayEvents dose", () => { + it("names an intake with its dose on one line", () => { + expect(lines([event("intake", "Ramipril", "5mg")])).toEqual([ + "Ramipril 5mg", + ]); + }); + + it("names an intake without a dose by its name alone", () => { + expect(lines([event("intake", "Ramipril", null)])).toEqual(["Ramipril"]); + }); + + it("keeps a dose change's new dose on its own line", () => { + expect(lines([event("doseChange", "Ramipril", "10 mg")])).toEqual([ + "Ramipril", + "10 mg", + ]); + }); +}); diff --git a/src/components/day/day-sections.tsx b/src/components/day/day-sections.tsx index 6f9993464..d41c63600 100644 --- a/src/components/day/day-sections.tsx +++ b/src/components/day/day-sections.tsx @@ -589,6 +589,13 @@ const EVENT_ICON: Record = { lifeEvent: Flag, }; +/** + * Event kinds whose meta is the dose taken. It reads as part of the name, + * "Ramipril 5 mg", as under Running that day, not as a second line of its + * own under the name. + */ +const DOSE_META_EVENT_KINDS: ReadonlySet = new Set(["intake"]); + export function DayEvents({ events }: { events: readonly DayEvent[] }) { const { t } = useTranslations(); const fmt = useFormatters(); @@ -599,9 +606,14 @@ export function DayEvents({ events }: { events: readonly DayEvent[] }) {