Skip to content

Commit

Permalink
test(e2e): refactor tests to use constant event payloads instead of m…
Browse files Browse the repository at this point in the history
…anual declaration
  • Loading branch information
thiagomini committed Jul 8, 2022
1 parent afeeb0c commit 929a3ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
28 changes: 13 additions & 15 deletions tests/e2e/module-e2e.spec.ts
Expand Up @@ -2,6 +2,11 @@ import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { EventEmitter2 } from 'eventemitter2';
import { AppModule } from '../src/app.module';
import {
TEST_EVENT_MULTIPLE_PAYLOAD,
TEST_EVENT_PAYLOAD,
TEST_EVENT_STRING_PAYLOAD,
} from '../src/constants';
import { EventsControllerConsumer } from '../src/events-controller.consumer';
import { EventsProviderPrependConsumer } from '../src/events-provider-prepend.consumer';
import { EventsProviderConsumer } from '../src/events-provider.consumer';
Expand All @@ -23,14 +28,14 @@ describe('EventEmitterModule - e2e', () => {
const eventsConsumerRef = app.get(EventsProviderConsumer);
await app.init();

expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' });
expect(eventsConsumerRef.eventPayload).toEqual(TEST_EVENT_PAYLOAD);
});

it(`should emit a "test-event" event to controllers`, async () => {
const eventsConsumerRef = app.get(EventsControllerConsumer);
await app.init();

expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' });
expect(eventsConsumerRef.eventPayload).toEqual(TEST_EVENT_PAYLOAD);
});

it('should be able to specify a consumer be prepended via OnEvent decorator options', async () => {
Expand All @@ -41,7 +46,7 @@ describe('EventEmitterModule - e2e', () => {
);
await app.init();

expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' });
expect(eventsConsumerRef.eventPayload).toEqual(TEST_EVENT_PAYLOAD);
expect(prependListenerSpy).toHaveBeenCalled();
});

Expand All @@ -65,32 +70,25 @@ describe('EventEmitterModule - e2e', () => {
it('should be able to emit a request-scoped event with a single payload', async () => {
await app.init();

expect(EventsProviderRequestScopedConsumer.injectedEventPayload).toEqual({
test: 'event',
});
expect(EventsProviderRequestScopedConsumer.injectedEventPayload).toEqual(
TEST_EVENT_PAYLOAD,
);
});

it('should be able to emit a request-scoped event with a string payload', async () => {
await app.init();

expect(
EventsProviderRequestScopedConsumer.injectedEventStringPayload,
).toEqual('some-string');
).toEqual(TEST_EVENT_STRING_PAYLOAD);
});

it('should be able to emit a request-scoped event with multiple payloads', async () => {
await app.init();

expect(
EventsProviderRequestScopedConsumer.injectedEventMultiPayload,
).toEqual([
{
test: 'event',
},
{
test2: 'event2',
},
]);
).toEqual(TEST_EVENT_MULTIPLE_PAYLOAD);
});

afterEach(async () => {
Expand Down
12 changes: 12 additions & 0 deletions tests/src/constants.ts
@@ -0,0 +1,12 @@
export const TEST_EVENT_PAYLOAD = {
test: 'event',
};

export const TEST_EVENT_MULTIPLE_PAYLOAD = [
TEST_EVENT_PAYLOAD,
{
test2: 'event2',
},
];

export const TEST_EVENT_STRING_PAYLOAD = 'some-string';
15 changes: 8 additions & 7 deletions tests/src/events.producer.ts
@@ -1,17 +1,18 @@
import { Injectable, OnApplicationBootstrap } from '@nestjs/common';
import { EventEmitter2 } from 'eventemitter2';
import {
TEST_EVENT_MULTIPLE_PAYLOAD,
TEST_EVENT_PAYLOAD,
TEST_EVENT_STRING_PAYLOAD,
} from './constants';

@Injectable()
export class EventsProducer implements OnApplicationBootstrap {
constructor(private readonly eventEmitter: EventEmitter2) {}

onApplicationBootstrap() {
this.eventEmitter.emit('test.event', { test: 'event' });
this.eventEmitter.emit(
'multiple.event',
{ test: 'event' },
{ test2: 'event2' },
);
this.eventEmitter.emit('string.event', 'some-string');
this.eventEmitter.emit('test.event', TEST_EVENT_PAYLOAD);
this.eventEmitter.emit('multiple.event', TEST_EVENT_MULTIPLE_PAYLOAD);
this.eventEmitter.emit('string.event', TEST_EVENT_STRING_PAYLOAD);
}
}

0 comments on commit 929a3ea

Please sign in to comment.