Skip to content

Commit

Permalink
export + tested function that generate root node for diff tree
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalabbad committed Aug 29, 2024
1 parent be85f65 commit a04c9ce
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 28 deletions.
69 changes: 41 additions & 28 deletions frontend/app/src/screens/diff/diff-tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,7 @@ export default function DiffTree({ nodes, loading, emptyMessage, ...props }: Dif
return [...acc, newNode];
}, [] as TreeProps["data"]);

const rootNodes = formattedNodes.filter(({ parent }) => parent === TREE_ROOT_ID);

const kindToUUIDsMap = rootNodes.reduce((acc, node) => {
const currentNodeKind = node.metadata?.kind?.toString();
if (!currentNodeKind) return acc;

const currentUUIDs = acc[currentNodeKind];
const nodeId = node.id.toString();
node.parent = currentNodeKind;
return {
...acc,
[currentNodeKind]: currentUUIDs ? [...currentUUIDs, nodeId] : [nodeId],
};
}, {} as Record<string, Array<string>>);

const parents = Object.entries(kindToUUIDsMap).map(([kind, uuids]) => {
return {
id: kind,
name: kind,
parent: TREE_ROOT_ID,
children: uuids,
isBranch: true,
metadata: {
kind,
},
};
});
setTreeData(addItemsToTree(EMPTY_TREE, [...parents, ...formattedNodes]));
setTreeData(addItemsToTree(EMPTY_TREE, generateRootCategoryNodeForDiffTree(formattedNodes)));
}, [nodes]);

if (treeData.length <= 1) return <NoDataFound message={emptyMessage ?? "No data to display."} />;
Expand Down Expand Up @@ -148,3 +121,43 @@ const DiffTreeItem = ({ element }: TreeItemProps) => {
</a>
);
};

export const generateRootCategoryNodeForDiffTree = (
diffTreeNodes: TreeProps["data"]
): TreeProps["data"] => {
return diffTreeNodes.reduce((acc, node) => {
const nodeKind = node.metadata?.kind as string | undefined;
if (node.parent !== TREE_ROOT_ID || !nodeKind) return [...acc, node];

const nodeUpdated = {
...node,
parent: nodeKind,
};

const existingRootCategoryNode = acc.find(({ id }) => id === nodeKind);

if (existingRootCategoryNode) {
const rootCategoryNodeUpdated = {
...existingRootCategoryNode,
children: [...existingRootCategoryNode.children, node.id],
};

return acc
.map((item) => (item.id === existingRootCategoryNode.id ? rootCategoryNodeUpdated : item))
.concat(nodeUpdated);
} else {
const newRootCategoryNode = {
id: nodeKind,
name: nodeKind,
parent: TREE_ROOT_ID,
children: [node.id],
isBranch: true,
metadata: {
kind: nodeKind,
},
};

return [...acc, newRootCategoryNode, nodeUpdated];
}
}, [] as TreeProps["data"]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { describe, expect, it } from "vitest";
import { generateRootCategoryNodeForDiffTree } from "../../../../src/screens/diff/diff-tree";
import { TreeProps } from "../../../../src/components/ui/tree";
import { TREE_ROOT_ID } from "../../../../src/screens/ipam/constants";

describe("Generate root category nodes for DiffTree", () => {
it("should return an empty array when no nodes are provided", () => {
// GIVEN
const diffTreeNodes: TreeProps["data"] = [];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([]);
});

it("should return an empty array when all provided nodes are not root nodes", () => {
// GIVEN
const diffTreeNodes = [{ id: "1", parent: "not-root", metadata: { kind: "A" } }];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([{ id: "1", parent: "not-root", metadata: { kind: "A" } }]);
});

it("should generate root category nodes for DiffTree with single node", () => {
// GIVEN
const diffTreeNodes = [{ id: "1", parent: TREE_ROOT_ID, metadata: { kind: "A" } }];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([
{
id: "A",
name: "A",
parent: TREE_ROOT_ID,
children: ["1"],
isBranch: true,
metadata: { kind: "A" },
},
{ id: "1", parent: "A", metadata: { kind: "A" } },
]);
});

it("should generate root category nodes for DiffTree with multiple nodes of the same kind", () => {
// GIVEN
const diffTreeNodes = [
{ id: "1", parent: TREE_ROOT_ID, metadata: { kind: "A" } },
{ id: "2", parent: TREE_ROOT_ID, metadata: { kind: "A" } },
{ id: "3", parent: TREE_ROOT_ID, metadata: { kind: "A" } },
];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([
{
id: "A",
name: "A",
parent: TREE_ROOT_ID,
children: ["1", "2", "3"],
isBranch: true,
metadata: { kind: "A" },
},
{ id: "1", parent: "A", metadata: { kind: "A" } },
{ id: "2", parent: "A", metadata: { kind: "A" } },
{ id: "3", parent: "A", metadata: { kind: "A" } },
]);
});

it("should generate root category nodes for DiffTree with multiple nodes of different kinds", () => {
// GIVEN
const diffTreeNodes = [
{ id: "1", parent: TREE_ROOT_ID, metadata: { kind: "A" } },
{ id: "2", parent: TREE_ROOT_ID, metadata: { kind: "B" } },
{ id: "3", parent: TREE_ROOT_ID, metadata: { kind: "C" } },
];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([
{
id: "A",
name: "A",
parent: TREE_ROOT_ID,
children: ["1"],
isBranch: true,
metadata: { kind: "A" },
},
{ id: "1", parent: "A", metadata: { kind: "A" } },
{
id: "B",
name: "B",
parent: TREE_ROOT_ID,
children: ["2"],
isBranch: true,
metadata: { kind: "B" },
},
{ id: "2", parent: "B", metadata: { kind: "B" } },
{
id: "C",
name: "C",
parent: TREE_ROOT_ID,
children: ["3"],
isBranch: true,
metadata: { kind: "C" },
},
{ id: "3", parent: "C", metadata: { kind: "C" } },
]);
});

it("should not generate root category nodes for nodes that have no kind", () => {
// GIVEN
const diffTreeNodes = [
{ id: "1", parent: TREE_ROOT_ID },
{ id: "2", parent: TREE_ROOT_ID, metadata: { kind: "A" } },
];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([
{ id: "1", parent: TREE_ROOT_ID },
{
id: "A",
name: "A",
parent: TREE_ROOT_ID,
children: ["2"],
isBranch: true,
metadata: { kind: "A" },
},
{ id: "2", parent: "A", metadata: { kind: "A" } },
]);
});

it("should not generate root category nodes for nodes that have null or undefined kind", () => {
// GIVEN
const diffTreeNodes = [
{ id: "1", parent: TREE_ROOT_ID, metadata: { kind: null } },
{ id: "2", parent: TREE_ROOT_ID, metadata: { kind: undefined } },
{ id: "3", parent: TREE_ROOT_ID, metadata: { kind: "A" } },
];

// WHEN
const result = generateRootCategoryNodeForDiffTree(diffTreeNodes);

// THEN
expect(result).toEqual([
{ id: "1", parent: TREE_ROOT_ID, metadata: { kind: null } },
{ id: "2", parent: TREE_ROOT_ID, metadata: { kind: undefined } },
{
id: "A",
name: "A",
parent: TREE_ROOT_ID,
children: ["3"],
isBranch: true,
metadata: { kind: "A" },
},
{ id: "3", parent: "A", metadata: { kind: "A" } },
]);
});
});

0 comments on commit a04c9ce

Please sign in to comment.