import { renderToStaticMarkup } from "react-dom/server"; import { describe, expect, it } from "vitest"; import { I18nProvider } from "@/lib/i18n/context"; import type { OcrReviewRow } from "../ocr-review-types"; import { OcrRowEditor } from "../ocr-row-editor"; const baseRow: OcrReviewRow = { key: "LDL-0", analyte: "LDL", value: 2.4, valueText: null, unit: "mmol/L", referenceLow: 0, referenceHigh: 3, referenceText: null, takenAt: null, confidence: { analyte: 1, value: 1, unit: 1, range: 1 }, biomarkerMatch: "existing", duplicateOf: null, confirmed: true, }; function render( errors: Parameters[0]["errors"], locale: "en" | "de" = "en", row: OcrReviewRow = baseRow, ): string { return renderToStaticMarkup( {}} errors={errors} /> , ); } describe(" required-field marking", () => { it("shows no error state before a save was attempted", () => { const html = render(undefined); expect(html).not.toContain('aria-invalid="true"'); expect(html).not.toContain('role="alert"'); }); it("marks the missing date, states why, and links the message to the field", () => { const html = render({ date: true }); const invalid = html.match(/]*aria-invalid="true"[^>]*>/g) ?? []; expect(invalid).toHaveLength(1); const describedBy = invalid[0].match(/aria-describedby="([^"]+)"/)?.[1]; expect(describedBy).toBeTruthy(); expect(html).toContain(`id="${describedBy}"`); expect(html).toContain("Add the sample date."); expect(html).toContain('role="alert"'); // The bordered wrapper is what the eye sees. expect(html).toContain("border-destructive"); }); it("marks only the fields that are wrong", () => { const html = render({ unit: true }); expect(html.match(/aria-invalid="true"/g)).toHaveLength(1); expect(html).toContain("Add a unit."); expect(html).not.toContain("Add the sample date."); }); it("marks a blank qualitative result", () => { const html = render({ valueText: true }, "en", { ...baseRow, value: null, valueText: "", unit: null, }); expect(html).toContain("Add a result."); expect(html.match(/aria-invalid="true"/g)).toHaveLength(1); }); it("carries a visible required marker on the fields a row needs", () => { const html = render(undefined); // Value, unit and date on a numeric row. expect(html.match(/text-destructive"[^>]*> \* { expect(render({ date: true }, "de")).toContain("Bitte ein Datum angeben."); }); });