lik/test/test.objectmap.ts

79 lines
2.7 KiB
TypeScript
Raw Normal View History

2017-07-05 12:29:08 +00:00
// import test framework
2018-07-15 14:04:27 +00:00
import { expect, tap } from '@pushrocks/tapbundle';
import * as events from 'events';
2018-08-31 15:24:35 +00:00
import * as smartpromise from '@pushrocks/smartpromise';
2017-07-05 12:29:08 +00:00
// import the module
2018-07-15 14:04:27 +00:00
import * as lik from '../ts/index';
2017-07-05 12:29:08 +00:00
// Objectmap
interface ITestObject {
2018-07-15 14:04:27 +00:00
propOne: string;
propTwo: string;
2017-07-05 12:29:08 +00:00
}
let testObjectmap: lik.ObjectMap<ITestObject>;
2017-07-05 12:29:08 +00:00
let testObject1: ITestObject = {
propOne: 'hello',
2020-07-12 00:44:50 +00:00
propTwo: 'hello2',
2018-07-15 14:04:27 +00:00
};
2017-07-05 12:29:08 +00:00
let testObject2: ITestObject = {
propOne: 'hello',
2020-07-12 00:44:50 +00:00
propTwo: 'hello2',
2018-07-15 14:04:27 +00:00
};
2017-07-05 12:29:08 +00:00
tap.test('new lik.Objectmap() -> should correctly instantiate an Objectmap', async () => {
testObjectmap = new lik.ObjectMap<ITestObject>();
expect(testObjectmap).be.instanceof(lik.ObjectMap);
2018-07-15 14:04:27 +00:00
});
2017-07-05 12:29:08 +00:00
tap.test('lik.Objectmap.add() -> should correctly add an object to Objectmap', async () => {
2018-07-15 14:04:27 +00:00
testObjectmap.add(testObject1);
2017-07-05 12:29:08 +00:00
// tslint:disable-next-line:no-unused-expression
2018-07-15 14:04:27 +00:00
expect(testObjectmap.checkForObject(testObject1)).be.true;
2017-07-05 12:29:08 +00:00
// tslint:disable-next-line:no-unused-expression
2018-07-15 14:04:27 +00:00
expect(testObjectmap.checkForObject(testObject2)).be.false;
});
2017-07-05 12:29:08 +00:00
tap.test('lik.Objectmap.remove() -> should correctly remove an object to Objectmap', async () => {
2018-07-15 14:04:27 +00:00
testObjectmap.add(testObject2);
testObjectmap.remove(testObject1);
2017-07-05 12:29:08 +00:00
// tslint:disable-next-line:no-unused-expression
2018-07-15 14:04:27 +00:00
expect(testObjectmap.checkForObject(testObject1)).be.false;
2017-07-05 12:29:08 +00:00
// tslint:disable-next-line:no-unused-expression
2018-07-15 14:04:27 +00:00
expect(testObjectmap.checkForObject(testObject2)).be.true;
});
2017-07-05 12:29:08 +00:00
tap.test('Objectmap.forEach -> should correctly run a function forEach map object', async () => {
2020-07-12 00:44:50 +00:00
testObjectmap.forEach((itemArg) => {
2018-07-15 14:04:27 +00:00
expect(itemArg).to.have.property('propOne');
});
});
2017-07-05 12:29:08 +00:00
tap.test('lik.Objectmap.find() -> should correctly find an object', async () => {
2018-07-15 14:04:27 +00:00
let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' };
testObjectmap.add(myObject);
2020-07-12 00:44:50 +00:00
let referenceObject = testObjectmap.find((itemArg) => {
2018-07-15 14:04:27 +00:00
return itemArg.propOne === 'helloThere';
});
2017-07-05 12:29:08 +00:00
// tslint:disable-next-line:no-unused-expression
2018-07-15 14:04:27 +00:00
expect(myObject === referenceObject).be.true;
});
2017-07-05 12:29:08 +00:00
tap.test('lik.Objectmap.getArray() -> should return a cloned array', async () => {
2018-07-15 14:04:27 +00:00
let myObject = { propOne: 'test1', propTwo: 'wow, how awesome' };
testObjectmap.add(myObject);
let clonedArray = testObjectmap.getArray();
expect(clonedArray[clonedArray.length - 1]).to.eql(myObject);
});
2017-07-05 12:29:08 +00:00
tap.test('should get one object and then remove it', async () => {
2018-07-15 14:04:27 +00:00
let originalLength = testObjectmap.getArray().length;
let oneObject = testObjectmap.getOneAndRemove();
2017-07-05 12:29:08 +00:00
// tslint:disable-next-line:no-unused-expression
2018-07-15 14:04:27 +00:00
expect(oneObject).not.be.null;
expect(testObjectmap.getArray().length).equal(originalLength - 1);
expect(testObjectmap.getArray()).to.not.contain(oneObject);
});
2017-07-05 12:29:08 +00:00
2018-07-15 14:04:27 +00:00
tap.start();