leistungsbilanz-ts/tests/room-area.test.ts

59 lines
2.3 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { RoomModal } from "../src/frontend/components/room-modal.js";
import {
formatRoomArea,
parseRoomAreaInput,
} from "../src/frontend/utils/room-area.js";
import {
createRoomSchema,
updateRoomSchema,
} from "../src/shared/validation/project-structure.schemas.js";
describe("room area input", () => {
it("parses optional German decimals without rounding", () => {
assert.deepEqual(parseRoomAreaInput(""), { ok: true, value: null });
assert.deepEqual(parseRoomAreaInput(" 23,456 "), { ok: true, value: 23.456 });
assert.deepEqual(parseRoomAreaInput(",5"), { ok: true, value: 0.5 });
assert.deepEqual(parseRoomAreaInput("0"), { ok: true, value: 0 });
for (const value of ["-1", "23.45", "abc", "1,2,3"]) {
assert.equal(parseRoomAreaInput(value).ok, false, value);
}
assert.equal(formatRoomArea(23.456), "23,46");
});
it("validates finite non-negative API values and remains backward compatible", () => {
const base = {
expectedRevision: 1,
roomNumber: "101",
roomName: "Büro",
};
assert.equal(createRoomSchema.safeParse({ ...base, areaM2: 23.456 }).success, true);
assert.equal(createRoomSchema.safeParse({ ...base, areaM2: null }).success, true);
assert.equal(createRoomSchema.safeParse(base).success, true);
assert.equal(updateRoomSchema.safeParse({ ...base, floorId: null }).success, true);
assert.equal(createRoomSchema.safeParse({ ...base, areaM2: -1 }).success, false);
assert.equal(createRoomSchema.safeParse({ ...base, areaM2: Number.NaN }).success, false);
});
it("renders the optional area with two decimals in the shared room dialog", () => {
const markup = renderToStaticMarkup(
createElement(RoomModal, {
floors: [],
initialValues: {
roomNumber: "101",
roomName: "Büro",
areaM2: 23.4,
},
isSaving: false,
onClose: () => undefined,
onSave: async () => undefined,
})
);
assert.match(markup, /Fläche \[m²\]/);
assert.match(markup, /value="23,40"/);
assert.match(markup, /Netto-Raumfläche/);
});
});