lik/test/test.objectmap.both.ts

77 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-07-12 00:44:50 +00:00
// import test framework
import { expect, tap } from '@pushrocks/tapbundle';
// import the module
2022-05-27 15:53:02 +00:00
import * as lik from '../ts/index.js';
2020-07-12 00:44:50 +00:00
// 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>();
2022-01-24 04:22:49 +00:00
expect(testObjectmap).toBeInstanceOf(lik.ObjectMap);
2020-07-12 00:44:50 +00:00
});
tap.test('lik.Objectmap.add() -> should correctly add an object to Objectmap', async () => {
testObjectmap.add(testObject1);
// tslint:disable-next-line:no-unused-expression
2022-01-24 04:22:49 +00:00
expect(testObjectmap.checkForObject(testObject1)).toBeTrue();
2020-07-12 00:44:50 +00:00
// tslint:disable-next-line:no-unused-expression
2022-01-24 04:22:49 +00:00
expect(testObjectmap.checkForObject(testObject2)).toBeFalse();
2020-07-12 00:44:50 +00:00
});
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
2022-01-24 04:22:49 +00:00
expect(testObjectmap.checkForObject(testObject1)).toBeFalse();
2020-07-12 00:44:50 +00:00
// tslint:disable-next-line:no-unused-expression
2022-01-24 04:22:49 +00:00
expect(testObjectmap.checkForObject(testObject2)).toBeTrue();
2020-07-12 00:44:50 +00:00
});
tap.test('Objectmap.forEach -> should correctly run a function forEach map object', async () => {
testObjectmap.forEach((itemArg) => {
2022-05-27 15:53:02 +00:00
expect(itemArg).toHaveProperty('propOne');
2020-07-12 00:44:50 +00:00
});
});
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) => {
2020-07-12 00:44:50 +00:00
return itemArg.propOne === 'helloThere';
});
// tslint:disable-next-line:no-unused-expression
2022-01-24 04:22:49 +00:00
expect(myObject === referenceObject).toBeTrue();
2020-07-12 00:44:50 +00:00
});
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();
2022-01-24 04:22:49 +00:00
expect(clonedArray[clonedArray.length - 1]).toEqual(myObject);
2020-07-12 00:44:50 +00:00
});
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
2022-05-27 15:53:02 +00:00
expect(oneObject).not.toBeNull();
2022-01-24 04:22:49 +00:00
expect(testObjectmap.getArray().length).toEqual(originalLength - 1);
2022-05-27 15:53:02 +00:00
expect(testObjectmap.getArray()).not.toContain(oneObject);
2020-07-12 00:44:50 +00:00
});
tap.start();