diff --git a/packages/math/src/segment.ts b/packages/math/src/segment.ts index ae282bfee5..7523ef3f2f 100644 --- a/packages/math/src/segment.ts +++ b/packages/math/src/segment.ts @@ -40,7 +40,7 @@ export const isLineSegment = ( Array.isArray(segment) && segment.length === 2 && isPoint(segment[0]) && - isPoint(segment[0]); + isPoint(segment[1]); /** * Return the coordinates resulting from rotating the given line about an origin by an angle in radians diff --git a/packages/math/tests/segment.test.ts b/packages/math/tests/segment.test.ts index 6b29bba591..a7091521df 100644 --- a/packages/math/tests/segment.test.ts +++ b/packages/math/tests/segment.test.ts @@ -1,5 +1,9 @@ import { pointFrom } from "../src/point"; -import { lineSegment, lineSegmentIntersectionPoints } from "../src/segment"; +import { + lineSegment, + lineSegmentIntersectionPoints, + isLineSegment, +} from "../src/segment"; describe("line-segment intersections", () => { it("should correctly detect intersection", () => { @@ -19,3 +23,23 @@ describe("line-segment intersections", () => { ).toEqual(null); }); }); +describe("isLineSegment validation", () => { + it("should return true for a valid segment", () => { + expect( + isLineSegment([ + [0, 0], + [1, 1], + ]), + ).toBe(true); + }); + + it("should return false if second element is not a point", () => { + const invalidSegment = [[0, 0], "not-a-point"] as any; + + expect(isLineSegment(invalidSegment)).toBe(false); + }); + + it("should return false for wrong length", () => { + expect(isLineSegment([[0, 0]])).toBe(false); + }); +});