Skip to content

Commit 37ddffd

Browse files
committed
test(tree): add tests for getTreePaths function
1 parent d827285 commit 37ddffd

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

src/tree.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { buildTree } from './tree';
3+
import { buildTree, getTreePaths } from './tree';
4+
5+
type TestTreeNode = {
6+
id: number;
7+
children?: TestTreeNode[];
8+
};
49

510
describe('tree utils', () => {
611
it('builds a nested tree from flat nodes', () => {
@@ -18,4 +23,48 @@ describe('tree utils', () => {
1823
expect(tree[0].children?.[0].id).toBe(2);
1924
expect(tree[0].children?.[0].children?.[0].id).toBe(3);
2025
});
26+
27+
it('gets the path to a nested node', () => {
28+
const nodes: TestTreeNode[] = [
29+
{
30+
id: 1,
31+
children: [
32+
{
33+
id: 2,
34+
children: [{ id: 3 }]
35+
}
36+
]
37+
}
38+
];
39+
40+
const paths = getTreePaths('id', 3, nodes);
41+
42+
expect(paths).toEqual([1, 2, 3]);
43+
});
44+
45+
it('returns an empty array when the target node does not exist', () => {
46+
const nodes: TestTreeNode[] = [
47+
{
48+
id: 1,
49+
children: [{ id: 2 }]
50+
}
51+
];
52+
53+
const paths = getTreePaths('id', 9, nodes);
54+
55+
expect(paths).toEqual([]);
56+
});
57+
58+
it('supports falsy target values like 0', () => {
59+
const nodes: TestTreeNode[] = [
60+
{
61+
id: 0,
62+
children: [{ id: 1 }]
63+
}
64+
];
65+
66+
const paths = getTreePaths('id', 0, nodes);
67+
68+
expect(paths).toEqual([0]);
69+
});
2170
});

src/tree.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,22 @@ export const buildTree = <K extends string, P extends string, T extends BaseFlat
4141
return tree;
4242
};
4343

44-
type BaseNode<K extends string, V extends string | number> = {
44+
type BaseNode<K extends string, V extends string | number = string | number> = {
4545
[key in K]: V;
4646
};
4747

48-
type BaseTreeNode<K extends string, V extends string | number, T extends BaseNode<K, V>> = T & {
49-
children?: Array<BaseTreeNode<K, V, T>>;
48+
type BaseTreeNode<K extends string, T extends BaseNode<K>> = T & {
49+
children?: Array<BaseTreeNode<K, T>>;
5050
};
5151

52-
export const getTreePaths = <K extends string, V extends string | number, T extends BaseNode<K, V>>(
52+
export const getTreePaths = <K extends string, T extends BaseNode<K>>(
5353
nodeKey: K,
54-
target: V,
55-
nodes: BaseTreeNode<K, V, T>[]
54+
target: T[K],
55+
nodes: BaseTreeNode<K, T>[]
5656
) => {
57-
const paths: V[] = [];
57+
const paths: T[K][] = [];
5858

59-
if (!target) {
60-
return paths;
61-
}
62-
63-
function dfs(node: BaseTreeNode<K, V, T>, path: V[]): V[] | null {
59+
function dfs(node: BaseTreeNode<K, T>, path: T[K][]): T[K][] | null {
6460
const currentPath = [...path, node[nodeKey]];
6561

6662
// if find the target value, return the path

0 commit comments

Comments
 (0)