aboutsummaryrefslogtreecommitdiffstats
path: root/packages/excalidraw/element/sizeHelpers.test.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/excalidraw/element/sizeHelpers.test.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/excalidraw/element/sizeHelpers.test.ts')
-rw-r--r--packages/excalidraw/element/sizeHelpers.test.ts67
1 files changed, 67 insertions, 0 deletions
diff --git a/packages/excalidraw/element/sizeHelpers.test.ts b/packages/excalidraw/element/sizeHelpers.test.ts
new file mode 100644
index 0000000..8e63dd9
--- /dev/null
+++ b/packages/excalidraw/element/sizeHelpers.test.ts
@@ -0,0 +1,67 @@
+import { vi } from "vitest";
+import { getPerfectElementSize } from "./sizeHelpers";
+import * as constants from "../constants";
+
+const EPSILON_DIGITS = 3;
+// Needed so that we can mock the value of constants which is done in
+// below tests. In Jest this wasn't needed as global override was possible
+// but vite doesn't allow that hence we need to mock
+vi.mock(
+ "../constants.ts",
+ //@ts-ignore
+ async (importOriginal) => {
+ const module: any = await importOriginal();
+ return { ...module };
+ },
+);
+describe("getPerfectElementSize", () => {
+ it("should return height:0 if `elementType` is line and locked angle is 0", () => {
+ const { height, width } = getPerfectElementSize("line", 149, 10);
+ expect(width).toBeCloseTo(149, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(0, EPSILON_DIGITS);
+ });
+
+ it("should return width:0 if `elementType` is line and locked angle is 90 deg (Math.PI/2)", () => {
+ const { height, width } = getPerfectElementSize("line", 10, 140);
+ expect(width).toBeCloseTo(0, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(140, EPSILON_DIGITS);
+ });
+
+ it("should return height:0 if `elementType` is arrow and locked angle is 0", () => {
+ const { height, width } = getPerfectElementSize("arrow", 200, 20);
+ expect(width).toBeCloseTo(200, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(0, EPSILON_DIGITS);
+ });
+ it("should return width:0 if `elementType` is arrow and locked angle is 90 deg (Math.PI/2)", () => {
+ const { height, width } = getPerfectElementSize("arrow", 10, 100);
+ expect(width).toBeCloseTo(0, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(100, EPSILON_DIGITS);
+ });
+
+ it("should return adjust height to be width * tan(locked angle)", () => {
+ const { height, width } = getPerfectElementSize("arrow", 120, 185);
+ expect(width).toBeCloseTo(120, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(207.846, EPSILON_DIGITS);
+ });
+
+ it("should return height equals to width if locked angle is 45 deg", () => {
+ const { height, width } = getPerfectElementSize("arrow", 135, 145);
+ expect(width).toBeCloseTo(135, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(135, EPSILON_DIGITS);
+ });
+
+ it("should return height:0 and width:0 when width and height are 0", () => {
+ const { height, width } = getPerfectElementSize("arrow", 0, 0);
+ expect(width).toBeCloseTo(0, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(0, EPSILON_DIGITS);
+ });
+
+ describe("should respond to SHIFT_LOCKING_ANGLE constant", () => {
+ it("should have only 2 locking angles per section if SHIFT_LOCKING_ANGLE = 45 deg (Math.PI/4)", () => {
+ (constants as any).SHIFT_LOCKING_ANGLE = Math.PI / 4;
+ const { height, width } = getPerfectElementSize("arrow", 120, 185);
+ expect(width).toBeCloseTo(120, EPSILON_DIGITS);
+ expect(height).toBeCloseTo(120, EPSILON_DIGITS);
+ });
+ });
+});