From 5f75784ce2a5b9f3a2d7fb96324d247b17cd36ff Mon Sep 17 00:00:00 2001 From: Yuri Mikhin Date: Wed, 25 Oct 2023 11:39:15 +0300 Subject: [PATCH] fixup! Do not consider invisible nodes in areNodesIntersecting. --- .../specs/are-nodes-intersecting.spec.ts | 46 ++++++++++++++++++- .../traverse-and-check-intersections.spec.ts | 12 +++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/api/services/figma/intersections/specs/are-nodes-intersecting.spec.ts b/src/api/services/figma/intersections/specs/are-nodes-intersecting.spec.ts index 6f1669a..22a48cd 100644 --- a/src/api/services/figma/intersections/specs/are-nodes-intersecting.spec.ts +++ b/src/api/services/figma/intersections/specs/are-nodes-intersecting.spec.ts @@ -7,10 +7,12 @@ describe('areNodesIntersecting', () => { const node = { absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, id: '1', + visible: true, } as unknown as SceneNode; const selectedNode = { id: '2', + visible: true, } as unknown as SceneNode; expect(areNodesIntersecting(node, selectedNode)).toBe(false); @@ -18,13 +20,15 @@ describe('areNodesIntersecting', () => { test('should return false if node and selectedNode are identical', () => { const node = { - absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, + absoluteBoundingBox: { height: 50, width: 50, x: 20, y: 20 }, id: '1', + visible: true, } as unknown as SceneNode; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 20, y: 20 }, id: '1', + visible: true, } as unknown as SceneNode; expect(areNodesIntersecting(node, selectedNode)).toBe(false); @@ -33,11 +37,13 @@ describe('areNodesIntersecting', () => { test('should return false if node has no bounding box', () => { const node = { id: '1', + visible: true, } as unknown as SceneNode; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 20, y: 20 }, id: '2', + visible: true, } as unknown as SceneNode; expect(areNodesIntersecting(node, selectedNode)).toBe(false); @@ -47,11 +53,13 @@ describe('areNodesIntersecting', () => { const selectedNode = { absoluteBoundingBox: { height: 40, width: 40, x: 25, y: 25 }, id: '1', + visible: true, } as unknown as SceneNode; const node = { absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(areNodesIntersecting(node, selectedNode)).toBe(true); @@ -61,11 +69,13 @@ describe('areNodesIntersecting', () => { const node = { absoluteBoundingBox: { height: 60, width: 60, x: 50, y: 50 }, id: '1', + visible: true, } as unknown as SceneNode; const selectedNode = { absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(areNodesIntersecting(node, selectedNode)).toBe(false); @@ -75,13 +85,47 @@ describe('areNodesIntersecting', () => { const node = { absoluteBoundingBox: { height: 10, width: 10, x: 120, y: 120 }, id: '1', + visible: true, } as unknown as SceneNode; const selectedNode = { absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, id: '2', + visible: true, + } as unknown as SceneNode; + + expect(areNodesIntersecting(node, selectedNode)).toBe(false); + }); + + test('should return false if node is not visible', () => { + const node = { + absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, + id: '1', + visible: false, + } as unknown as SceneNode; + + const selectedNode = { + absoluteBoundingBox: { height: 50, width: 50, x: 20, y: 20 }, + id: '2', + visible: true, } as unknown as SceneNode; expect(areNodesIntersecting(node, selectedNode)).toBe(false); }); + + test('should return true if node is fully contained within visible selectedNode', () => { + const selectedNode = { + absoluteBoundingBox: { height: 40, width: 40, x: 25, y: 25 }, + id: '1', + visible: true, + } as unknown as SceneNode; + + const node = { + absoluteBoundingBox: { height: 100, width: 100, x: 10, y: 10 }, + id: '2', + visible: true, + } as unknown as SceneNode; + + expect(areNodesIntersecting(node, selectedNode)).toBe(true); + }); }); diff --git a/src/api/services/figma/intersections/specs/traverse-and-check-intersections.spec.ts b/src/api/services/figma/intersections/specs/traverse-and-check-intersections.spec.ts index 69c3b28..9788712 100644 --- a/src/api/services/figma/intersections/specs/traverse-and-check-intersections.spec.ts +++ b/src/api/services/figma/intersections/specs/traverse-and-check-intersections.spec.ts @@ -8,12 +8,14 @@ describe('traverseAndCheckIntersections', () => { { absoluteBoundingBox: { height: 50, width: 50, x: 200, y: 200 }, id: '1', + visible: true, }, ] as unknown as SceneNode[]; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(traverseAndCheckIntersections(nodes, selectedNode)).toEqual([]); @@ -24,12 +26,14 @@ describe('traverseAndCheckIntersections', () => { { absoluteBoundingBox: { height: 100, width: 100, x: 0, y: 0 }, id: '1', + visible: true, }, ] as unknown as SceneNode[]; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(traverseAndCheckIntersections(nodes, selectedNode)).toEqual(nodes); @@ -42,14 +46,17 @@ describe('traverseAndCheckIntersections', () => { { absoluteBoundingBox: { height: 90, width: 90, x: 0, y: 0 }, id: '3', + visible: true, }, ], id: '1', + visible: true, } as unknown as FrameNode; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(traverseAndCheckIntersections([frameNode], selectedNode)).toEqual([ @@ -66,15 +73,18 @@ describe('traverseAndCheckIntersections', () => { { absoluteBoundingBox: { height: 5, width: 5, x: 90, y: 90 }, id: '3', + visible: true, }, ], id: '1', + visible: true, }, ] as unknown as SceneNode[]; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(traverseAndCheckIntersections(nodes, selectedNode)).toEqual([ @@ -87,12 +97,14 @@ describe('traverseAndCheckIntersections', () => { { absoluteBoundingBox: { height: 50, width: 50, x: 10, y: 10 }, id: '2', + visible: true, }, ] as unknown as SceneNode[]; const selectedNode = { absoluteBoundingBox: { height: 50, width: 50, x: 10, y: 10 }, id: '2', + visible: true, } as unknown as SceneNode; expect(traverseAndCheckIntersections(nodes, selectedNode)).toEqual([]);