UNPKG

7.2 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const assert = require("assert");
19const tree_1 = require("./tree");
20const tree_model_1 = require("./tree-model");
21const mock_tree_model_1 = require("./test/mock-tree-model");
22const chai_1 = require("chai");
23const tree_test_container_1 = require("./test/tree-test-container");
24describe('Tree', () => {
25 it('addChildren', () => {
26 assertTreeNode(`{
27 "id": "parent",
28 "name": "parent",
29 "children": [
30 {
31 "id": "foo",
32 "name": "foo",
33 "parent": "parent",
34 "nextSibling": "bar"
35 },
36 {
37 "id": "bar",
38 "name": "bar",
39 "parent": "parent",
40 "previousSibling": "foo",
41 "nextSibling": "baz"
42 },
43 {
44 "id": "baz",
45 "name": "baz",
46 "parent": "parent",
47 "previousSibling": "bar"
48 }
49 ]
50}`, getNode());
51 });
52 it('removeChild - first', () => {
53 const node = getNode();
54 tree_1.CompositeTreeNode.removeChild(node, node.children[0]);
55 assertTreeNode(`{
56 "id": "parent",
57 "name": "parent",
58 "children": [
59 {
60 "id": "bar",
61 "name": "bar",
62 "parent": "parent",
63 "nextSibling": "baz"
64 },
65 {
66 "id": "baz",
67 "name": "baz",
68 "parent": "parent",
69 "previousSibling": "bar"
70 }
71 ]
72}`, node);
73 });
74 it('removeChild - second', () => {
75 const node = getNode();
76 tree_1.CompositeTreeNode.removeChild(node, node.children[1]);
77 assertTreeNode(`{
78 "id": "parent",
79 "name": "parent",
80 "children": [
81 {
82 "id": "foo",
83 "name": "foo",
84 "parent": "parent",
85 "nextSibling": "baz"
86 },
87 {
88 "id": "baz",
89 "name": "baz",
90 "parent": "parent",
91 "previousSibling": "foo"
92 }
93 ]
94}`, node);
95 });
96 it('removeChild - third', () => {
97 const node = getNode();
98 tree_1.CompositeTreeNode.removeChild(node, node.children[2]);
99 assertTreeNode(`{
100 "id": "parent",
101 "name": "parent",
102 "children": [
103 {
104 "id": "foo",
105 "name": "foo",
106 "parent": "parent",
107 "nextSibling": "bar"
108 },
109 {
110 "id": "bar",
111 "name": "bar",
112 "parent": "parent",
113 "previousSibling": "foo"
114 }
115 ]
116}`, node);
117 });
118 let model;
119 beforeEach(() => {
120 model = createTreeModel();
121 model.root = mock_tree_model_1.MockTreeModel.HIERARCHICAL_MOCK_ROOT();
122 });
123 describe('getNode', () => {
124 it('returns undefined for undefined nodes', done => {
125 (0, chai_1.expect)(model.getNode(undefined)).to.be.undefined;
126 done();
127 });
128 it('returns undefined for a non-existing id', done => {
129 (0, chai_1.expect)(model.getNode('10')).to.be.undefined;
130 done();
131 });
132 it('returns a valid node for existing an id', done => {
133 (0, chai_1.expect)(model.getNode('1.1')).not.to.be.undefined;
134 done();
135 });
136 });
137 describe('validateNode', () => {
138 it('returns undefined for undefined nodes', done => {
139 (0, chai_1.expect)(model.validateNode(undefined)).to.be.undefined;
140 done();
141 });
142 it('returns undefined for non-existing nodes', done => {
143 (0, chai_1.expect)(model.validateNode(mock_tree_model_1.MockTreeModel.Node.toTreeNode({ 'id': '10' }))).to.be.undefined;
144 done();
145 });
146 it('returns a valid node for an existing node', done => {
147 (0, chai_1.expect)(model.validateNode(retrieveNode('1.1'))).not.to.be.undefined;
148 done();
149 });
150 });
151 describe('refresh', () => {
152 it('refreshes all composite nodes starting with the root', done => {
153 let result = true;
154 const expectedRefreshedNodes = new Set([
155 retrieveNode('1'),
156 retrieveNode('1.1'),
157 retrieveNode('1.2'),
158 retrieveNode('1.2.1')
159 ]);
160 model.onNodeRefreshed((e) => {
161 result = result && expectedRefreshedNodes.has(e);
162 expectedRefreshedNodes.delete(e);
163 });
164 model.refresh().then(() => {
165 (0, chai_1.expect)(result).to.be.true;
166 (0, chai_1.expect)(expectedRefreshedNodes.size).to.be.equal(0);
167 done();
168 });
169 });
170 });
171 describe('refresh(parent: Readonly<CompositeTreeNode>)', () => {
172 it('refreshes all composite nodes starting with the provided node', done => {
173 let result = true;
174 const expectedRefreshedNodes = new Set([
175 retrieveNode('1.2'),
176 retrieveNode('1.2.1')
177 ]);
178 model.onNodeRefreshed((e) => {
179 result = result && expectedRefreshedNodes.has(e);
180 expectedRefreshedNodes.delete(e);
181 });
182 model.refresh(retrieveNode('1.2')).then(() => {
183 (0, chai_1.expect)(result).to.be.true;
184 (0, chai_1.expect)(expectedRefreshedNodes.size).to.be.equal(0);
185 done();
186 });
187 });
188 });
189 function getNode() {
190 return tree_1.CompositeTreeNode.addChildren({
191 id: 'parent',
192 name: 'parent',
193 children: [],
194 parent: undefined
195 }, [{
196 id: 'foo',
197 name: 'foo',
198 parent: undefined
199 }, {
200 id: 'bar',
201 name: 'bar',
202 parent: undefined
203 }, {
204 id: 'baz',
205 name: 'baz',
206 parent: undefined
207 }]);
208 }
209 function assertTreeNode(expectation, node) {
210 // eslint-disable-next-line @typescript-eslint/no-explicit-any
211 assert.deepStrictEqual(expectation, JSON.stringify(node, (key, value) => {
212 if (key === 'parent' || key === 'previousSibling' || key === 'nextSibling') {
213 return value && value.id;
214 }
215 return value;
216 }, 2));
217 }
218 function createTreeModel() {
219 const container = (0, tree_test_container_1.createTreeTestContainer)();
220 return container.get(tree_model_1.TreeModel);
221 }
222 function retrieveNode(id) {
223 const readonlyNode = model.getNode(id);
224 return readonlyNode;
225 }
226});
227//# sourceMappingURL=tree.spec.js.map
\No newline at end of file