Skip to content

Commit

Permalink
test(@nestjs/event-emitter): add testing to non array metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Maligosus committed Mar 6, 2023
1 parent a28ae33 commit 04cbdc7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/e2e/module-e2e.spec.ts
Expand Up @@ -7,6 +7,8 @@ import {
TEST_EVENT_PAYLOAD,
TEST_EVENT_STRING_PAYLOAD,
} from '../src/constants';
import { CUSTOM_DECORATOR_EVENT } from '../src/custom-decorator-test.constants';
import { CustomEventDecoratorConsumer } from '../src/custom-decorator-test.consumer';
import { EventsControllerConsumer } from '../src/events-controller.consumer';
import { EventsProviderAliasedConsumer } from '../src/events-provider-aliased.consumer';
import { EventsProviderPrependConsumer } from '../src/events-provider-prepend.consumer';
Expand Down Expand Up @@ -108,6 +110,18 @@ describe('EventEmitterModule - e2e', () => {
).toEqual(TEST_EVENT_MULTIPLE_PAYLOAD);
});

it('should work with non array metadata', async () => {
await app.init();

const emitter = app.get(EventEmitter2);
const customConsumer = app.get(CustomEventDecoratorConsumer);

// callback called synchronysly
emitter.emit(CUSTOM_DECORATOR_EVENT);

expect(customConsumer.isEmitted).toBeTruthy();
});

afterEach(async () => {
await app.close();
});
Expand Down
2 changes: 2 additions & 0 deletions tests/src/app.module.ts
@@ -1,5 +1,6 @@
import { Module } from '@nestjs/common';
import { EventEmitterModule } from '../../lib';
import { CustomEventDecoratorConsumer } from './custom-decorator-test.consumer';
import { EventsControllerConsumer } from './events-controller.consumer';
import { EventsProviderAliasedConsumer } from './events-provider-aliased.consumer';
import { EventsProviderPrependConsumer } from './events-provider-prepend.consumer';
Expand All @@ -26,6 +27,7 @@ import { TestProvider } from './test-provider';
provide: 'AnAliasedConsumer',
useExisting: EventsProviderAliasedConsumer,
},
CustomEventDecoratorConsumer,
],
})
export class AppModule {}
1 change: 1 addition & 0 deletions tests/src/custom-decorator-test.constants.ts
@@ -0,0 +1 @@
export const CUSTOM_DECORATOR_EVENT = 'custom-decorator-event';
13 changes: 13 additions & 0 deletions tests/src/custom-decorator-test.consumer.ts
@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import { CUSTOM_DECORATOR_EVENT } from './custom-decorator-test.constants';
import { CustomEvent } from './custom-event.decorator';

@Injectable()
export class CustomEventDecoratorConsumer {
public isEmitted = false;

@CustomEvent(CUSTOM_DECORATOR_EVENT)
handleCustomEvent() {
this.isEmitted = true;
}
}
12 changes: 12 additions & 0 deletions tests/src/custom-event.decorator.ts
@@ -0,0 +1,12 @@
import { SetMetadata } from '@nestjs/common';

import { OnEventMetadata } from '../../lib';
import { EVENT_LISTENER_METADATA } from '../../lib/constants';

import type { OnEventOptions } from '../../lib/interfaces';

export const CustomEvent = (event: string, options?: OnEventOptions) =>
SetMetadata(EVENT_LISTENER_METADATA, {
event,
options,
} as OnEventMetadata);

0 comments on commit 04cbdc7

Please sign in to comment.