|
| 1 | +/* |
| 2 | +Copyright 2026-present The maxGraph project Contributors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { afterEach, beforeAll, describe, expect, test } from '@jest/globals'; |
| 18 | +import { |
| 19 | + BaseGraph, |
| 20 | + Cell, |
| 21 | + CellState, |
| 22 | + type CellStateStyle, |
| 23 | + EdgeHandler, |
| 24 | + EdgeStyle, |
| 25 | + type EdgeStyleFunction, |
| 26 | + EdgeStyleRegistry, |
| 27 | + Geometry, |
| 28 | + Point, |
| 29 | + Rectangle, |
| 30 | + RectangleShape, |
| 31 | + registerDefaultEdgeStyles, |
| 32 | + unregisterAllEdgeStyles, |
| 33 | +} from '../../../src'; |
| 34 | + |
| 35 | +const createEdgeHandlerForStyle = ( |
| 36 | + edgeStyle: EdgeStyleFunction | undefined, |
| 37 | + pointCount: number, |
| 38 | + { skipDefaultRegistration = false } = {} |
| 39 | +): EdgeHandler => { |
| 40 | + const graph = new BaseGraph(); |
| 41 | + if (!skipDefaultRegistration) { |
| 42 | + registerDefaultEdgeStyles(); |
| 43 | + } |
| 44 | + |
| 45 | + const cell = new Cell(); |
| 46 | + cell.setEdge(true); |
| 47 | + cell.setVertex(false); |
| 48 | + cell.setGeometry(new Geometry()); |
| 49 | + |
| 50 | + const style: CellStateStyle = edgeStyle ? { edgeStyle } : {}; |
| 51 | + const cellState = new CellState(graph.view, cell, style); |
| 52 | + cellState.absolutePoints = Array.from( |
| 53 | + { length: pointCount }, |
| 54 | + (_, i) => new Point(i * 10, 0) |
| 55 | + ); |
| 56 | + cellState.shape = new RectangleShape(new Rectangle(), 'green', 'blue'); |
| 57 | + |
| 58 | + return new EdgeHandler(cellState); |
| 59 | +}; |
| 60 | + |
| 61 | +const createEdgeHandlerWithoutGeometry = (): EdgeHandler => { |
| 62 | + const graph = new BaseGraph(); |
| 63 | + const cell = new Cell(); |
| 64 | + cell.setEdge(true); |
| 65 | + cell.setVertex(false); |
| 66 | + // no geometry set |
| 67 | + |
| 68 | + const cellState = new CellState(graph.view, cell, { |
| 69 | + edgeStyle: EdgeStyle.EntityRelation, |
| 70 | + }); |
| 71 | + cellState.absolutePoints = [new Point(0, 0), new Point(10, 0)]; |
| 72 | + cellState.shape = new RectangleShape(new Rectangle(), 'green', 'blue'); |
| 73 | + |
| 74 | + return new EdgeHandler(cellState); |
| 75 | +}; |
| 76 | + |
| 77 | +describe('isHandleVisible', () => { |
| 78 | + beforeAll(() => { |
| 79 | + unregisterAllEdgeStyles(); |
| 80 | + }); |
| 81 | + afterEach(() => { |
| 82 | + unregisterAllEdgeStyles(); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('EntityRelation edge style', () => { |
| 86 | + test('first handle is visible', () => { |
| 87 | + const handler = createEdgeHandlerForStyle(EdgeStyle.EntityRelation, 5); |
| 88 | + expect(handler.isHandleVisible(0)).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + test('last handle is visible', () => { |
| 92 | + const handler = createEdgeHandlerForStyle(EdgeStyle.EntityRelation, 5); |
| 93 | + expect(handler.isHandleVisible(4)).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + test('intermediate handle is not visible', () => { |
| 97 | + const handler = createEdgeHandlerForStyle(EdgeStyle.EntityRelation, 5); |
| 98 | + expect(handler.isHandleVisible(2)).toBe(false); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + test('other edge style - intermediate handle is visible', () => { |
| 103 | + const handler = createEdgeHandlerForStyle(EdgeStyle.OrthConnector, 5); |
| 104 | + expect(handler.isHandleVisible(2)).toBe(true); |
| 105 | + }); |
| 106 | + |
| 107 | + test('no geometry - all handles are visible', () => { |
| 108 | + const handler = createEdgeHandlerWithoutGeometry(); |
| 109 | + expect(handler.isHandleVisible(0)).toBe(true); |
| 110 | + expect(handler.isHandleVisible(1)).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + test('no edge style - intermediate handle is visible', () => { |
| 114 | + const handler = createEdgeHandlerForStyle(undefined, 5); |
| 115 | + expect(handler.isHandleVisible(2)).toBe(true); |
| 116 | + }); |
| 117 | + |
| 118 | + test('EntityRelation registered without allowIntermediateHandles metadata - intermediate handle is visible', () => { |
| 119 | + EdgeStyleRegistry.add('entityRelationEdgeStyle', EdgeStyle.EntityRelation, {}); |
| 120 | + |
| 121 | + const handler = createEdgeHandlerForStyle(EdgeStyle.EntityRelation, 5, { |
| 122 | + skipDefaultRegistration: true, |
| 123 | + }); |
| 124 | + expect(handler.isHandleVisible(2)).toBe(true); |
| 125 | + }); |
| 126 | +}); |
0 commit comments