-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathtree-walker.test.js
More file actions
79 lines (69 loc) · 2.37 KB
/
Copy pathtree-walker.test.js
File metadata and controls
79 lines (69 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-env jest */
import { getAllNodes, getRoot } from './tree-walker'
const testTrees = (() => {
const _edges = {
// Tree one.
a: { parent: undefined, children: ['b', 'c'] },
b: { parent: 'a', children: [] },
c: { parent: 'a', children: ['d', 'e'] },
d: { parent: 'c', children: [] },
e: { parent: 'c', children: [] },
// Tree two.
f: { parent: undefined, children: ['g'] },
g: { parent: 'f', children: ['h'] },
h: { parent: 'g', children: [] },
}
return {
_edges,
getParent: async node => _edges[node].parent,
getChildren: async node => _edges[node].children,
}
})()
describe('getAllNodes', () => {
const getAllNodesOfTestTree = getAllNodes(testTrees)
test('gets all nodes given a leaf node', async () => {
const nodes = [
(await getAllNodesOfTestTree('e')).sort(),
(await getAllNodesOfTestTree('h')).sort(),
]
expect(nodes).toEqual([['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h']])
})
test('gets all nodes given a non-leaf node', async () => {
const nodes = [
(await getAllNodesOfTestTree('c')).sort(),
(await getAllNodesOfTestTree('g')).sort(),
]
expect(nodes).toEqual([['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h']])
})
test('gets all nodes given the root node', async () => {
const nodes = [
(await getAllNodesOfTestTree('a')).sort(),
(await getAllNodesOfTestTree('f')).sort(),
]
expect(nodes).toEqual([['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h']])
})
})
describe('getRoot', () => {
const getRootOfTestTree = getRoot(testTrees)
test('gets the root given a leaf node', async () => {
const roots = [
await getRootOfTestTree('e'),
await getRootOfTestTree('h'),
]
expect(roots).toEqual(['a', 'f'])
})
test('gets the root given a non-leaf node', async () => {
const roots = [
await getRootOfTestTree('c'),
await getRootOfTestTree('g'),
]
expect(roots).toEqual(['a', 'f'])
})
test('gets the root given the root node itself', async () => {
const roots = [
await getRootOfTestTree('a'),
await getRootOfTestTree('f'),
]
expect(roots).toEqual(['a', 'f'])
})
})