210 lines
6.6 KiB
TypeScript
210 lines
6.6 KiB
TypeScript
// import test framework
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
|
|
|
// import the module
|
|
import * as lik from '../ts/index.js';
|
|
|
|
// Objectmap
|
|
interface ITestObject {
|
|
propOne: string;
|
|
propTwo: string;
|
|
}
|
|
let testObjectmap: lik.ObjectMap<ITestObject>;
|
|
let testObject1: ITestObject = {
|
|
propOne: 'hello',
|
|
propTwo: 'hello2',
|
|
};
|
|
let testObject2: ITestObject = {
|
|
propOne: 'hello',
|
|
propTwo: 'hello2',
|
|
};
|
|
|
|
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
|
|
testObjectmap = new lik.ObjectMap<ITestObject>();
|
|
expect(testObjectmap).toBeInstanceOf(lik.ObjectMap);
|
|
expect(testObjectmap.length).toEqual(0);
|
|
expect(testObjectmap.isEmpty()).toBeTrue();
|
|
});
|
|
|
|
tap.test('lik.Objectmap.add() -> should correctly add an object to Objectmap', async () => {
|
|
testObjectmap.add(testObject1);
|
|
// tslint:disable-next-line:no-unused-expression
|
|
expect(testObjectmap.checkForObject(testObject1)).toBeTrue();
|
|
// tslint:disable-next-line:no-unused-expression
|
|
expect(testObjectmap.checkForObject(testObject2)).toBeFalse();
|
|
expect(testObjectmap.length).toEqual(1);
|
|
});
|
|
|
|
tap.test('add same object twice returns same key', async () => {
|
|
const key1 = testObjectmap.add(testObject1);
|
|
const key2 = testObjectmap.add(testObject1);
|
|
expect(key1).toEqual(key2);
|
|
expect(testObjectmap.length).toEqual(1);
|
|
});
|
|
|
|
tap.test('lik.Objectmap.remove() -> should correctly remove an object to Objectmap', async () => {
|
|
testObjectmap.add(testObject2);
|
|
testObjectmap.remove(testObject1);
|
|
// tslint:disable-next-line:no-unused-expression
|
|
expect(testObjectmap.checkForObject(testObject1)).toBeFalse();
|
|
// tslint:disable-next-line:no-unused-expression
|
|
expect(testObjectmap.checkForObject(testObject2)).toBeTrue();
|
|
});
|
|
|
|
tap.test('Objectmap.forEach -> should correctly run a function forEach map object', async () => {
|
|
testObjectmap.forEach((itemArg) => {
|
|
expect(itemArg).toHaveProperty('propOne');
|
|
});
|
|
});
|
|
|
|
tap.test('lik.Objectmap.find() -> should correctly find an object', async () => {
|
|
let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' };
|
|
testObjectmap.add(myObject);
|
|
let referenceObject = await testObjectmap.find(async (itemArg) => {
|
|
return itemArg.propOne === 'helloThere';
|
|
});
|
|
// tslint:disable-next-line:no-unused-expression
|
|
expect(myObject === referenceObject).toBeTrue();
|
|
});
|
|
|
|
tap.test('lik.Objectmap.getArray() -> should return a cloned array', async () => {
|
|
let myObject = { propOne: 'test1', propTwo: 'wow, how awesome' };
|
|
testObjectmap.add(myObject);
|
|
let clonedArray = testObjectmap.getArray();
|
|
expect(clonedArray[clonedArray.length - 1]).toEqual(myObject);
|
|
});
|
|
|
|
tap.test('should get one object and then remove it', async () => {
|
|
let originalLength = testObjectmap.getArray().length;
|
|
let oneObject = testObjectmap.getOneAndRemove();
|
|
// tslint:disable-next-line:no-unused-expression
|
|
expect(oneObject).not.toBeNull();
|
|
expect(testObjectmap.getArray().length).toEqual(originalLength - 1);
|
|
expect(testObjectmap.getArray()).not.toContain(oneObject);
|
|
});
|
|
|
|
tap.test('addMappedUnique and getMappedUnique work', async () => {
|
|
const map = new lik.ObjectMap<string>();
|
|
map.addMappedUnique('myKey', 'myValue');
|
|
expect(map.getMappedUnique('myKey')).toEqual('myValue');
|
|
});
|
|
|
|
tap.test('removeMappedUnique works', async () => {
|
|
const map = new lik.ObjectMap<string>();
|
|
map.addMappedUnique('k1', 'v1');
|
|
const removed = map.removeMappedUnique('k1');
|
|
expect(removed).toEqual('v1');
|
|
expect(map.getMappedUnique('k1')).toBeUndefined();
|
|
});
|
|
|
|
tap.test('addArray adds multiple objects', async () => {
|
|
const map = new lik.ObjectMap<{ id: number }>();
|
|
const items = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
map.addArray(items);
|
|
expect(map.length).toEqual(3);
|
|
for (const item of items) {
|
|
expect(map.checkForObject(item)).toBeTrue();
|
|
}
|
|
});
|
|
|
|
tap.test('findSync works', async () => {
|
|
const map = new lik.ObjectMap<{ name: string }>();
|
|
const obj = { name: 'target' };
|
|
map.add({ name: 'other' });
|
|
map.add(obj);
|
|
const found = map.findSync((item) => item.name === 'target');
|
|
expect(found).toEqual(obj);
|
|
});
|
|
|
|
tap.test('findOneAndRemove works', async () => {
|
|
const map = new lik.ObjectMap<{ val: number }>();
|
|
map.add({ val: 1 });
|
|
map.add({ val: 2 });
|
|
const removed = await map.findOneAndRemove(async (item) => item.val === 1);
|
|
expect(removed.val).toEqual(1);
|
|
expect(map.length).toEqual(1);
|
|
});
|
|
|
|
tap.test('isEmpty returns correct state', async () => {
|
|
const map = new lik.ObjectMap<string>();
|
|
expect(map.isEmpty()).toBeTrue();
|
|
map.add('hello');
|
|
expect(map.isEmpty()).toBeFalse();
|
|
});
|
|
|
|
tap.test('wipe clears all entries', async () => {
|
|
const map = new lik.ObjectMap<number>();
|
|
map.add(1);
|
|
map.add(2);
|
|
map.add(3);
|
|
map.wipe();
|
|
expect(map.isEmpty()).toBeTrue();
|
|
expect(map.length).toEqual(0);
|
|
});
|
|
|
|
tap.test('concat creates merged map', async () => {
|
|
const map1 = new lik.ObjectMap<string>();
|
|
map1.add('a');
|
|
const map2 = new lik.ObjectMap<string>();
|
|
map2.add('b');
|
|
const merged = map1.concat(map2);
|
|
expect(merged.length).toEqual(2);
|
|
});
|
|
|
|
tap.test('map/filter/reduce work', async () => {
|
|
const map = new lik.ObjectMap<{ val: number }>();
|
|
map.add({ val: 1 });
|
|
map.add({ val: 2 });
|
|
map.add({ val: 3 });
|
|
|
|
const mapped = map.map((item) => item.val * 2);
|
|
expect(mapped.length).toEqual(3);
|
|
expect(mapped).toContain(2);
|
|
expect(mapped).toContain(4);
|
|
expect(mapped).toContain(6);
|
|
|
|
const filtered = map.filter((item) => item.val > 1);
|
|
expect(filtered.length).toEqual(2);
|
|
|
|
const sum = map.reduce((acc, item) => acc + item.val, 0);
|
|
expect(sum).toEqual(6);
|
|
});
|
|
|
|
tap.test('Symbol.iterator enables for...of', async () => {
|
|
const map = new lik.ObjectMap<number>();
|
|
map.add(10);
|
|
map.add(20);
|
|
const collected: number[] = [];
|
|
for (const item of map) {
|
|
collected.push(item);
|
|
}
|
|
expect(collected.length).toEqual(2);
|
|
expect(collected).toContain(10);
|
|
expect(collected).toContain(20);
|
|
});
|
|
|
|
tap.test('eventSubject emits add and remove events', async () => {
|
|
const map = new lik.ObjectMap<string>();
|
|
const events: Array<{ operation: string; payload: string }> = [];
|
|
map.eventSubject.subscribe((event) => {
|
|
events.push(event);
|
|
});
|
|
map.add('x');
|
|
map.remove('x');
|
|
expect(events.length).toEqual(2);
|
|
expect(events[0].operation).toEqual('add');
|
|
expect(events[1].operation).toEqual('remove');
|
|
});
|
|
|
|
tap.test('destroy wipes and completes eventSubject', async () => {
|
|
const map = new lik.ObjectMap<number>();
|
|
map.add(1);
|
|
let completed = false;
|
|
map.eventSubject.subscribe({ complete: () => { completed = true; } });
|
|
map.destroy();
|
|
expect(map.isEmpty()).toBeTrue();
|
|
expect(completed).toBeTrue();
|
|
});
|
|
|
|
export default tap.start();
|