Skip to content

Commit 7d32ac1

Browse files
committed
Added test script + simple test for flattenTree()
1 parent ebda8a6 commit 7d32ac1

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"build:es": "npm run clean:es && cross-env NODE_ENV=production cross-env BABEL_ENV=es babel src --out-dir dist/es --ignore *.spec.js",
2525
"prepublish": "npm run build",
2626
"clean:commonjs": "rimraf dist/commonjs",
27-
"clean:es": "rimraf dist/es"
27+
"clean:es": "rimraf dist/es",
28+
"test": "mocha \"test/**/*spec.js*\" --compilers js:babel-register --reporter=dot"
2829
},
2930
"files": [
3031
"dist"

src/StickyTree.jsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export default class StickyTree extends React.PureComponent {
205205
}
206206

207207
/**
208-
* Returns the parent path for the specified index within nodePosCache.
208+
* Returns the parent path from nodePosCache for the specified index within nodePosCache.
209209
* @param nodeIndex
210210
* @returns {Array<Node>}
211211
*/
@@ -221,6 +221,12 @@ export default class StickyTree extends React.PureComponent {
221221
return path.reverse();
222222
}
223223

224+
/**
225+
* Searches from the current node position downwards to see if the top of nodes above are greater
226+
* than or equal to the current scrollTop
227+
* @param scrollTop
228+
* @returns {number}
229+
*/
224230
forwardSearch(scrollTop) {
225231
const nodePosCache = this.nodePosCache;
226232
for (let i = this.state.currNodePos; i < nodePosCache.length; i++) {
@@ -231,6 +237,12 @@ export default class StickyTree extends React.PureComponent {
231237
return nodePosCache.length - 1;
232238
}
233239

240+
/**
241+
* Searches from the current node position upwards to see if the top of nodes above are less than
242+
* or equal the current scrollTop.
243+
* @param scrollTop
244+
* @returns {number}
245+
*/
234246
backwardSearch(scrollTop) {
235247
const nodePosCache = this.nodePosCache;
236248
for (let i = this.state.currNodePos; i >= 0; i--) {

test/StickyTree.spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect } from 'chai';
2+
3+
import { StickyTree } from '../src/index';
4+
5+
describe('StickyTree test', () => {
6+
let tree;
7+
let props;
8+
9+
it('flattenTree returns a cache of all required info', () => {
10+
const treeNodes = [
11+
{ name: 'root', children: [1, 4] },
12+
{ name: 'node1', children: [2, 3] },
13+
{ name: 'node1-1' },
14+
{ name: 'node1-2' },
15+
{ name: 'node2', children: [5, 6, 7] },
16+
{ name: 'node2-1' },
17+
{ name: 'node2-2' },
18+
{ name: 'node2-3' }
19+
];
20+
21+
props = {
22+
getChildren: pos => treeNodes[pos].children,
23+
getHeight: pos => 10
24+
25+
};
26+
tree = new StickyTree(props);
27+
const nodePosCache = tree.flattenTree(0);
28+
29+
expect(nodePosCache[0]).to.deep.equal({
30+
node: 0,
31+
top: 0,
32+
parentIndex: undefined,
33+
index: 0,
34+
children: [1, 4],
35+
height: 80
36+
});
37+
38+
expect(nodePosCache[1]).to.deep.equal({
39+
node: 1,
40+
top: 10,
41+
parentIndex: 0,
42+
index: 1,
43+
children: [2, 3],
44+
height: 30
45+
});
46+
47+
expect(nodePosCache[2]).to.deep.equal({ node: 2, top: 20, parentIndex: 1, index: 2, height: 10 });
48+
49+
expect(nodePosCache[4]).to.deep.equal({
50+
node: 4,
51+
top: 40,
52+
parentIndex: 0,
53+
index: 4,
54+
children: [5, 6, 7],
55+
height: 40
56+
});
57+
});
58+
});

0 commit comments

Comments
 (0)