fix(api): add message service & subscriber service happy path unit tests

This commit is contained in:
yassinedorbozgithub 2025-05-12 10:51:46 +01:00
parent 545c8e967c
commit e203485d75
2 changed files with 74 additions and 0 deletions

View File

@ -31,6 +31,9 @@ import {
rootMongooseTestModule, rootMongooseTestModule,
} from '@/utils/test/test'; } from '@/utils/test/test';
import { buildTestingMocks } from '@/utils/test/utils'; import { buildTestingMocks } from '@/utils/test/utils';
import { IOOutgoingSubscribeMessage } from '@/websocket/pipes/io-message.pipe';
import { Room } from '@/websocket/types';
import { WebsocketGateway } from '@/websocket/websocket.gateway';
import { MessageRepository } from '../repositories/message.repository'; import { MessageRepository } from '../repositories/message.repository';
import { Message, MessageModel } from '../schemas/message.schema'; import { Message, MessageModel } from '../schemas/message.schema';
@ -53,6 +56,13 @@ describe('MessageService', () => {
let recipient: Subscriber; let recipient: Subscriber;
let messagesWithSenderAndRecipient: Message[]; let messagesWithSenderAndRecipient: Message[];
let user: User; let user: User;
let mockGateway: Partial<WebsocketGateway>;
let mockMessageService: MessageService;
const SESSION_ID = 'session-123';
const SUCCESS_PAYLOAD: IOOutgoingSubscribeMessage = {
success: true,
subscribe: Room.MESSAGE,
};
beforeAll(async () => { beforeAll(async () => {
const { getMocks } = await buildTestingMocks({ const { getMocks } = await buildTestingMocks({
@ -102,11 +112,38 @@ describe('MessageService', () => {
recipient: allSubscribers.find(({ id }) => id === message.recipient)?.id, recipient: allSubscribers.find(({ id }) => id === message.recipient)?.id,
sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id, sentBy: allUsers.find(({ id }) => id === message.sentBy)?.id,
})); }));
mockGateway = {
joinNotificationSockets: jest.fn(),
};
mockMessageService = new MessageService(
{} as any,
{} as any,
mockGateway as any,
);
}); });
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
afterAll(closeInMongodConnection); afterAll(closeInMongodConnection);
describe('subscribe', () => {
it('should join Notification sockets message room and return a success response', async () => {
const req = { sessionID: SESSION_ID };
const res = {
json: jest.fn(),
status: jest.fn().mockReturnThis(),
};
await mockMessageService.subscribe(req as any, res as any);
expect(mockGateway.joinNotificationSockets).toHaveBeenCalledWith(
SESSION_ID,
Room.MESSAGE,
);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(SUCCESS_PAYLOAD);
});
});
describe('findOneAndPopulate', () => { describe('findOneAndPopulate', () => {
it('should find message by id, and populate its corresponding sender and recipient', async () => { it('should find message by id, and populate its corresponding sender and recipient', async () => {
jest.spyOn(messageRepository, 'findOneAndPopulate'); jest.spyOn(messageRepository, 'findOneAndPopulate');

View File

@ -28,6 +28,9 @@ import {
rootMongooseTestModule, rootMongooseTestModule,
} from '@/utils/test/test'; } from '@/utils/test/test';
import { buildTestingMocks } from '@/utils/test/utils'; import { buildTestingMocks } from '@/utils/test/utils';
import { IOOutgoingSubscribeMessage } from '@/websocket/pipes/io-message.pipe';
import { Room } from '@/websocket/types';
import { WebsocketGateway } from '@/websocket/websocket.gateway';
import { LabelRepository } from '../repositories/label.repository'; import { LabelRepository } from '../repositories/label.repository';
import { SubscriberRepository } from '../repositories/subscriber.repository'; import { SubscriberRepository } from '../repositories/subscriber.repository';
@ -45,6 +48,13 @@ describe('SubscriberService', () => {
let allSubscribers: Subscriber[]; let allSubscribers: Subscriber[];
let allLabels: Label[]; let allLabels: Label[];
let allUsers: User[]; let allUsers: User[];
let mockGateway: Partial<WebsocketGateway>;
let mockSubscriberService: SubscriberService;
const SESSION_ID = 'session-123';
const SUCCESS_PAYLOAD: IOOutgoingSubscribeMessage = {
success: true,
subscribe: Room.SUBSCRIBER,
};
beforeAll(async () => { beforeAll(async () => {
const { getMocks } = await buildTestingMocks({ const { getMocks } = await buildTestingMocks({
@ -84,11 +94,38 @@ describe('SubscriberService', () => {
allSubscribers = await subscriberRepository.findAll(); allSubscribers = await subscriberRepository.findAll();
allLabels = await labelRepository.findAll(); allLabels = await labelRepository.findAll();
allUsers = await userRepository.findAll(); allUsers = await userRepository.findAll();
mockGateway = {
joinNotificationSockets: jest.fn(),
};
mockSubscriberService = new SubscriberService(
{} as any,
{} as any,
mockGateway as any,
);
}); });
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
afterAll(closeInMongodConnection); afterAll(closeInMongodConnection);
describe('subscribe', () => {
it('should join Notification sockets subscriber room and return a success response', async () => {
const req = { sessionID: SESSION_ID };
const res = {
json: jest.fn(),
status: jest.fn().mockReturnThis(),
};
await mockSubscriberService.subscribe(req as any, res as any);
expect(mockGateway.joinNotificationSockets).toHaveBeenCalledWith(
SESSION_ID,
Room.SUBSCRIBER,
);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(SUCCESS_PAYLOAD);
});
});
describe('findOneAndPopulate', () => { describe('findOneAndPopulate', () => {
it('should find subscribers, and foreach subscriber populate its corresponding labels', async () => { it('should find subscribers, and foreach subscriber populate its corresponding labels', async () => {
jest.spyOn(subscriberService, 'findOneAndPopulate'); jest.spyOn(subscriberService, 'findOneAndPopulate');