Skip to content

Commit d578c06

Browse files
committed
feat: 🎸 added getNumberOfVisibleDescendants to TreeState
1 parent 98b98c6 commit d578c06

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export interface TreeState {
144144
getNodeAt: (state: State, index: number) => Node;
145145
getTree: (state: State) => Node[];
146146
createFromTree: (tree: Node[]) => State;
147+
getNumberOfVisibleDescendants: (state: State, index: number) => number;
147148
}
148149

149150
export const selectors: Selectors;

src/state/TreeState.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {getFlattenedTreePaths} from '../selectors/getFlattenedTree';
22
import {getNodeFromPath} from '../selectors/nodes';
33

4-
class State {
4+
export class State {
55
flattenedTree = null;
66
tree = null;
77

@@ -41,6 +41,29 @@ export default class TreeState {
4141
return getNodeFromPath(rowPath, state.tree);
4242
};
4343

44+
/**
45+
* Given a state and an index, finds the number of visible descendants
46+
* @param {State} state - The current state
47+
* @param {number} index - The visible row index
48+
* @return {number} The number of visible descendants
49+
*/
50+
static getNumberOfVisibleDescendants = (state, index) => {
51+
const {id} = TreeState.getNodeAt(state, index);
52+
53+
const {flattenedTree} = state;
54+
let i;
55+
56+
for (i = index; i < flattenedTree.length; i++) {
57+
const path = flattenedTree[i];
58+
59+
if (!path.some(p => p === id)) {
60+
break;
61+
}
62+
}
63+
64+
return Math.max(i - 1 - index, 0);
65+
};
66+
4467
/**
4568
* Given a state, gets the tree
4669
* @param {State} state - The current state

src/state/__tests__/TreeState.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,47 @@ describe('TreeState', () => {
6161
expect(() => TreeState.getTree(() => {})).toThrowError('Expected a State instance but got function');
6262
});
6363
});
64+
65+
describe('getNumberOfVisibleDescendants', () => {
66+
test('should fail for when invalid state is supplied', () => {
67+
expect(() => TreeState.getNumberOfVisibleDescendants('state', 0)).toThrowError(
68+
'Expected a State instance but got string',
69+
);
70+
expect(() => TreeState.getNumberOfVisibleDescendants(1225, 0)).toThrowError(
71+
'Expected a State instance but got number',
72+
);
73+
expect(() => TreeState.getNumberOfVisibleDescendants([], 0)).toThrowError(
74+
'Expected a State instance but got object',
75+
);
76+
expect(() => TreeState.getNumberOfVisibleDescendants({}, 0)).toThrowError(
77+
'Expected a State instance but got object',
78+
);
79+
expect(() => TreeState.getNumberOfVisibleDescendants(true, 0)).toThrowError(
80+
'Expected a State instance but got boolean',
81+
);
82+
expect(() => TreeState.getNumberOfVisibleDescendants(() => {}, 0)).toThrowError(
83+
'Expected a State instance but got function',
84+
);
85+
});
86+
87+
test('should get a correct number of descendants for a node with deep descendants', () => {
88+
expect(TreeState.getNumberOfVisibleDescendants(TreeState.createFromTree(Nodes), 0)).toEqual(4);
89+
});
90+
91+
test('should get a correct number of descendants for a node without grand children', () => {
92+
expect(TreeState.getNumberOfVisibleDescendants(TreeState.createFromTree(Nodes), 1)).toEqual(2);
93+
});
94+
95+
test('should get a correct number of descendants for a node with deep descendants', () => {
96+
expect(TreeState.getNumberOfVisibleDescendants(TreeState.createFromTree(Nodes), 0)).toEqual(4);
97+
});
98+
99+
test('should get 0 descendants for a node that does not have any descendants in the root node', () => {
100+
expect(TreeState.getNumberOfVisibleDescendants(TreeState.createFromTree(Nodes), 6)).toEqual(0);
101+
});
102+
103+
test('should get 0 descendants for a node that does not have any descendants', () => {
104+
expect(TreeState.getNumberOfVisibleDescendants(TreeState.createFromTree(Nodes), 3)).toEqual(0);
105+
});
106+
});
64107
});

0 commit comments

Comments
 (0)