test(graph-utils): add tests

This commit is contained in:
moklick
2020-07-21 13:49:37 +02:00
parent c1734af953
commit 69c41b9b91
2 changed files with 138 additions and 1 deletions
+8 -1
View File
@@ -33,9 +33,16 @@ const getEdgeId = ({ source, target }: Connection): ElementId => `reactflow__edg
export const addEdge = (edgeParams: Edge | Connection, elements: Elements): Elements => {
if (!edgeParams.source || !edgeParams.target) {
throw new Error('Can not create edge. An edge needs a source and a target');
throw new Error("Can't create edge. An edge needs a source and a target.");
}
// make sure that there is node with the target and one with the source id
[edgeParams.source, edgeParams.target].forEach((id) => {
if (!elements.find((e) => isNode(e) && e.id === id)) {
throw new Error(`Can't create edge. Node with id=${id} does not exist.`);
}
});
if (isEdge(edgeParams)) {
return elements.concat({ ...edgeParams });
}