test(web): cover timeline drag math guards
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { computeDragDates, pixelsToDays, shiftDate } from "./dragMath.js";
|
||||
|
||||
describe("dragMath", () => {
|
||||
it("returns zero days for invalid cell widths instead of leaking invalid drag deltas", () => {
|
||||
expect(pixelsToDays(40, 0)).toBe(0);
|
||||
expect(pixelsToDays(40, Number.NaN)).toBe(0);
|
||||
expect(pixelsToDays(40, Number.POSITIVE_INFINITY)).toBe(0);
|
||||
});
|
||||
|
||||
it("shifts dates without mutating the original date", () => {
|
||||
const original = new Date("2026-04-10T00:00:00.000Z");
|
||||
|
||||
const shifted = shiftDate(original, 3);
|
||||
|
||||
expect(shifted.toISOString()).toBe("2026-04-13T00:00:00.000Z");
|
||||
expect(original.toISOString()).toBe("2026-04-10T00:00:00.000Z");
|
||||
});
|
||||
|
||||
it("clamps resize-start drags so the start does not cross the end", () => {
|
||||
const { start, end } = computeDragDates(
|
||||
"resize-start",
|
||||
new Date("2026-04-10T00:00:00.000Z"),
|
||||
new Date("2026-04-12T00:00:00.000Z"),
|
||||
5,
|
||||
);
|
||||
|
||||
expect(start.toISOString()).toBe("2026-04-12T00:00:00.000Z");
|
||||
expect(end.toISOString()).toBe("2026-04-12T00:00:00.000Z");
|
||||
});
|
||||
|
||||
it("clamps resize-end drags so the end does not cross the start", () => {
|
||||
const { start, end } = computeDragDates(
|
||||
"resize-end",
|
||||
new Date("2026-04-10T00:00:00.000Z"),
|
||||
new Date("2026-04-12T00:00:00.000Z"),
|
||||
-5,
|
||||
);
|
||||
|
||||
expect(start.toISOString()).toBe("2026-04-10T00:00:00.000Z");
|
||||
expect(end.toISOString()).toBe("2026-04-10T00:00:00.000Z");
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,9 @@
|
||||
|
||||
/** Convert a pixel delta to a number of whole days based on cell width. */
|
||||
export function pixelsToDays(deltaX: number, cellWidth: number): number {
|
||||
if (!Number.isFinite(cellWidth) || cellWidth <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return Math.round(deltaX / cellWidth);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user