import { describe, expect, test } from "bun:test"; import { getElementSnapDelta, getSnapLines, getValuesOnAxis, } from "../snapUtils"; describe("getElementSnapDelta", () => { test("Right of moving to left of stationary", () => { const movingRect = { x: 105, y: 110, width: 100, height: 100 }; const stationaryRect = { x: 209, y: 100, width: 100, height: 100 }; expect( getElementSnapDelta({ movingValues: getValuesOnAxis(movingRect, "x"), stationaryValues: getValuesOnAxis(stationaryRect, "x"), proximity: 5, }) ).toEqual(4); }); test("Left of moving to left of stationary", () => { const movingRect = { x: 109, y: 100, width: 100, height: 100 }; const stationaryRect = { x: 105, y: 110, width: 100, height: 100 }; expect( getElementSnapDelta({ movingValues: getValuesOnAxis(movingRect, "x"), stationaryValues: getValuesOnAxis(stationaryRect, "x"), proximity: 5, }) ).toEqual(-4); }); test("Left of moving to right of stationary", () => { const movingRect = { x: 209, y: 100, width: 100, height: 100 }; const stationaryRect = { x: 105, y: 110, width: 100, height: 100 }; expect( getElementSnapDelta({ movingValues: getValuesOnAxis(movingRect, "x"), stationaryValues: getValuesOnAxis(stationaryRect, "x"), proximity: 5, }) ).toEqual(-4); }); test("Right of moving to right of stationary", () => { const movingRect = { x: 100, y: 110, width: 50, height: 100 }; const stationaryRect = { x: 54, y: 100, width: 100, height: 100 }; expect( getElementSnapDelta({ movingValues: getValuesOnAxis(movingRect, "x"), stationaryValues: getValuesOnAxis(stationaryRect, "x"), proximity: 5, }) ).toEqual(4); }); }); describe("getSnapLines", () => { test("Snap line should cover all shapes when moving shape snaps to multiple shapes on X axis", () => { const movingRect = { x: 204, y: 900, width: 100, height: 100 }; const stationaryRects = [ { x: 204, y: 100, width: 100, height: 100 }, { x: 4, y: 300, width: 200, height: 100 }, ]; const snapLines = getSnapLines({ movingRect, stationaryRects, axis: "x", }); expect(snapLines).toEqual([ { x1: 204, x2: 204, y1: 100, y2: 1000 }, { x1: 254, x2: 254, y1: 100, y2: 1000 }, { x1: 304, x2: 304, y1: 100, y2: 1000 }, ]); }); });