Skip to content

Commit

Permalink
fix: basic unit tests including mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
orig committed Sep 19, 2023
1 parent 00bc8ae commit d45bbee
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
16 changes: 10 additions & 6 deletions libs/queue-manager/src/lib/__mocks__/queue-manager.service.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
17 changes: 0 additions & 17 deletions libs/queue-manager/src/lib/producer/__mocks__/producer.service.ts

This file was deleted.

31 changes: 26 additions & 5 deletions libs/queue-manager/src/lib/producer/producer.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand All @@ -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,
});
});
});
6 changes: 5 additions & 1 deletion libs/queue-manager/src/lib/producer/producer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions libs/queue-manager/src/lib/queue-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit d45bbee

Please sign in to comment.