From 1f71b345eea25988268aff402674c15d6a0b6da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= Date: Wed, 1 Apr 2026 09:24:38 +0200 Subject: [PATCH] test(web): cover allocation visual state helpers --- .../timeline/allocationVisualState.test.ts | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 apps/web/src/components/timeline/allocationVisualState.test.ts diff --git a/apps/web/src/components/timeline/allocationVisualState.test.ts b/apps/web/src/components/timeline/allocationVisualState.test.ts new file mode 100644 index 0000000..a1d30fc --- /dev/null +++ b/apps/web/src/components/timeline/allocationVisualState.test.ts @@ -0,0 +1,100 @@ +import { describe, expect, it } from "vitest"; +import { + applyPointerOffsetPreviewRect, + applyVisualOverrides, + getDragPointerOffset, +} from "./allocationVisualState.js"; + +describe("allocationVisualState", () => { + it("keeps only the residual pointer offset after full-day snapping", () => { + expect(getDragPointerOffset(53, 2, 20)).toBe(13); + expect(getDragPointerOffset(-53, -2, 20)).toBe(-13); + }); + + it("clamps resize-start preview rectangles to the configured minimum width", () => { + expect( + applyPointerOffsetPreviewRect({ + left: 100, + width: 40, + mode: "resize-start", + pointerOffsetX: 35, + minWidth: 20, + }), + ).toEqual({ + left: 120, + width: 20, + }); + }); + + it("keeps move previews stable when there is no residual pointer offset", () => { + expect( + applyPointerOffsetPreviewRect({ + left: 100, + width: 40, + mode: "move", + pointerOffsetX: 0, + minWidth: 20, + }), + ).toEqual({ + left: 100, + width: 40, + }); + }); + + it("returns the original entries array when no override matches", () => { + const entries = [ + { + id: "alloc_1", + startDate: "2026-04-10", + endDate: "2026-04-12", + label: "original", + }, + ] as const; + + const result = applyVisualOverrides( + entries, + new Map([ + [ + "alloc_other", + { + startDate: new Date("2026-04-20T00:00:00.000Z"), + endDate: new Date("2026-04-21T00:00:00.000Z"), + }, + ], + ]), + ); + + expect(result).toBe(entries); + }); + + it("clones override dates without mutating unaffected entries", () => { + const entries = [ + { + id: "alloc_1", + startDate: "2026-04-10", + endDate: "2026-04-12", + label: "changed", + }, + { + id: "alloc_2", + startDate: "2026-04-15", + endDate: "2026-04-16", + label: "unchanged", + }, + ]; + const override = { + startDate: new Date("2026-04-20T00:00:00.000Z"), + endDate: new Date("2026-04-22T00:00:00.000Z"), + }; + + const result = applyVisualOverrides(entries, new Map([["alloc_1", override]])); + + expect(result).not.toBe(entries); + expect(result[0]).not.toBe(entries[0]); + expect(result[1]).toBe(entries[1]); + expect(result[0]?.startDate).toEqual(new Date("2026-04-20T00:00:00.000Z")); + expect(result[0]?.endDate).toEqual(new Date("2026-04-22T00:00:00.000Z")); + expect(result[0]?.startDate).not.toBe(override.startDate); + expect(result[0]?.endDate).not.toBe(override.endDate); + }); +});