|
| 1 | +import { Point } from '@patternfly/react-topology'; |
| 2 | + |
| 3 | +import { LayoutType } from '../../components/Visualization/Canvas/canvas.models'; |
| 4 | +import { OrthogonalBendpointsEdge } from './OrthogonalBendpointsEdge'; |
| 5 | + |
| 6 | +const node = (pos: { x: number; y: number }, size: { width: number; height: number }) => ({ |
| 7 | + getPosition: () => pos, |
| 8 | + getDimensions: () => size, |
| 9 | +}); |
| 10 | + |
| 11 | +const wireEdge = (edge: OrthogonalBendpointsEdge, layout: string, source: unknown, target: unknown) => { |
| 12 | + jest.spyOn(edge, 'getSource').mockReturnValue(source as never); |
| 13 | + jest.spyOn(edge, 'getTarget').mockReturnValue(target as never); |
| 14 | + jest.spyOn(edge, 'getGraph').mockReturnValue({ getLayout: () => layout } as never); |
| 15 | +}; |
| 16 | + |
| 17 | +describe('OrthogonalBendpointsEdge', () => { |
| 18 | + let edge: OrthogonalBendpointsEdge; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + edge = new OrthogonalBendpointsEdge(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + jest.restoreAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('returns no bendpoints for a self-loop', () => { |
| 29 | + const self = {}; |
| 30 | + wireEdge(edge, LayoutType.DagreHorizontal, self, self); |
| 31 | + expect(edge.getBendpoints()).toEqual([]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns no bendpoints when source or target is missing', () => { |
| 35 | + wireEdge(edge, LayoutType.DagreHorizontal, undefined, node({ x: 0, y: 0 }, { width: 10, height: 10 })); |
| 36 | + expect(edge.getBendpoints()).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('produces a horizontal step (same Y as source) and a vertical step at midX in horizontal layout', () => { |
| 40 | + const source = node({ x: 0, y: 0 }, { width: 100, height: 50 }); |
| 41 | + const target = node({ x: 300, y: 200 }, { width: 100, height: 50 }); |
| 42 | + wireEdge(edge, LayoutType.DagreHorizontal, source, target); |
| 43 | + |
| 44 | + // Centers: source = (50, 25), target = (350, 225). midX = 200. |
| 45 | + expect(edge.getBendpoints()).toEqual([new Point(200, 25), new Point(200, 225)]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('produces a vertical step (same X as source) and a horizontal step at midY in vertical layout', () => { |
| 49 | + const source = node({ x: 0, y: 0 }, { width: 100, height: 50 }); |
| 50 | + const target = node({ x: 300, y: 200 }, { width: 100, height: 50 }); |
| 51 | + wireEdge(edge, LayoutType.DagreVertical, source, target); |
| 52 | + |
| 53 | + // Centers: source = (50, 25), target = (350, 225). midY = 125. |
| 54 | + expect(edge.getBendpoints()).toEqual([new Point(50, 125), new Point(350, 125)]); |
| 55 | + }); |
| 56 | +}); |
0 commit comments