2024-04-18 21:55:33 +02:00
|
|
|
import { tap, expect } from '@push.rocks/tapbundle';
|
2022-05-27 17:53:02 +02:00
|
|
|
import * as lik from '../ts/index.js';
|
2017-11-20 09:26:13 +01:00
|
|
|
|
|
|
|
|
class TestClass {
|
2018-07-15 16:04:27 +02:00
|
|
|
constructor(public hey: string) {
|
2017-11-20 09:26:13 +01:00
|
|
|
// nothing here
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-31 17:24:35 +02:00
|
|
|
|
|
|
|
|
let testTree = new lik.Tree<TestClass>();
|
|
|
|
|
|
2018-07-15 16:04:27 +02:00
|
|
|
let testInstance = new TestClass('first');
|
2018-08-31 17:24:35 +02:00
|
|
|
let testInstance2 = new TestClass('second');
|
|
|
|
|
let testInstance3 = new TestClass('third');
|
|
|
|
|
let testInstance4 = new TestClass('fourth');
|
|
|
|
|
let testInstance5 = new TestClass('fifth');
|
|
|
|
|
let testInstance6 = new TestClass('sixth');
|
2017-11-20 09:26:13 +01:00
|
|
|
|
|
|
|
|
tap.test('create a valid tree instance', async () => {
|
2018-07-15 16:04:27 +02:00
|
|
|
testTree = new lik.Tree();
|
2022-01-24 05:22:49 +01:00
|
|
|
expect(testTree).toBeInstanceOf(lik.Tree);
|
2018-07-15 16:04:27 +02:00
|
|
|
});
|
2017-11-20 09:26:13 +01:00
|
|
|
|
|
|
|
|
tap.test('should insert an object', async () => {
|
2018-07-15 16:04:27 +02:00
|
|
|
testTree.initialize(testInstance);
|
|
|
|
|
let resultArray = testTree.treeToArray(testInstance, {});
|
2022-05-27 17:53:02 +02:00
|
|
|
expect(resultArray).toContain(testInstance);
|
2018-07-15 16:04:27 +02:00
|
|
|
});
|
2017-11-20 09:26:13 +01:00
|
|
|
|
2018-08-31 17:24:35 +02:00
|
|
|
tap.test('should add other objects in a hierachy', async () => {
|
|
|
|
|
testTree.appendChild(testInstance, testInstance2);
|
|
|
|
|
testTree.appendChild(testInstance, testInstance3);
|
|
|
|
|
testTree.appendChild(testInstance, testInstance4);
|
2018-11-23 20:33:44 +01:00
|
|
|
});
|
2018-08-31 17:24:35 +02:00
|
|
|
|
2026-03-22 08:44:49 +00:00
|
|
|
tap.test('hasChildren returns correct value', async () => {
|
|
|
|
|
expect(testTree.hasChildren(testInstance)).toBeTrue();
|
|
|
|
|
expect(testTree.hasChildren(testInstance2)).toBeFalse();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('firstChild and lastChild work', async () => {
|
|
|
|
|
const first = testTree.firstChild(testInstance);
|
|
|
|
|
expect(first.hey).toEqual('second');
|
|
|
|
|
const last = testTree.lastChild(testInstance);
|
|
|
|
|
expect(last.hey).toEqual('fourth');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('parent returns correct parent', async () => {
|
|
|
|
|
const parent = testTree.parent(testInstance2);
|
|
|
|
|
expect(parent.hey).toEqual('first');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('nextSibling and previousSibling work', async () => {
|
|
|
|
|
const next = testTree.nextSibling(testInstance2);
|
|
|
|
|
expect(next.hey).toEqual('third');
|
|
|
|
|
const prev = testTree.previousSibling(testInstance3);
|
|
|
|
|
expect(prev.hey).toEqual('second');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('childrenCount returns correct count', async () => {
|
|
|
|
|
expect(testTree.childrenCount(testInstance)).toEqual(3);
|
|
|
|
|
expect(testTree.childrenCount(testInstance2)).toEqual(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('index returns sibling index', async () => {
|
|
|
|
|
expect(testTree.index(testInstance2)).toEqual(0);
|
|
|
|
|
expect(testTree.index(testInstance3)).toEqual(1);
|
|
|
|
|
expect(testTree.index(testInstance4)).toEqual(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('childrenToArray returns children', async () => {
|
|
|
|
|
const children = testTree.childrenToArray(testInstance, {});
|
|
|
|
|
expect(children.length).toEqual(3);
|
|
|
|
|
expect(children[0].hey).toEqual('second');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('insertBefore works', async () => {
|
|
|
|
|
testTree.initialize(testInstance5);
|
|
|
|
|
testTree.insertBefore(testInstance3, testInstance5);
|
|
|
|
|
const idx = testTree.index(testInstance5);
|
|
|
|
|
expect(idx).toEqual(1);
|
|
|
|
|
expect(testTree.nextSibling(testInstance5).hey).toEqual('third');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('insertAfter works', async () => {
|
|
|
|
|
testTree.initialize(testInstance6);
|
|
|
|
|
testTree.insertAfter(testInstance3, testInstance6);
|
|
|
|
|
expect(testTree.previousSibling(testInstance6).hey).toEqual('third');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('remove detaches node', async () => {
|
|
|
|
|
const countBefore = testTree.childrenCount(testInstance);
|
|
|
|
|
testTree.remove(testInstance6);
|
|
|
|
|
expect(testTree.childrenCount(testInstance)).toEqual(countBefore - 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('treeIterator with options works', async () => {
|
|
|
|
|
const items: TestClass[] = [];
|
|
|
|
|
for (const item of testTree.treeIterator(testInstance, {})) {
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
|
|
|
|
expect(items.length).toBeGreaterThan(1);
|
|
|
|
|
expect(items[0].hey).toEqual('first');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('nextSiblingsIterator works (bug was fixed)', async () => {
|
|
|
|
|
const siblings: TestClass[] = [];
|
|
|
|
|
for (const item of testTree.nextSiblingsIterator(testInstance2)) {
|
|
|
|
|
siblings.push(item);
|
|
|
|
|
}
|
|
|
|
|
expect(siblings.length).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('ancestorsIterator works (bug was fixed)', async () => {
|
|
|
|
|
const ancestors: TestClass[] = [];
|
|
|
|
|
for (const item of testTree.ancestorsIterator(testInstance2)) {
|
|
|
|
|
ancestors.push(item);
|
|
|
|
|
}
|
|
|
|
|
expect(ancestors.length).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-23 20:33:44 +01:00
|
|
|
tap.test("should create a JSON object that reflects a tree's hierachy", async () => {
|
2026-03-22 08:44:49 +00:00
|
|
|
const jsonTree = testTree.toJsonWithHierachy(testInstance);
|
|
|
|
|
expect(jsonTree).toHaveProperty('data');
|
|
|
|
|
expect(jsonTree).toHaveProperty('children');
|
|
|
|
|
expect(jsonTree.data.hey).toEqual('first');
|
|
|
|
|
expect(jsonTree.children.length).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('fromJsonWithHierachy rebuilds a tree', async () => {
|
|
|
|
|
const newTree = new lik.Tree<{ name: string }>();
|
|
|
|
|
const jsonRoot = {
|
|
|
|
|
data: { name: 'root' },
|
|
|
|
|
children: [
|
|
|
|
|
{ data: { name: 'child1' }, children: [] },
|
|
|
|
|
{
|
|
|
|
|
data: { name: 'child2' },
|
|
|
|
|
children: [{ data: { name: 'grandchild' }, children: [] }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
const root = newTree.fromJsonWithHierachy(jsonRoot);
|
|
|
|
|
expect(root.name).toEqual('root');
|
|
|
|
|
expect(newTree.hasChildren(root)).toBeTrue();
|
|
|
|
|
expect(newTree.childrenCount(root)).toEqual(2);
|
|
|
|
|
const children = newTree.childrenToArray(root, {});
|
|
|
|
|
expect(children[1].name).toEqual('child2');
|
|
|
|
|
expect(newTree.hasChildren(children[1])).toBeTrue();
|
2018-08-31 17:24:35 +02:00
|
|
|
});
|
|
|
|
|
|
2024-04-18 21:55:33 +02:00
|
|
|
export default tap.start();
|