diff --git a/libs/queue-manager/src/lib/__mocks__/queue-manager.service.ts b/libs/queue-manager/src/lib/__mocks__/queue-manager.service.ts index 10e1cda4..1c55ea34 100644 --- a/libs/queue-manager/src/lib/__mocks__/queue-manager.service.ts +++ b/libs/queue-manager/src/lib/__mocks__/queue-manager.service.ts @@ -1,12 +1,16 @@ -import { Injectable } from '@nestjs/common'; +// Mock implementation for the queue manager service (memphis-dev) +export class MemphisMock { + produce(payload: any) { + console.log(`Producing message to ${payload.stationName}`); + } +} -@Injectable() +// Mock implementation for the queue manager service export class QueueManagerService { - constructor(private readonly queueManager: any) {} + private readonly queueManager: MemphisMock; - produce(payload: any) { - // Mock implementation for producing messages - console.log(`Producing message to ${payload.stationName}`); + constructor() { + this.queueManager = new MemphisMock(); } getQueueManager() { diff --git a/libs/queue-manager/src/lib/producer/__mocks__/producer.service.ts b/libs/queue-manager/src/lib/producer/__mocks__/producer.service.ts deleted file mode 100644 index 3a5f3bfe..00000000 --- a/libs/queue-manager/src/lib/producer/__mocks__/producer.service.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { QueueManagerService } from '@reduced.to/queue-manager'; -import { Memphis } from 'memphis-dev/*'; - - -export abstract class ProducerService extends QueueManagerService { - constructor(private readonly queueManager: Memphis, protected readonly name: string) { - super(new MockQueueManager()); // Inject the mock queue manager - } - - async publish(queueName: string, data: any) { - return this.getQueueManager().produce({ - stationName: queueName, - producerName: this.name, - message: data, - }); - } -} diff --git a/libs/queue-manager/src/lib/producer/producer.service.spec.ts b/libs/queue-manager/src/lib/producer/producer.service.spec.ts index a2074aa6..adf5b507 100644 --- a/libs/queue-manager/src/lib/producer/producer.service.spec.ts +++ b/libs/queue-manager/src/lib/producer/producer.service.spec.ts @@ -1,20 +1,23 @@ import { Inject } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { ProducerService, QUEUE_MANAGER_INJECTION_TOKEN, QueueManagerModule } from '@reduced.to/queue-manager'; +import { ProducerService, QUEUE_MANAGER_INJECTION_TOKEN, QueueManagerModule, QueueManagerService } from '@reduced.to/queue-manager'; import { Memphis } from 'memphis-dev'; import { AppConfigModule } from '@reduced.to/config'; +jest.mock('../queue-manager.service'); + describe('ProducerService', () => { + + const TEST_PRODUCER_NAME = 'test-producer'; class TestProducerService extends ProducerService { constructor(@Inject(QUEUE_MANAGER_INJECTION_TOKEN) queueManager: Memphis) { - super(queueManager, 'test-producer'); + super(queueManager, TEST_PRODUCER_NAME); } } let service: TestProducerService; beforeEach(async () => { - jest.setTimeout(600000); const module: TestingModule = await Test.createTestingModule({ imports: [AppConfigModule, QueueManagerModule], providers: [TestProducerService], @@ -27,8 +30,26 @@ describe('ProducerService', () => { expect(service).toBeDefined(); }); + it('should get the queue manager', () => { + expect(service.getQueueManager()).toBeDefined(); + }); + + it('should get the producer name', () => { + expect(service.getName()).toBe(TEST_PRODUCER_NAME); + }); + it('should publish a message to the queue', async () => { - const test = await service.publish('test-queue', 'test-message'); - console.log(test); + const queueManagerSpy = jest.spyOn(service.getQueueManager(), 'produce'); + + const QUEUE_NAME = 'test-queue'; + const PAYLOAD = { message: 'test', 1: 2 }; + + await service.publish(QUEUE_NAME, PAYLOAD); + expect(queueManagerSpy).toBeCalledTimes(1); + expect(queueManagerSpy).toBeCalledWith({ + stationName: QUEUE_NAME, + producerName: TEST_PRODUCER_NAME, + message: PAYLOAD, + }); }); }); diff --git a/libs/queue-manager/src/lib/producer/producer.service.ts b/libs/queue-manager/src/lib/producer/producer.service.ts index 71255739..b64d875e 100644 --- a/libs/queue-manager/src/lib/producer/producer.service.ts +++ b/libs/queue-manager/src/lib/producer/producer.service.ts @@ -3,10 +3,14 @@ import { QUEUE_MANAGER_INJECTION_TOKEN, QueueManagerService } from '@reduced.to/ import { Memphis } from 'memphis-dev/*'; export abstract class ProducerService extends QueueManagerService { - constructor(@Inject(QUEUE_MANAGER_INJECTION_TOKEN) queueManager: Memphis, protected readonly name: string) { + constructor(@Inject(QUEUE_MANAGER_INJECTION_TOKEN) queueManager: Memphis, private readonly name: string) { super(queueManager); } + getName() { + return this.name; + } + async publish(queueName: string, data: any) { return this.getQueueManager().produce({ stationName: queueName, diff --git a/libs/queue-manager/src/lib/queue-manager.service.ts b/libs/queue-manager/src/lib/queue-manager.service.ts index 08ead9c1..fa358146 100644 --- a/libs/queue-manager/src/lib/queue-manager.service.ts +++ b/libs/queue-manager/src/lib/queue-manager.service.ts @@ -3,10 +3,10 @@ import { QUEUE_MANAGER_INJECTION_TOKEN } from '@reduced.to/queue-manager'; import { Memphis } from 'memphis-dev/*'; @Injectable() -export abstract class QueueManagerService { +export class QueueManagerService { constructor(@Inject(QUEUE_MANAGER_INJECTION_TOKEN) private readonly queueManager: Memphis) {} - protected getQueueManager() { + getQueueManager() { return this.queueManager; } }