|
1 | 1 | import { Injectable, Scope } from '@nestjs/common'; |
2 | 2 | import { Test } from '@nestjs/testing'; |
3 | | -import { DefaultEventEmitter, Emitter, EventBus, EventBusModule, From, On } from './index'; |
| 3 | +import { DefaultEventEmitter, Emitter, EventBus, EventBusModule, EventBusModuleOptions, From, On } from './index'; |
4 | 4 |
|
5 | 5 | @Emitter('mocked') |
6 | 6 | class MockEmitter extends DefaultEventEmitter {} |
@@ -59,12 +59,17 @@ class MockOn { |
59 | 59 | onTestFromMocked(payload: any) { |
60 | 60 | this.calls('mocked', payload); |
61 | 61 | } |
| 62 | + |
| 63 | + @On('test.*') |
| 64 | + onTestWildcard(payload: any) { |
| 65 | + this.calls('test.*', payload); |
| 66 | + } |
62 | 67 | } |
63 | 68 |
|
64 | 69 | describe('Module Tests', () => { |
65 | | - async function setup() { |
| 70 | + async function setup(config?: EventBusModuleOptions) { |
66 | 71 | const module = Test.createTestingModule({ |
67 | | - imports: [EventBusModule.forRoot()], |
| 72 | + imports: [EventBusModule.forRoot(config)], |
68 | 73 | providers: [MockEmitter, MockOn, MockOnRequest] |
69 | 74 | }); |
70 | 75 | return module.compile().then(module => module.init()); |
@@ -126,4 +131,19 @@ describe('Module Tests', () => { |
126 | 131 | const actual = module.get(EventBus).emitAsync('rethrow', { _: 'error' }); |
127 | 132 | await expect(actual).rejects.toThrow('rethrow'); |
128 | 133 | }); |
| 134 | + |
| 135 | + it('should work with evementemitter2 options (wildcard)', async () => { |
| 136 | + const module = await setup({ |
| 137 | + wildcard: true, |
| 138 | + delimiter: '.' |
| 139 | + }); |
| 140 | + |
| 141 | + await module.get(EventBus).emitAsync('test.one', { _: '1' }); |
| 142 | + await module.get(EventBus).emitAsync('test.two', { _: '2' }); |
| 143 | + |
| 144 | + const actual = module.get(MockOn).calls; |
| 145 | + expect(actual).toBeCalledTimes(2); |
| 146 | + expect(actual).toBeCalledWith('test.*', { _: '1' }); |
| 147 | + expect(actual).toBeCalledWith('test.*', { _: '2' }); |
| 148 | + }); |
129 | 149 | }); |
0 commit comments