Skip to content

Commit 5a16d13

Browse files
committed
feat: 🎸 added getNodeDeepness to TreeState
1 parent 364e4aa commit 5a16d13

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export interface TreeState {
146146
getTree: (state: State) => Node[];
147147
createFromTree: (tree: Node[]) => State;
148148
getNumberOfVisibleDescendants: (state: State, index: number) => number;
149+
getNodeDeepness: (state: State, index: number) => number;
149150
}
150151

151152
export interface TreeStateModifiers {

src/state/TreeState.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ export default class TreeState {
4141
return getNodeFromPath(rowPath, state.tree);
4242
};
4343

44+
/**
45+
* Given a state, finds a node deepness at a certain row index.
46+
* @param {State} state - The current state
47+
* @param {number} index - The visible row index
48+
* @return {number} The node deepness
49+
*/
50+
static getNodeDeepness = (state, index) => {
51+
validateState(state);
52+
53+
const rowPath = state.flattenedTree[index];
54+
55+
if (!rowPath) {
56+
throw Error(
57+
`Tried to get node at row "${index}" but got nothing, the tree are ${state.flattenedTree.length} visible rows`,
58+
);
59+
}
60+
61+
return rowPath.length - 1;
62+
};
63+
4464
/**
4565
* Given a state and an index, finds the number of visible descendants
4666
* @param {State} state - The current state

src/state/__tests__/TreeState.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ describe('TreeState', () => {
6262
});
6363
});
6464

65+
describe('getNodeDeepness', () => {
66+
test('should get the correct deepness for existing rowId', () => {
67+
expect(TreeState.getNodeDeepness(TreeState.createFromTree(Nodes), 0)).toBe(0);
68+
expect(TreeState.getNodeDeepness(TreeState.createFromTree(Nodes), 1)).toBe(1);
69+
expect(TreeState.getNodeDeepness(TreeState.createFromTree(Nodes), 2)).toBe(2);
70+
expect(TreeState.getNodeDeepness(TreeState.createFromTree(Nodes), 3)).toBe(2);
71+
expect(TreeState.getNodeDeepness(TreeState.createFromTree(Nodes), 6)).toBe(0);
72+
});
73+
74+
test('should fail with a custom error when supplied rowId does not exist', () => {
75+
expect(() => TreeState.getNodeDeepness(TreeState.createFromTree(Nodes), 40)).toThrowErrorMatchingSnapshot();
76+
});
77+
78+
test('should fail for when invalid state is supplied', () => {
79+
expect(() => TreeState.getNodeDeepness('state', 0)).toThrowError('Expected a State instance but got string');
80+
expect(() => TreeState.getNodeDeepness(1225, 0)).toThrowError('Expected a State instance but got number');
81+
expect(() => TreeState.getNodeDeepness([], 0)).toThrowError('Expected a State instance but got object');
82+
expect(() => TreeState.getNodeDeepness({}, 0)).toThrowError('Expected a State instance but got object');
83+
expect(() => TreeState.getNodeDeepness(true, 0)).toThrowError('Expected a State instance but got boolean');
84+
expect(() => TreeState.getNodeDeepness(() => {}, 0)).toThrowError('Expected a State instance but got function');
85+
});
86+
});
87+
6588
describe('getNumberOfVisibleDescendants', () => {
6689
test('should fail for when invalid state is supplied', () => {
6790
expect(() => TreeState.getNumberOfVisibleDescendants('state', 0)).toThrowError(

src/state/__tests__/__snapshots__/TreeState.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,5 @@ Object {
9696
},
9797
}
9898
`;
99+
100+
exports[`TreeState getNodeDeepness should fail with a custom error when supplied rowId does not exist 1`] = `"Tried to get node at row \\"40\\" but got nothing, the tree are 7 visible rows"`;

0 commit comments

Comments
 (0)