Skip to content

Commit

Permalink
add more tests & refine previous (#8)
Browse files Browse the repository at this point in the history
* add more tests & refine previous

* add more tests for event

* add more tests for event

* slight time fix

* fix(event): make equals working correctly
  • Loading branch information
sckv committed Mar 7, 2022
1 parent 97d0a66 commit 1f9eb9f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 29 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -28,7 +28,8 @@
"scripts": {
"build": "rm -rf ./lib && tsc -p tsconfig.build.json",
"build:dev": "rm -rf ./dev-lib && tsc",
"test": "jest --runInBand --coverage --passWithNoTests",
"test": "jest --runInBand --coverage",
"test:watch": "jest --runInBand --watch",
"lint": "eslint ./src --fix",
"release": "semantic-release -e ./.releaserc.json"
},
Expand Down Expand Up @@ -56,4 +57,4 @@
"lru-native2": "^1.2.5",
"reflect-metadata": "^0.1.13"
}
}
}
69 changes: 69 additions & 0 deletions src/engine/__tests__/event.test.ts
@@ -0,0 +1,69 @@
import { Event } from '../event';

describe('Event suite', () => {
it('create event with timespamp and uuid', () => {
const event = Event.create({ payload: 'test' });

expect(event.timestamp).toBeDefined();
expect(event.uuid).toBeDefined();
});

it('event equals is working', () => {
const event = Event.create({ payload: 'test' });
const anotherEvent = Event.create({ payload: 'test' });

expect(event.equals(event)).toBeTruthy();
expect(event.equals(anotherEvent)).toBeFalsy();
});

it('event payloads equals is working', () => {
const event = Event.create({ payload: 'test' });
const anotherEvent = Event.create({ payload: 'test' });
const anotherDifferentEvent = Event.create({ payload: 'test2' });

expect(event.payloadEquals(anotherEvent)).toBeTruthy();
expect(event.payloadEquals(anotherDifferentEvent)).toBeFalsy();
});

it('event isBefore and isAfter works', async () => {
const event = Event.create({ payload: 'first' });
await new Promise<void>((res) => {
setTimeout(res, 5);
});

const after = Event.create({ payload: 'second' });
await new Promise<void>((res) => {
setTimeout(res, 5);
});

const lastOne = Event.create({ payload: 'second' });

expect(event.isBefore(after)).toBeTruthy();
expect(after.isBefore(lastOne)).toBeTruthy();
expect(lastOne.isAfter(after)).toBeTruthy();
});

it('event to json', () => {
const event = Event.create({ payload: 'test' });

expect(event.toJSON()).toEqual({ payload: 'test' });
});

it('get unique stamp', () => {
const event = Event.create({ payload: 'test' });

expect(event.getUniqueStamp()).toMatch(/^\d+-\S+$/);
expect(event.getUniqueStamp('transpOrt')).toMatch(/^\d+-\S+-transpOrt$/);
});

it('add orphan transports to the event', () => {
const event = Event.create({ payload: 'test' });

event.addOrphanTransport('transport1');

expect([...event.orphanTransports!]).toEqual(['transport1']);

event.addOrphanTransport('transport2');
expect([...event.orphanTransports!]).toEqual(['transport1', 'transport2']);
});
});
31 changes: 7 additions & 24 deletions src/engine/__tests__/typed-bus.test.ts
Expand Up @@ -208,13 +208,11 @@ describe('Typed-Bus suite', () => {

new Test();

TypedBus.publish({ amount: 1, currency: 'EUR' });
await TypedBus.publish({ amount: 1, currency: 'EUR' });

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });

await new Promise((res) => setImmediate(res));

expect(console.error).toHaveBeenCalledWith(
'Error in consumer named "method"',
expect.any(Error),
Expand All @@ -238,13 +236,11 @@ describe('Typed-Bus suite', () => {

new Test();

TypedBus.publish({ amount: 1, currency: 'EUR' });
await TypedBus.publish({ amount: 1, currency: 'EUR' });

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });

await new Promise((res) => setImmediate(res));

expect(console.error).toHaveBeenCalledWith(
'Error in consumer named "asyncMethod"',
expect.any(Error),
Expand Down Expand Up @@ -284,8 +280,6 @@ describe('Typed-Bus suite', () => {
TypedBus.publish({ amount: 1, currency: 'EUR' }, { onlySendTo: ['internal'] });
TypedBus.publish({ amount: 2, currency: 'USD' }, { onlySendTo: ['new-transport'] });

await new Promise((res) => setImmediate(res));

expect(spyOld).toHaveBeenCalledTimes(1);
expect(spyOld).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });

Expand All @@ -300,6 +294,7 @@ describe('Typed-Bus suite', () => {
class NewTransport extends Transport {
name = 'new-transport';
waitForReady = true;
ready = false;
}

TypedBus.addTransport(new NewTransport());
Expand All @@ -322,10 +317,8 @@ describe('Typed-Bus suite', () => {

new Test();

TypedBus.publish({ amount: 1, currency: 'EUR' }, { onlySendTo: ['internal'] });
TypedBus.publish({ amount: 3, currency: 'JPY' }, { onlySendTo: ['new-transport'] });

await new Promise((res) => setTimeout(res, 50));
await TypedBus.publish({ amount: 1, currency: 'EUR' }, { onlySendTo: ['internal'] });
await TypedBus.publish({ amount: 3, currency: 'JPY' }, { onlySendTo: ['new-transport'] });

expect(spyOld).toHaveBeenCalledTimes(1);
expect(spyOld).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });
Expand All @@ -346,13 +339,11 @@ describe('Typed-Bus suite', () => {

TypedBus.publish({ amount: 1, currency: 'EUR' });

await new Promise((res) => setTimeout(res));

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });
});

it('adds new consumer with direct API and remove it by id', async () => {
it('adds new consumer with direct API and remove it by consumer id', async () => {
const spy = jest.fn();
const spySecond = jest.fn();

Expand All @@ -372,8 +363,6 @@ describe('Typed-Bus suite', () => {

TypedBus.publish({ amount: 1, currency: 'EUR' });

await new Promise((res) => setTimeout(res));

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });

Expand All @@ -400,8 +389,6 @@ describe('Typed-Bus suite', () => {

TypedBus.publish({ amount: 1, currency: 'EUR' });

await new Promise((res) => setTimeout(res));

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });

Expand Down Expand Up @@ -434,8 +421,6 @@ describe('Typed-Bus suite', () => {

TypedBus.publish({ amount: 1, currency: 'EUR' });

await new Promise((res) => setTimeout(res));

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });

Expand Down Expand Up @@ -465,9 +450,7 @@ describe('Typed-Bus suite', () => {
TypedBus.addConsumer(contract, consumer);
TypedBus.addConsumer(contract, consumer2, { listenTo: ['internal', 'new-transport'] });

TypedBus.publish({ amount: 1, currency: 'EUR' });

await new Promise((res) => setTimeout(res));
await TypedBus.publish({ amount: 1, currency: 'EUR' });

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({ amount: 1, currency: 'EUR' });
Expand Down
3 changes: 2 additions & 1 deletion src/engine/event.ts
Expand Up @@ -40,7 +40,8 @@ export class Event<T = any> {
}

equals(to: Event) {
if (this.uuid !== to.uuid && this.timestamp !== to.timestamp) return false;
if (this.uuid !== to.uuid || this.timestamp !== to.timestamp) return false;
return true;
}

payloadEquals(to: Event) {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/transport.ts
Expand Up @@ -119,7 +119,7 @@ export abstract class Transport {
await new Promise<void>((res) => {
setTimeout(() => {
if (this.ready) res();
}, 1000);
}, 50);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.build.json
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./lib"
},
"exclude": ["**/__tests__/**"]
"exclude": ["**/__tests__/**", "src/example/**"]
}

0 comments on commit 1f9eb9f

Please sign in to comment.