UNPKG

6.29 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 Thomas Drosdzoll.
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 chai_1 = require("chai");
19const mock_tree_model_1 = require("./test/mock-tree-model");
20const tree_model_1 = require("./tree-model");
21const tree_1 = require("./tree");
22const tree_test_container_1 = require("./test/tree-test-container");
23const promise_util_1 = require("../../common/promise-util");
24describe('TreeExpansionService', () => {
25 let model;
26 beforeEach(() => {
27 model = createTreeModel();
28 model.root = mock_tree_model_1.MockTreeModel.HIERARCHICAL_MOCK_ROOT();
29 });
30 describe('expandNode', () => {
31 it("won't expand an already expanded node", done => {
32 const node = retrieveNode('1');
33 model.expandNode(node).then(result => {
34 (0, chai_1.expect)(result).to.be.undefined;
35 done();
36 });
37 });
38 it('will expand a collapsed node', done => {
39 const node = retrieveNode('1');
40 model.collapseNode(node).then(() => {
41 model.expandNode(node).then(result => {
42 (0, chai_1.expect)(result).to.be.eq(result);
43 done();
44 });
45 });
46 });
47 it("won't expand an undefined node", done => {
48 model.expandNode(undefined).then(result => {
49 (0, chai_1.expect)(result).to.be.undefined;
50 done();
51 });
52 });
53 });
54 describe('collapseNode', () => {
55 it('will collapse an expanded node', done => {
56 const node = retrieveNode('1');
57 model.collapseNode(node).then(result => {
58 (0, chai_1.expect)(result).to.be.eq(result);
59 done();
60 });
61 });
62 it("won't collapse an already collapsed node", done => {
63 const node = retrieveNode('1');
64 model.collapseNode(node).then(() => {
65 model.collapseNode(node).then(result => {
66 (0, chai_1.expect)(result).to.be.false;
67 done();
68 });
69 });
70 });
71 it('cannot collapse a leaf node', done => {
72 const node = retrieveNode('1.1.2');
73 model.collapseNode(node).then(result => {
74 (0, chai_1.expect)(result).to.be.false;
75 done();
76 });
77 });
78 });
79 describe('collapseAll', () => {
80 it('will collapse all nodes recursively', done => {
81 model.collapseAll(retrieveNode('1')).then(result => {
82 (0, chai_1.expect)(result).to.be.eq(result);
83 done();
84 });
85 });
86 it("won't collapse nodes recursively if the root node is collapsed already", done => {
87 model.collapseNode(retrieveNode('1')).then(() => {
88 model.collapseAll(retrieveNode('1')).then(result => {
89 (0, chai_1.expect)(result).to.be.eq(result);
90 done();
91 });
92 });
93 });
94 });
95 describe('toggleNodeExpansion', () => {
96 it('changes the expansion state from expanded to collapsed', done => {
97 const node = retrieveNode('1');
98 model.onExpansionChanged((e) => {
99 (0, chai_1.expect)(e).to.be.equal(node);
100 (0, chai_1.expect)(e.expanded).to.be.false;
101 });
102 model.toggleNodeExpansion(node).then(() => {
103 done();
104 });
105 });
106 it('changes the expansion state from collapsed to expanded', done => {
107 const node = retrieveNode('1');
108 model.collapseNode(node).then(() => {
109 });
110 model.onExpansionChanged((e) => {
111 (0, chai_1.expect)(e).to.be.equal(node);
112 (0, chai_1.expect)(e.expanded).to.be.true;
113 });
114 model.toggleNodeExpansion(node).then(() => {
115 done();
116 });
117 });
118 });
119 it('node should be refreshed on expansion', async () => {
120 const container = (0, tree_test_container_1.createTreeTestContainer)();
121 container.rebind(tree_1.Tree).to(class extends tree_1.TreeImpl {
122 async resolveChildren(parent) {
123 await (0, promise_util_1.timeout)(200);
124 return [{
125 id: 'child',
126 parent
127 }];
128 }
129 });
130 const root = {
131 id: 'parent',
132 parent: undefined,
133 children: [],
134 expanded: false
135 };
136 const treeModel = container.get(tree_model_1.TreeModel);
137 treeModel.root = root;
138 chai_1.assert.isFalse(root.expanded, 'before');
139 chai_1.assert.equal(root.children.length, 0, 'before');
140 const expanding = treeModel.expandNode(root);
141 chai_1.assert.isFalse(root.expanded, 'between');
142 chai_1.assert.equal(root.children.length, 0, 'between');
143 await expanding;
144 chai_1.assert.isTrue(root.expanded, 'after');
145 chai_1.assert.equal(root.children.length, 1, 'after');
146 });
147 function createTreeModel() {
148 const container = (0, tree_test_container_1.createTreeTestContainer)();
149 return container.get(tree_model_1.TreeModel);
150 }
151 function retrieveNode(id) {
152 const readonlyNode = model.getNode(id);
153 return readonlyNode;
154 }
155});
156//# sourceMappingURL=tree-expansion.spec.js.map
\No newline at end of file