Fix dependency graph with new nodes

Signed-off-by: Oliver Sand <oliver.sand@sda-se.com>
This commit is contained in:
Oliver Sand
2021-08-25 12:09:19 +02:00
parent 8576195e38
commit 6d76bca859
3 changed files with 28 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core-components': patch
---
Handle changes to nodes passed into `<DependencyGraph>` correctly.
@@ -49,6 +49,28 @@ describe('<DependencyGraph />', () => {
expect(queryAllByTestId(LABEL_TEST_ID)).toHaveLength(0);
});
it('update render if already referenced nodes are added later', async () => {
const { getByText, queryAllByTestId, findAllByTestId, rerender } = render(
<DependencyGraph nodes={nodes.slice(0, 2)} edges={edges} />,
);
let renderedNodes = await findAllByTestId(NODE_TEST_ID);
expect(renderedNodes).toHaveLength(2);
expect(getByText(nodes[0].id)).toBeInTheDocument();
expect(getByText(nodes[1].id)).toBeInTheDocument();
expect(queryAllByTestId(EDGE_TEST_ID)).toHaveLength(2);
expect(queryAllByTestId(LABEL_TEST_ID)).toHaveLength(0);
rerender(<DependencyGraph nodes={nodes} edges={edges} />);
renderedNodes = await findAllByTestId(NODE_TEST_ID);
expect(renderedNodes).toHaveLength(3);
expect(getByText(nodes[0].id)).toBeInTheDocument();
expect(getByText(nodes[1].id)).toBeInTheDocument();
expect(queryAllByTestId(EDGE_TEST_ID)).toHaveLength(2);
expect(queryAllByTestId(LABEL_TEST_ID)).toHaveLength(0);
});
it('renders edge labels if present', async () => {
const labeledEdges = [
{ ...edges[0], label: 'first' },
@@ -171,7 +171,7 @@ export function DependencyGraph({
.nodes()
.find(nodeId => node.id === nodeId);
if (existingNode) {
if (existingNode && graph.current.node(existingNode)) {
const { width, height, x, y } = graph.current.node(existingNode);
graph.current.setNode(existingNode, { ...node, width, height, x, y });
} else {