|
| 1 | +import { MultiDirectedGraph } from 'graphology'; |
| 2 | +import { getFullPathHighlightedEntities, getIsHighlightedItemInGraph } from './utils'; |
| 3 | + |
| 4 | +// Linear directed graph with a split inbound on node-c: |
| 5 | +// node-a → node-b → node-c ← node-d |
| 6 | +const buildTestGraph = () => { |
| 7 | + const graph = new MultiDirectedGraph(); |
| 8 | + graph.addNode('node-a'); |
| 9 | + graph.addNode('node-b'); |
| 10 | + graph.addNode('node-c'); |
| 11 | + graph.addNode('node-d'); |
| 12 | + graph.addDirectedEdgeWithKey('edge-ab', 'node-a', 'node-b'); |
| 13 | + graph.addDirectedEdgeWithKey('edge-bc', 'node-b', 'node-c'); |
| 14 | + graph.addDirectedEdgeWithKey('edge-dc', 'node-d', 'node-c'); |
| 15 | + return graph; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('SigmaChart Utils', () => { |
| 19 | + let graph: MultiDirectedGraph; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + graph = buildTestGraph(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('getIsHighlightedItemInGraph', () => { |
| 26 | + it('returns true when the highlighted item is a node in the graph', () => { |
| 27 | + expect(getIsHighlightedItemInGraph(graph, 'node-a')).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns true when the highlighted item is an edge in the graph', () => { |
| 31 | + expect(getIsHighlightedItemInGraph(graph, 'edge-ab')).toBe(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns undefined when highlightedItem is null', () => { |
| 35 | + expect(getIsHighlightedItemInGraph(graph, null)).toBeUndefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns false when the highlighted item is not in the graph', () => { |
| 39 | + expect(getIsHighlightedItemInGraph(graph, 'node-unknown')).toBe(false); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('getFullPathHighlightedEntities', () => { |
| 44 | + describe('when a node is selected', () => { |
| 45 | + it('returns the full inbound and outbound path when selecting a middle node', () => { |
| 46 | + const { highlightedNodeIds, highlightedEdgeIds } = getFullPathHighlightedEntities(graph, 'node-b'); |
| 47 | + |
| 48 | + expect([...highlightedNodeIds]).toEqual(expect.arrayContaining(['node-a', 'node-b', 'node-c'])); |
| 49 | + expect([...highlightedEdgeIds]).toEqual(expect.arrayContaining(['edge-ab', 'edge-bc'])); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns only the outbound path when selecting the start node', () => { |
| 53 | + const { highlightedNodeIds, highlightedEdgeIds } = getFullPathHighlightedEntities(graph, 'node-a'); |
| 54 | + |
| 55 | + expect([...highlightedNodeIds]).toEqual(expect.arrayContaining(['node-a', 'node-b', 'node-c'])); |
| 56 | + expect([...highlightedEdgeIds]).toEqual(expect.arrayContaining(['edge-ab', 'edge-bc'])); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns all inbound paths when selecting the end node with multiple inbound edges', () => { |
| 60 | + const { highlightedNodeIds, highlightedEdgeIds } = getFullPathHighlightedEntities(graph, 'node-c'); |
| 61 | + |
| 62 | + expect([...highlightedNodeIds]).toEqual( |
| 63 | + expect.arrayContaining(['node-a', 'node-b', 'node-c', 'node-d']) |
| 64 | + ); |
| 65 | + expect([...highlightedEdgeIds]).toEqual(expect.arrayContaining(['edge-ab', 'edge-bc', 'edge-dc'])); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns only the selected node when it has no connections', () => { |
| 69 | + const isolatedGraph = new MultiDirectedGraph(); |
| 70 | + isolatedGraph.addNode('node-isolated'); |
| 71 | + const { highlightedNodeIds, highlightedEdgeIds } = getFullPathHighlightedEntities( |
| 72 | + isolatedGraph, |
| 73 | + 'node-isolated' |
| 74 | + ); |
| 75 | + |
| 76 | + expect([...highlightedNodeIds]).toEqual(['node-isolated']); |
| 77 | + expect(highlightedEdgeIds.size).toBe(0); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('when an edge is selected', () => { |
| 82 | + it('returns the edge and both of its endpoint nodes', () => { |
| 83 | + const { highlightedNodeIds, highlightedEdgeIds } = getFullPathHighlightedEntities(graph, 'edge-ab'); |
| 84 | + |
| 85 | + expect([...highlightedNodeIds]).toEqual(expect.arrayContaining(['node-a', 'node-b'])); |
| 86 | + expect([...highlightedEdgeIds]).toEqual(['edge-ab']); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns empty sets when highlightedItem is null', () => { |
| 91 | + const { highlightedNodeIds, highlightedEdgeIds } = getFullPathHighlightedEntities(graph, null); |
| 92 | + |
| 93 | + expect(highlightedNodeIds.size).toBe(0); |
| 94 | + expect(highlightedEdgeIds.size).toBe(0); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments