|
1 | | -import {expect, sinon} from '@loopback/testlab'; |
| 1 | +import {sinon} from '@loopback/testlab'; |
2 | 2 | import {ProducerApp} from './fixtures/producers-app'; |
3 | 3 | import {TestProducerService} from './fixtures/services/test-producer.service'; |
4 | 4 | import {setupProducerApplication} from './helpers/app-builder'; |
5 | 5 | import {QueueStub} from './stubs/bullmq-queue.stub'; |
6 | 6 | import {Events} from '../test-stream'; |
7 | | -import {DEFAULT_SOURCE} from '../../../constants'; |
8 | 7 |
|
9 | | -[ |
| 8 | +interface TestConfig { |
| 9 | + type: string; |
| 10 | + queue: QueueStub; |
| 11 | + condition: () => boolean; |
| 12 | +} |
| 13 | + |
| 14 | +const testConfigs: TestConfig[] = [ |
10 | 15 | { |
11 | 16 | type: 'Stubbed queue', |
12 | 17 | queue: new QueueStub(), |
13 | 18 | condition: () => true, |
14 | 19 | }, |
15 | | -].forEach(({type, queue, condition}) => { |
| 20 | +]; |
| 21 | + |
| 22 | +for (const {type, queue, condition} of testConfigs) { |
| 23 | + runBullMqConnectorTests(type, queue, condition); |
| 24 | +} |
| 25 | + |
| 26 | +function runBullMqConnectorTests( |
| 27 | + type: string, |
| 28 | + queue: QueueStub, |
| 29 | + condition: () => boolean, |
| 30 | +): void { |
16 | 31 | describe(`BullMq Connector: With ${type}`, () => { |
17 | 32 | let producerApp: ProducerApp | undefined; |
18 | 33 | let producerService: TestProducerService; |
19 | 34 | let listenerStub: sinon.SinonStub; |
| 35 | + |
20 | 36 | before(async function () { |
21 | | - if (!condition()) { |
22 | | - // eslint-disable-next-line @typescript-eslint/no-invalid-this |
23 | | - this.skip(); |
24 | | - } |
| 37 | + if (!condition()) this.skip(); |
25 | 38 | producerApp = await setupProducerApplication(queue); |
26 | 39 | producerService = producerApp.getSync<TestProducerService>( |
27 | 40 | `services.TestProducerService`, |
28 | 41 | ); |
29 | 42 | listenerStub = sinon.stub().resolves(); |
30 | | - if (queue) { |
31 | | - queue.register(listenerStub); |
32 | | - } |
| 43 | + queue.register(listenerStub); |
33 | 44 | }); |
34 | | - beforeEach(() => { |
35 | | - listenerStub.reset(); |
36 | | - }); |
37 | | - after(async () => { |
38 | | - await producerApp?.stop(); |
| 45 | + |
| 46 | + beforeEach(() => listenerStub.reset()); |
| 47 | + after(async () => await producerApp?.stop()); |
| 48 | + |
| 49 | + runProducerTests( |
| 50 | + () => listenerStub, |
| 51 | + () => producerService, |
| 52 | + ); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +function runProducerTests( |
| 57 | + getListenerStub: () => sinon.SinonStub, |
| 58 | + getProducerService: () => TestProducerService, |
| 59 | +): void { |
| 60 | + describe('Producer', () => { |
| 61 | + it('should produce an event for a particular topic', async () => { |
| 62 | + const producerService = getProducerService(); |
| 63 | + const listenerStub = getListenerStub(); |
| 64 | + |
| 65 | + await producerService.produceEventA('test string'); |
| 66 | + |
| 67 | + sinon.assert.calledWithExactly( |
| 68 | + listenerStub, |
| 69 | + 'Event A', |
| 70 | + 'test string', |
| 71 | + {}, |
| 72 | + ); |
39 | 73 | }); |
40 | | - describe('Producer', () => { |
41 | | - it('should produce an event for a particular topic', async () => { |
42 | | - await producerService.produceEventA('test string'); |
43 | | - sinon.assert.calledWithExactly( |
44 | | - listenerStub, |
45 | | - 'Event A', |
46 | | - 'test string', |
47 | | - {}, |
48 | | - ); |
49 | | - }); |
50 | | - it('should produce multiple events for a particular topic', async () => { |
51 | | - const data = ['test string 1', 'test string 2']; |
52 | | - await producerService.produceMultipleA(data); |
53 | | - sinon.assert.calledWithExactly( |
54 | | - listenerStub, |
55 | | - JSON.stringify( |
56 | | - data.map(d => ({name: 'Event A', data: d, type: Events.A})), |
57 | | - ), |
58 | | - '', |
59 | | - {}, |
60 | | - ); |
61 | | - }); |
| 74 | + |
| 75 | + it('should produce multiple events for a particular topic', async () => { |
| 76 | + const producerService = getProducerService(); |
| 77 | + const listenerStub = getListenerStub(); |
| 78 | + |
| 79 | + const data = ['test string 1', 'test string 2']; |
| 80 | + await producerService.produceMultipleA(data); |
| 81 | + |
| 82 | + sinon.assert.calledWithExactly( |
| 83 | + listenerStub, |
| 84 | + JSON.stringify( |
| 85 | + data.map(d => ({name: 'Event A', data: d, type: Events.A})), |
| 86 | + ), |
| 87 | + '', |
| 88 | + {}, |
| 89 | + ); |
62 | 90 | }); |
63 | 91 | }); |
64 | | -}); |
| 92 | +} |
0 commit comments