import { Rect } from "@noya-app/noya-geometry"; import { round } from "@noya-app/noya-utils"; import { describe, expect, test } from "bun:test"; import { getScaleTransform } from "../utils/scalingUtils"; // Ignore rounding errors in tests, for now const roundRect = (rect: Rect): Rect => ({ x: round(rect.x, 2), y: round(rect.y, 2), width: round(rect.width, 2), height: round(rect.height, 2), }); const square100 = { x: 0, y: 0, width: 100, height: 100 }; describe("not constrained", () => { test("resize rect se", () => { const transform = getScaleTransform(square100, { x: 5, y: 10 }, "se", { preserveAspectRatio: false, scalingOriginMode: "extent", ignoreSnapping: false, }); expect(roundRect(transform.applyTo(square100))).toEqual({ x: 0, y: 0, width: 105, height: 110, }); }); test("resize rect se, flip vertical", () => { const transform = getScaleTransform(square100, { x: 5, y: -200 }, "se", { preserveAspectRatio: false, scalingOriginMode: "extent", ignoreSnapping: false, }); expect(roundRect(transform.applyTo(square100))).toEqual({ x: 0, y: 0, width: 105, height: -100, }); }); test("resize rect se, flip horizontal", () => { const transform = getScaleTransform(square100, { x: -200, y: 10 }, "se", { preserveAspectRatio: false, scalingOriginMode: "extent", ignoreSnapping: false, }); expect(roundRect(transform.applyTo(square100))).toEqual({ x: 0, y: 0, width: -100, height: 110, }); }); }); describe("constrained", () => { test("resize rect se", () => { const transform = getScaleTransform(square100, { x: 5, y: 10 }, "se", { preserveAspectRatio: true, scalingOriginMode: "extent", ignoreSnapping: false, }); expect(roundRect(transform.applyTo(square100))).toEqual({ x: 0, y: 0, width: 110, height: 110, }); }); test("resize rect se, flip vertical", () => { const transform = getScaleTransform(square100, { x: 5, y: -200 }, "se", { preserveAspectRatio: true, scalingOriginMode: "extent", ignoreSnapping: false, }); expect(roundRect(transform.applyTo(square100))).toEqual({ x: 0, y: 0, width: 105, height: -105, }); }); test("resize rect se, flip horizontal", () => { const transform = getScaleTransform(square100, { x: -200, y: 10 }, "se", { preserveAspectRatio: true, scalingOriginMode: "extent", ignoreSnapping: false, }); expect(roundRect(transform.applyTo(square100))).toEqual({ x: 0, y: 0, width: -110, height: 110, }); }); });