Skip to content

Commit

Permalink
test(): refactor request-scoped event payload
Browse files Browse the repository at this point in the history
refactor request-scoped event payloads to increase the readability and reduce the burden to add other request payload types in the future
  • Loading branch information
thiagomini committed Jul 8, 2022
1 parent 929a3ea commit b434bd6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
10 changes: 5 additions & 5 deletions tests/e2e/module-e2e.spec.ts
Expand Up @@ -70,24 +70,24 @@ 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_PAYLOAD,
);
expect(
EventsProviderRequestScopedConsumer.injectedEventPayload.objectValue,
).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,
EventsProviderRequestScopedConsumer.injectedEventPayload.stringValue,
).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,
EventsProviderRequestScopedConsumer.injectedEventPayload.arrayValue,
).toEqual(TEST_EVENT_MULTIPLE_PAYLOAD);
});

Expand Down
17 changes: 5 additions & 12 deletions tests/src/events-provider.request-scoped.consumer.ts
@@ -1,24 +1,17 @@
import { Inject, Injectable } from '@nestjs/common';
import { OnEvent } from '../../lib';
import { EVENT_PAYLOAD } from '../../lib';
import { RequestScopedEventPayload } from './request-scoped-event-payload';

@Injectable()
export class EventsProviderRequestScopedConsumer {
constructor(@Inject(EVENT_PAYLOAD) public eventRef: any) {
if (Array.isArray(this.eventRef)) {
EventsProviderRequestScopedConsumer.injectedEventMultiPayload =
this.eventRef;
} else if (typeof this.eventRef === 'string') {
EventsProviderRequestScopedConsumer.injectedEventStringPayload =
this.eventRef;
} else {
EventsProviderRequestScopedConsumer.injectedEventPayload = this.eventRef;
}
EventsProviderRequestScopedConsumer.injectedEventPayload.setPayload(
this.eventRef,
);
}

public static injectedEventPayload = {};
public static injectedEventMultiPayload: any[] = [];
public static injectedEventStringPayload = '';
public static injectedEventPayload = new RequestScopedEventPayload();

@OnEvent('test.*')
onTestEvent() {}
Expand Down
25 changes: 25 additions & 0 deletions tests/src/request-scoped-event-payload.ts
@@ -0,0 +1,25 @@
/**
* Class used to test injected payloads on the RequestScoped listener.
* Each value stored in the instance represents a different type of payload.
*/
export class RequestScopedEventPayload {
public objectValue: Record<string, any>;
public arrayValue: any[];
public stringValue: string;

constructor() {
this.objectValue = {};
this.arrayValue = [];
this.stringValue = '';
}

public setPayload(value: any) {
if (Array.isArray(value)) {
this.arrayValue = value;
} else if (typeof value === 'string') {
this.stringValue = value;
} else {
this.objectValue = value;
}
}
}

0 comments on commit b434bd6

Please sign in to comment.