Skip to content

Commit

Permalink
feat: Add reconnect method
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Nov 25, 2023
1 parent cee5dde commit 089ac67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/ua-utils/src/omnigraph/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class OmniGraphBuilder<TNodeConfig, TEdgeConfig> {
const existingEdge = this.getEdgeAt({ from: fromNode.point, to: toNode.point })
const newEdge = r(fromNode, toNode, existingEdge)

if (existingEdge != null) this.removeEdgeAt(existingEdge.vector)
if (newEdge != null) this.addEdges(newEdge)
else if (existingEdge != null) this.removeEdgeAt(existingEdge.vector)
})
),
this
Expand Down
39 changes: 39 additions & 0 deletions packages/ua-utils/test/omnigraph/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,45 @@ describe('omnigraph/builder', () => {
)
})
})

describe('reconnect', () => {
it('should return self', () => {
const builder = new OmniGraphBuilder()
const reconnector = jest.fn()

expect(builder.reconnect(reconnector)).toBe(builder)
})

it('should not call reconnector when there are no nodes', () => {
const builder = new OmniGraphBuilder()
const reconnector = jest.fn()

builder.reconnect(reconnector)

expect(builder.nodes).toEqual([])
expect(builder.edges).toEqual([])
expect(reconnector).not.toHaveBeenCalled()
})

it('should call reconnector for every node combination', () => {
fc.assert(
fc.property(nodesArbitrary, (nodes) => {
const builder = new OmniGraphBuilder()
const reconnector = jest.fn()

builder.addNodes(...nodes)
builder.reconnect(reconnector)
expect(reconnector).toHaveBeenCalledTimes(builder.nodes.length * builder.nodes.length)

for (const nodeA of builder.nodes) {
for (const nodeB of builder.nodes) {
expect(reconnector).toHaveBeenCalledWith(nodeA, nodeB, undefined)
}
}
})
)
})
})
})

describe('accessor methods', () => {
Expand Down

0 comments on commit 089ac67

Please sign in to comment.