1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as Tree from './tree';
7 describe('Tree', () => {
8 let tree: Tree.Tree<string>;
11 tree = Tree.createTree();
14 it('sets new node', () => {
15 const newTree = Tree.setNode({ children: [], id: 'Node 1', parent: '', value: 'Value 1' })(tree);
16 expect(Tree.getNode('Node 1')(newTree)).toEqual({ children: [], id: 'Node 1', parent: '', value: 'Value 1' });
19 it('adds new node reference to parent children', () => {
20 const [newTree] = [tree]
21 .map(Tree.setNode({ children: [], id: 'Node 1', parent: '', value: 'Value 1' }))
22 .map(Tree.setNode({ children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 2' }));
24 expect(Tree.getNode('Node 1')(newTree)).toEqual({ children: ['Node 2'], id: 'Node 1', parent: '', value: 'Value 1' });
27 it('gets node ancestors', () => {
29 { children: [], id: 'Node 1', parent: '', value: 'Value 1' },
30 { children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 1' },
31 { children: [], id: 'Node 3', parent: 'Node 2', value: 'Value 1' }
32 ].reduce((tree, node) => Tree.setNode(node)(tree), tree);
33 expect(Tree.getNodeAncestorsIds('Node 3')(newTree)).toEqual(['Node 1', 'Node 2']);
36 it('gets node descendants', () => {
38 { children: [], id: 'Node 1', parent: '', value: 'Value 1' },
39 { children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 1' },
40 { children: [], id: 'Node 2.1', parent: 'Node 2', value: 'Value 1' },
41 { children: [], id: 'Node 3', parent: 'Node 1', value: 'Value 1' },
42 { children: [], id: 'Node 3.1', parent: 'Node 3', value: 'Value 1' }
43 ].reduce((tree, node) => Tree.setNode(node)(tree), tree);
44 expect(Tree.getNodeDescendantsIds('Node 1')(newTree)).toEqual(['Node 2', 'Node 3', 'Node 2.1', 'Node 3.1']);
47 it('gets root descendants', () => {
49 { children: [], id: 'Node 1', parent: '', value: 'Value 1' },
50 { children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 1' },
51 { children: [], id: 'Node 2.1', parent: 'Node 2', value: 'Value 1' },
52 { children: [], id: 'Node 3', parent: 'Node 1', value: 'Value 1' },
53 { children: [], id: 'Node 3.1', parent: 'Node 3', value: 'Value 1' }
54 ].reduce((tree, node) => Tree.setNode(node)(tree), tree);
55 expect(Tree.getNodeDescendantsIds('')(newTree)).toEqual(['Node 1', 'Node 2', 'Node 3', 'Node 2.1', 'Node 3.1']);
58 it('gets node children', () => {
60 { children: [], id: 'Node 1', parent: '', value: 'Value 1' },
61 { children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 1' },
62 { children: [], id: 'Node 2.1', parent: 'Node 2', value: 'Value 1' },
63 { children: [], id: 'Node 3', parent: 'Node 1', value: 'Value 1' },
64 { children: [], id: 'Node 3.1', parent: 'Node 3', value: 'Value 1' }
65 ].reduce((tree, node) => Tree.setNode(node)(tree), tree);
66 expect(Tree.getNodeChildrenIds('Node 1')(newTree)).toEqual(['Node 2', 'Node 3']);
69 it('gets root children', () => {
71 { children: [], id: 'Node 1', parent: '', value: 'Value 1' },
72 { children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 1' },
73 { children: [], id: 'Node 2.1', parent: 'Node 2', value: 'Value 1' },
74 { children: [], id: 'Node 3', parent: '', value: 'Value 1' },
75 { children: [], id: 'Node 3.1', parent: 'Node 3', value: 'Value 1' }
76 ].reduce((tree, node) => Tree.setNode(node)(tree), tree);
77 expect(Tree.getNodeChildrenIds('')(newTree)).toEqual(['Node 1', 'Node 3']);
80 it('maps tree', () => {
82 { children: [], id: 'Node 1', parent: '', value: 'Value 1' },
83 { children: [], id: 'Node 2', parent: 'Node 1', value: 'Value 2' },
84 ].reduce((tree, node) => Tree.setNode(node)(tree), tree);
85 const mappedTree = Tree.mapTreeValues<string, number>(value => parseInt(value.split(' ')[1], 10))(newTree);
86 expect(Tree.getNode('Node 2')(mappedTree)).toEqual({ children: [], id: 'Node 2', parent: 'Node 1', value: 2 }, );