i18n(labs): say why a biomarker's unit cannot change once it has readings

This commit is contained in:
Marc-André Bombeck committed 2026-10-03 18:22:55 +02:00
1 parent dd7636e147
commit 3a35dbcf37
8 files changed
+122

No files matched your search

@@ -0,0 +1,80 @@
import { describe, it, expect, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { I18nProvider } from "@/lib/i18n/context";
import { ApiError } from "@/lib/api/api-fetch";
import { localizedApiError } from "@/lib/api/localized-error";
import en from "../../../../messages/en.json";
/**
* The marker editor says, before a save, that a marker's unit stays as it is
* once readings exist, and the refusal the server sends for such a change
* reads as that sentence rather than the generic save error.
*/
vi.mock("@/lib/api/api-fetch", async (importOriginal) => ({
...(await importOriginal<typeof import("@/lib/api/api-fetch")>()),
apiPost: vi.fn(),
apiPut: vi.fn(),
}));
import { BiomarkerForm } from "../biomarker-form";
import type { BiomarkerDto } from "../types";
const MARKER: BiomarkerDto = {
id: "bm-1",
name: "Glucose",
unit: "mg/dL",
lowerBound: 70,
upperBound: 99,
panel: null,
hasContext: false,
context: null,
hidden: false,
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
};
function render(node: React.ReactNode) {
return renderToStaticMarkup(
<I18nProvider initialLocale="en">{node}</I18nProvider>,
);
}
describe("<BiomarkerForm> unit lock", () => {
const hint = en.labs.biomarker.form.unitLockedHint;
it("tells the editor of an existing marker that the unit is locked by readings", () => {
const html = render(<BiomarkerForm existing={MARKER} />);
expect(hint.length).toBeGreaterThan(0);
expect(html).toContain(hint.replace(/'/g, "&#x27;"));
expect(html).toMatch(/aria-describedby="[^"]*-unit-hint"/);
});
it("says nothing of a lock while a new marker is defined", () => {
const html = render(<BiomarkerForm />);
expect(html).not.toContain(hint.replace(/'/g, "&#x27;"));
});
it("reads the server's refusal as its own sentence, not the save error", () => {
const err = new ApiError("locked", 422, {
errorCode: "biomarkers.unit.locked",
readingCount: 2,
});
const t = (key: string) => {
const value = key
.split(".")
.reduce<unknown>(
(node, part) =>
node && typeof node === "object"
? (node as Record<string, unknown>)[part]
: undefined,
en,
);
return typeof value === "string" ? value : key;
};
expect(localizedApiError(err, t, "labs.biomarker.form.saveError")).toBe(
en.apiErrors.biomarkers.unit.locked,
);
});
});