Files
HealthLog/src/__tests__/helpers/unit-display-mock.ts
T
Marc-André Bombeck 0028290117 fix(cycle): read basal temperature and the unit-bearing chart titles in the reader's units
The basal body temperature field, its chart and its tooltip were fixed
to degrees Celsius whatever the reader had chosen: the placeholder said
°C, the field accepted only 30-45, the chart plotted Celsius and the
tooltip printed "{temp} °C". Two chart titles carried a fixed unit as
well ("Wrist temperature (°C)", "Walking speed (km/h)") above an axis
already in the reader's unit.

The field now reads and writes in the reader's unit and stores Celsius;
a reading the person did not touch is saved back unchanged rather than
through a round trip of rounding. The chart plots, pads and labels in
the reader's unit, the two titles drop the unit the axis already
names, and the cycle contrast's fixed kg, °C and mg/dL labels, which
the transform had already replaced, leave the bundles.

Refs #1067
2026-10-01 13:23:17 +02:00

43 lines
1.6 KiB
TypeScript

/**
* A real `UnitDisplay` for one fixed preference, for component tests that
* render without a QueryClient (`useUnitDisplay` reads the account through
* react-query). Built on the production transforms, so a test asserts the
* same numbers and symbols the hook would give.
*
* vi.mock("@/hooks/use-unit-display", async () => {
* const { unitDisplayFor } = await import("@/__tests__/helpers/unit-display-mock");
* return { useUnitDisplay: () => unitDisplayFor("metric") };
* });
*/
import type { UnitDisplay } from "@/hooks/use-unit-display";
import type { GlucoseUnit } from "@/lib/glucose";
import {
applyDisplayTransform,
applyDisplayTransformDelta,
getDisplayTransform,
hasDisplayTransform,
invertDisplayTransform,
type UnitPreference,
} from "@/lib/measurements/display-transform";
export function unitDisplayFor(
preference: UnitPreference,
glucoseUnit: GlucoseUnit = "mg/dL",
): UnitDisplay {
// The hook resolves mass, length and temperature through the
// metric/imperial branch alone; glucose is converted where it renders.
const forType = (type: string) => getDisplayTransform(type, preference);
return {
preference,
glucoseUnit,
transformFor: forType,
toDisplay: (type, value) => applyDisplayTransform(value, forType(type)),
fromDisplay: (type, value) => invertDisplayTransform(value, forType(type)),
toDisplayDelta: (type, value) =>
applyDisplayTransformDelta(value, forType(type)),
unitFor: (type) => forType(type).displayUnit,
decimalsFor: (type) => forType(type).decimals,
isTransformed: (type) => hasDisplayTransform(type),
};
}