import { tap, expect } from '@push.rocks/tapbundle'; import * as lik from '../ts/index.js'; class TestClass { constructor(public hey: string) { // nothing here } } let testTree = new lik.Tree(); let testInstance = new TestClass('first'); 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'); tap.test('create a valid tree instance', async () => { testTree = new lik.Tree(); expect(testTree).toBeInstanceOf(lik.Tree); }); tap.test('should insert an object', async () => { testTree.initialize(testInstance); let resultArray = testTree.treeToArray(testInstance, {}); expect(resultArray).toContain(testInstance); }); tap.test('should add other objects in a hierachy', async () => { testTree.appendChild(testInstance, testInstance2); testTree.appendChild(testInstance, testInstance3); testTree.appendChild(testInstance, testInstance4); }); 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); }); tap.test("should create a JSON object that reflects a tree's hierachy", async () => { 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(); }); export default tap.start();