UNPKG

22.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var test_1 = require("../test");
4var _1 = require(".");
5var create = _1.TreeQuery.create;
6describe('TreeQuery', function () {
7 describe('create', function () {
8 it('with root (default node type)', function () {
9 var root = { id: 'root' };
10 var query = create(root);
11 test_1.expect(query.root).to.equal(root);
12 test_1.expect(query.namespace).to.eql('');
13 });
14 it('with root (specific node type)', function () {
15 var root = { id: 'root', count: 0 };
16 var query = create(root);
17 test_1.expect(query.root).to.equal(root);
18 test_1.expect(query.namespace).to.eql('');
19 });
20 it('with namespace', function () {
21 var test = function (namespace, expected) {
22 var root = { id: 'root' };
23 var query = create({ root: root, namespace: namespace });
24 test_1.expect(query.namespace).to.eql(expected);
25 };
26 test('', '');
27 test(' ', '');
28 test('foo', 'foo');
29 test(' foo ', 'foo');
30 });
31 });
32 describe('children (static)', function () {
33 it('no arguments', function () {
34 var count = 0;
35 var res = _1.TreeQuery.children(undefined, function () { return count++; });
36 test_1.expect(res).to.eql([]);
37 test_1.expect(count).to.eql(1);
38 });
39 it('no childen => empty array (and assigns by default)', function () {
40 var node = { id: 'root' };
41 test_1.expect(node.children).to.eql(undefined);
42 var res = _1.TreeQuery.children(node);
43 test_1.expect(res).to.eql([]);
44 test_1.expect(node.children).to.equal(res);
45 });
46 it('no childen => empty array (and does not assign)', function () {
47 var node = { id: 'root' };
48 test_1.expect(node.children).to.eql(undefined);
49 var res1 = _1.TreeQuery.children(node, { assign: false });
50 test_1.expect(res1).to.eql([]);
51 test_1.expect(node.children).to.equal(undefined);
52 var res2 = _1.TreeQuery.children(node, undefined, { assign: false });
53 test_1.expect(res2).to.eql([]);
54 test_1.expect(node.children).to.equal(undefined);
55 });
56 it('returns child array (instance)', function () {
57 var node = { id: 'root', children: [{ id: 'child' }] };
58 test_1.expect(_1.TreeQuery.children(node)).to.eql([{ id: 'child' }]);
59 });
60 it('runs visitor function', function () {
61 var node = { id: 'root', children: [{ id: 'child-1' }, { id: 'child-2' }] };
62 var visits = [];
63 _1.TreeQuery.children(node, function (children) { return children.forEach(function (child) { return visits.push(child); }); });
64 test_1.expect(visits.map(function (c) { return c.id; })).to.eql(['child-1', 'child-2']);
65 });
66 });
67 describe('hasChild (static)', function () {
68 var root = { id: 'A', children: [{ id: 'B' }, { id: 'C' }] };
69 it('does have child', function () {
70 test_1.expect(_1.TreeQuery.hasChild(root, 'B')).to.eql(true);
71 test_1.expect(_1.TreeQuery.hasChild(root, 'C')).to.eql(true);
72 test_1.expect(_1.TreeQuery.hasChild(root, { id: 'C' })).to.eql(true);
73 });
74 it('does not have child', function () {
75 test_1.expect(_1.TreeQuery.hasChild(root, 'A')).to.eql(false);
76 test_1.expect(_1.TreeQuery.hasChild(root, 'NO_MATCH')).to.eql(false);
77 test_1.expect(_1.TreeQuery.hasChild(root, undefined)).to.eql(false);
78 test_1.expect(_1.TreeQuery.hasChild(undefined, undefined)).to.eql(false);
79 });
80 });
81 describe('walkDown', function () {
82 it('walkDown from root', function () {
83 var tree = {
84 id: 'root',
85 children: [{ id: 'child-1' }, { id: 'child-2' }],
86 };
87 var query = create(tree);
88 var items = [];
89 query.walkDown(function (e) { return items.push(e); });
90 test_1.expect(items.length).to.eql(3);
91 test_1.expect(items[0].node).to.equal(tree);
92 test_1.expect(items[1].node).to.equal(tree.children && tree.children[0]);
93 test_1.expect(items[2].node).to.equal(tree.children && tree.children[1]);
94 test_1.expect(items.every(function (m) { return m.namespace === ''; })).to.eql(true);
95 });
96 it('walkDown from root (with namespace)', function () {
97 var tree = {
98 id: 'ns:root',
99 children: [{ id: 'ns:child-1' }, { id: 'ns:child-2' }],
100 };
101 var query = create(tree);
102 var items = [];
103 query.walkDown(function (e) { return items.push(e); });
104 test_1.expect(items.length).to.eql(3);
105 test_1.expect(items.every(function (m) { return m.namespace === 'ns'; })).to.eql(true);
106 test_1.expect(items[0].key).to.equal('root');
107 test_1.expect(items[0].id).to.equal('ns:root');
108 test_1.expect(items[0].node.id).to.equal('ns:root');
109 test_1.expect(items[1].key).to.equal('child-1');
110 test_1.expect(items[1].id).to.equal('ns:child-1');
111 test_1.expect(items[1].node.id).to.equal('ns:child-1');
112 test_1.expect(items[2].key).to.equal('child-2');
113 test_1.expect(items[2].id).to.equal('ns:child-2');
114 test_1.expect(items[2].node.id).to.equal('ns:child-2');
115 });
116 it('walkDown: skip (children)', function () {
117 var tree = {
118 id: 'root',
119 children: [
120 { id: 'child-1', children: [{ id: 'child-1.1' }, { id: 'child-1.1' }] },
121 { id: 'child-2' },
122 ],
123 };
124 var query = create(tree);
125 var nodes = [];
126 query.walkDown(function (e) {
127 nodes.push(e.node);
128 if (e.node.id === 'child-1') {
129 e.skip();
130 }
131 });
132 test_1.expect(nodes.length).to.eql(3);
133 test_1.expect(nodes[0].id).to.eql('root');
134 test_1.expect(nodes[1].id).to.eql('child-1');
135 test_1.expect(nodes[2].id).to.eql('child-2');
136 });
137 it('walkDown: stop mid-way', function () {
138 var root = {
139 id: 'root',
140 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'child-2.1' }] }],
141 };
142 var state = create(root);
143 var walked = [];
144 state.walkDown(function (e) {
145 walked.push(e);
146 if (e.id === 'child-1') {
147 e.stop();
148 }
149 });
150 test_1.expect(walked.length).to.eql(2);
151 test_1.expect(walked[0].id).to.eql('root');
152 test_1.expect(walked[1].id).to.eql('child-1');
153 });
154 it('walkDown: passes level/parent to visitor', function () {
155 var grandchild = { id: 'grandchild' };
156 var child = { id: 'child', children: [grandchild] };
157 var root = { id: 'root', children: [child] };
158 var query = create(root);
159 var items = [];
160 query.walkDown(function (e) { return items.push(e); });
161 test_1.expect(items.length).to.eql(3);
162 test_1.expect(items[0].level).to.eql(0);
163 test_1.expect(items[1].level).to.eql(1);
164 test_1.expect(items[2].level).to.eql(2);
165 test_1.expect(items[0].parent).to.eql(undefined);
166 test_1.expect(items[1].parent).to.eql(root);
167 test_1.expect(items[2].parent).to.eql(child);
168 });
169 it('walkDown: passes index to visitor (sibling position)', function () {
170 var root = {
171 id: 'root',
172 children: [{ id: 'child-1' }, { id: 'child-2' }],
173 };
174 var query = create(root);
175 var items = [];
176 query.walkDown(function (e) { return items.push(e); });
177 test_1.expect(items[0].index).to.eql(-1);
178 test_1.expect(items[1].index).to.eql(0);
179 test_1.expect(items[2].index).to.eql(1);
180 });
181 it('walkDown: does not walk down into child namespace', function () {
182 var _a;
183 var root = {
184 id: 'ns1:root',
185 children: [{ id: 'ns1:child-1' }, { id: 'ns1:child-2', children: [{ id: 'ns2:foo' }] }],
186 };
187 var query = create({ root: root, namespace: 'ns1' });
188 test_1.expect(query.namespace).to.eql('ns1');
189 test_1.expect((_a = create(root).findById('ns2:foo')) === null || _a === void 0 ? void 0 : _a.id).to.eql('ns2:foo');
190 var walked = [];
191 query.walkDown(function (e) { return walked.push(e); });
192 var ids = walked.map(function (e) { return e.id; });
193 test_1.expect(ids.length).to.eql(3);
194 test_1.expect(ids.includes('ns2:foo')).to.eql(false);
195 });
196 });
197 describe('walkUp', function () {
198 it('walkUp: to root', function () {
199 var tree = {
200 id: 'root',
201 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'grandchild-1' }] }],
202 };
203 var query = create(tree);
204 var start = query.findById('grandchild-1');
205 var items = [];
206 query.walkUp(start, function (e) { return items.push(e); });
207 test_1.expect(items.length).to.eql(3);
208 test_1.expect(items.map(function (e) { return e.node.id; })).to.eql(['grandchild-1', 'child-2', 'root']);
209 test_1.expect(items.every(function (m) { return m.namespace === ''; })).to.eql(true);
210 test_1.expect(items[0].parent && items[0].parent.id).to.eql('child-2');
211 test_1.expect(items[1].parent && items[1].parent.id).to.eql('root');
212 test_1.expect(items[2].parent && items[2].parent.id).to.eql(undefined);
213 test_1.expect(items[0].index).to.eql(0);
214 test_1.expect(items[1].index).to.eql(1);
215 test_1.expect(items[2].index).to.eql(-1);
216 });
217 it('walkUp: to root (with namespace)', function () {
218 var tree = {
219 id: 'ns:root',
220 children: [
221 { id: 'ns:child-1' },
222 { id: 'ns:child-2', children: [{ id: 'ns:grandchild-1' }] },
223 ],
224 };
225 var query = create(tree);
226 var start = query.findById('ns:grandchild-1');
227 var items = [];
228 query.walkUp(start, function (e) { return items.push(e); });
229 test_1.expect(items.length).to.eql(3);
230 test_1.expect(items.every(function (m) { return m.namespace === 'ns'; })).to.eql(true);
231 test_1.expect(items.map(function (e) { return e.key; })).to.eql(['grandchild-1', 'child-2', 'root']);
232 test_1.expect(items.map(function (e) { return e.node.id; })).to.eql(['ns:grandchild-1', 'ns:child-2', 'ns:root']);
233 });
234 it('walkUp: stop mid-way', function () {
235 var tree = {
236 id: 'root',
237 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'grandchild-1' }] }],
238 };
239 var query = create(tree);
240 var start = query.findById('grandchild-1');
241 var list = [];
242 query.walkUp(start, function (e) {
243 list.push(e);
244 if (e.node.id === 'child-2') {
245 e.stop();
246 }
247 });
248 test_1.expect(list.length).to.eql(2);
249 test_1.expect(list.map(function (e) { return e.id; })).to.eql(['grandchild-1', 'child-2']);
250 });
251 it('walkUp: startAt not found', function () {
252 var tree = {
253 id: 'root',
254 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'grandchild-1' }] }],
255 };
256 var query = create(tree);
257 var test = function (startAt) {
258 var walked = [];
259 query.walkUp(startAt, function (e) { return walked.push(e); });
260 test_1.expect(walked).to.eql([]);
261 };
262 test();
263 test('404');
264 test({ id: '404' });
265 });
266 it('walkUp: passes level (from start of ascent)', function () {
267 var tree = {
268 id: 'root',
269 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'grandchild-1' }] }],
270 };
271 var query = create(tree);
272 var walked = [];
273 query.walkUp('grandchild-1', function (e) { return walked.push(e); });
274 test_1.expect(walked.map(function (e) { return e.level; })).to.eql([0, 1, 2]);
275 });
276 it('walkUp: does not walk up into parent namespace', function () {
277 var tree = {
278 id: 'ns1:root',
279 children: [
280 { id: 'ns1:child-1' },
281 { id: 'ns2:child-2', children: [{ id: 'ns2:child-2.1' }] },
282 ],
283 };
284 var root = create(tree).findById('ns2:child-2');
285 var child = create(tree).findById('ns2:child-2.1');
286 test_1.expect(child).to.exist;
287 var query = create({ root: root, namespace: 'ns2' });
288 var test = function (startAt) {
289 var walked = [];
290 query.walkUp(startAt, function (e) { return walked.push(e); });
291 test_1.expect(walked.map(function (e) { return e.key; })).to.eql(['child-2.1', 'child-2']);
292 };
293 test(child);
294 test(child === null || child === void 0 ? void 0 : child.id);
295 test('child-2.1');
296 });
297 });
298 describe('find', function () {
299 it('no namespace', function () {
300 var tree = {
301 id: 'root',
302 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'child-3' }] }],
303 };
304 var query = create(tree);
305 var res1 = query.find(function (e) { return e.node.id === 'child-3'; });
306 var res2 = query.find(function (e) { return e.node.id === 'NO_EXIT'; });
307 test_1.expect(res1).to.eql({ id: 'child-3' });
308 test_1.expect(res2).to.eql(undefined);
309 });
310 it('with namespace', function () {
311 var tree = {
312 id: 'ns:root',
313 children: [{ id: 'child-1' }, { id: 'ns:child-2', children: [{ id: 'ns:child-3' }] }],
314 };
315 var query = create(tree);
316 var res1a = query.find(function (e) { return e.namespace === 'ns' && e.key === 'child-3'; });
317 var res1b = query.find(function (e) { return e.id === 'ns:child-3'; });
318 var res2 = query.find(function (e) { return e.namespace === 'foo' && e.key === 'child-3'; });
319 test_1.expect(res1a).to.eql({ id: 'ns:child-3' });
320 test_1.expect(res1b).to.eql({ id: 'ns:child-3' });
321 test_1.expect(res2).to.eql(undefined);
322 });
323 it('root with namespace', function () {
324 var tree = {
325 id: 'root',
326 children: [
327 { id: 'child-1' },
328 { id: 'ns:child-2', children: [{ id: 'ns:child-2.1' }, { id: 'ns:child-2.1' }] },
329 ],
330 };
331 var query = create(tree);
332 var res = query.find(function (e) { return e.namespace === 'ns'; });
333 test_1.expect(res === null || res === void 0 ? void 0 : res.id).to.eql('ns:child-2');
334 });
335 });
336 describe('findById', function () {
337 it('no namespace', function () {
338 var tree = {
339 id: 'root',
340 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'child-3' }] }],
341 };
342 var query = create(tree);
343 var res1 = query.findById('child-3');
344 var res2 = query.findById('NO_EXIST');
345 var res3 = query.findById(undefined);
346 var res4 = query.findById({ id: 'child-2' });
347 test_1.expect(res1).to.eql({ id: 'child-3' });
348 test_1.expect(res2).to.eql(undefined);
349 test_1.expect(res3).to.eql(undefined);
350 test_1.expect(res4).to.eql({ id: 'child-2', children: [{ id: 'child-3' }] });
351 });
352 it('within namespace', function () {
353 var _a, _b, _c, _d, _e, _f, _g, _h, _j;
354 var root = {
355 id: 'ns1:root',
356 children: [{ id: 'ns1:child-1' }, { id: 'ns2:child-2' }, { id: 'foo' }],
357 };
358 var query = create({ root: root });
359 var ns1 = create({ root: root, namespace: 'ns1' });
360 test_1.expect((_a = query.findById('root')) === null || _a === void 0 ? void 0 : _a.id).to.eql('ns1:root');
361 test_1.expect(query.findById('foo:root')).to.eql(undefined);
362 test_1.expect((_b = query.findById('ns1:root')) === null || _b === void 0 ? void 0 : _b.id).to.eql('ns1:root');
363 test_1.expect((_c = query.findById('ns1:child-1')) === null || _c === void 0 ? void 0 : _c.id).to.eql('ns1:child-1');
364 test_1.expect((_d = query.findById('ns2:child-2')) === null || _d === void 0 ? void 0 : _d.id).to.eql('ns2:child-2');
365 test_1.expect((_e = query.findById('foo')) === null || _e === void 0 ? void 0 : _e.id).to.eql('foo');
366 test_1.expect((_f = ns1.findById('root')) === null || _f === void 0 ? void 0 : _f.id).to.eql('ns1:root');
367 test_1.expect((_g = ns1.findById('ns1:root')) === null || _g === void 0 ? void 0 : _g.id).to.eql('ns1:root');
368 test_1.expect(ns1.findById('foo:root')).to.eql(undefined);
369 test_1.expect(ns1.findById('404')).to.eql(undefined);
370 test_1.expect(ns1.findById('ns1:404')).to.eql(undefined);
371 test_1.expect((_h = ns1.findById('child-1')) === null || _h === void 0 ? void 0 : _h.id).to.eql('ns1:child-1');
372 test_1.expect((_j = ns1.findById('ns1:child-1')) === null || _j === void 0 ? void 0 : _j.id).to.eql('ns1:child-1');
373 test_1.expect(ns1.findById('child-2')).to.eql(undefined);
374 test_1.expect(ns1.findById('ns2:child-2')).to.eql(undefined);
375 test_1.expect(ns1.findById('foo')).to.eql(undefined);
376 });
377 });
378 describe('parent', function () {
379 it('has a parent', function () {
380 var grandchild = { id: 'grandchild' };
381 var child = { id: 'child', children: [grandchild] };
382 var root = { id: 'root', children: [child] };
383 var query = create(root);
384 test_1.expect(query.parent(child)).to.equal(root);
385 test_1.expect(query.parent(grandchild)).to.equal(child);
386 });
387 it('has no parent', function () {
388 var grandchild = { id: 'grandchild' };
389 var child = { id: 'child', children: [grandchild] };
390 var root = { id: 'root', children: [] };
391 var query = create(root);
392 test_1.expect(query.parent(child)).to.equal(undefined);
393 test_1.expect(query.parent(grandchild)).to.equal(undefined);
394 });
395 });
396 describe('ancestor', function () {
397 var tree = {
398 id: 'root',
399 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'child-3' }] }],
400 };
401 it('matches self (first)', function () {
402 var query = create(tree);
403 var node = query.findById('child-3');
404 var res = query.ancestor(node, function (e) { return e.node.id === 'child-3'; });
405 test_1.expect(res && res.id).to.eql('child-3');
406 });
407 it('finds matching ancestor', function () {
408 var query = create(tree);
409 var node = query.findById('child-3');
410 var res1 = query.ancestor(node, function (e) { return e.node.id === 'child-2'; });
411 var res2 = query.ancestor(node, function (e) { return e.node.id === 'root'; });
412 test_1.expect(res1 && res1.id).to.eql('child-2');
413 test_1.expect(res2 && res2.id).to.eql('root');
414 });
415 it('no match', function () {
416 var query = create(tree);
417 var node = query.findById('root');
418 var res = query.ancestor(node, function (e) { return e.node.id === 'child-1'; });
419 test_1.expect(res).to.eql(undefined);
420 });
421 });
422 describe('depth', function () {
423 var root = {
424 id: 'A',
425 children: [{ id: 'B' }, { id: 'C', children: [{ id: 'D' }] }],
426 };
427 it('retrieves depth', function () {
428 var query = create(root);
429 test_1.expect(query.depth('A')).to.eql(0);
430 test_1.expect(query.depth({ id: 'A' })).to.eql(0);
431 test_1.expect(query.depth('B')).to.eql(1);
432 test_1.expect(query.depth('C')).to.eql(1);
433 test_1.expect(query.depth('D')).to.eql(2);
434 test_1.expect(query.depth({ id: 'D' })).to.eql(2);
435 });
436 it('-1', function () {
437 var query = create({ id: 'root' });
438 test_1.expect(query.depth('C')).to.eql(-1);
439 test_1.expect(query.depth(undefined)).to.eql(-1);
440 test_1.expect(query.depth('NO_EXIST')).to.eql(-1);
441 test_1.expect(query.depth({ id: 'NO_EXIST' })).to.eql(-1);
442 });
443 });
444 describe('exists', function () {
445 var tree = {
446 id: 'root',
447 children: [{ id: 'child-1' }, { id: 'child-2', children: [{ id: 'child-3' }] }],
448 };
449 it('exists', function () {
450 var query = create(tree);
451 test_1.expect(query.exists('root')).to.eql(true);
452 test_1.expect(query.exists('child-3')).to.eql(true);
453 test_1.expect(query.exists({ id: 'root' })).to.eql(true);
454 test_1.expect(query.exists({ id: 'child-3' })).to.eql(true);
455 test_1.expect(query.exists(function (e) { return e.id === 'child-3'; })).to.eql(true);
456 });
457 it('does not exist', function () {
458 var query = create(tree);
459 test_1.expect(query.exists('404')).to.eql(false);
460 test_1.expect(query.exists({ id: '404' })).to.eql(false);
461 test_1.expect(query.exists(function (e) { return e.id === '404'; })).to.eql(false);
462 });
463 });
464});