diff --git a/api/src/chat/services/message.service.spec.ts b/api/src/chat/services/message.service.spec.ts index 8952c244..927b5898 100644 --- a/api/src/chat/services/message.service.spec.ts +++ b/api/src/chat/services/message.service.spec.ts @@ -31,6 +31,9 @@ import { rootMongooseTestModule, } from '@/utils/test/test'; 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 { Message, MessageModel } from '../schemas/message.schema'; @@ -53,6 +56,13 @@ describe('MessageService', () => { let recipient: Subscriber; let messagesWithSenderAndRecipient: Message[]; let user: User; + let mockGateway: Partial; + let mockMessageService: MessageService; + const SESSION_ID = 'session-123'; + const SUCCESS_PAYLOAD: IOOutgoingSubscribeMessage = { + success: true, + subscribe: Room.MESSAGE, + }; beforeAll(async () => { const { getMocks } = await buildTestingMocks({ @@ -102,11 +112,38 @@ describe('MessageService', () => { recipient: allSubscribers.find(({ id }) => id === message.recipient)?.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); 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', () => { it('should find message by id, and populate its corresponding sender and recipient', async () => { jest.spyOn(messageRepository, 'findOneAndPopulate'); diff --git a/api/src/chat/services/subscriber.service.spec.ts b/api/src/chat/services/subscriber.service.spec.ts index 3bd3104c..a78b4ae6 100644 --- a/api/src/chat/services/subscriber.service.spec.ts +++ b/api/src/chat/services/subscriber.service.spec.ts @@ -28,6 +28,9 @@ import { rootMongooseTestModule, } from '@/utils/test/test'; 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 { SubscriberRepository } from '../repositories/subscriber.repository'; @@ -45,6 +48,13 @@ describe('SubscriberService', () => { let allSubscribers: Subscriber[]; let allLabels: Label[]; let allUsers: User[]; + let mockGateway: Partial; + let mockSubscriberService: SubscriberService; + const SESSION_ID = 'session-123'; + const SUCCESS_PAYLOAD: IOOutgoingSubscribeMessage = { + success: true, + subscribe: Room.SUBSCRIBER, + }; beforeAll(async () => { const { getMocks } = await buildTestingMocks({ @@ -84,11 +94,38 @@ describe('SubscriberService', () => { allSubscribers = await subscriberRepository.findAll(); allLabels = await labelRepository.findAll(); allUsers = await userRepository.findAll(); + mockGateway = { + joinNotificationSockets: jest.fn(), + }; + mockSubscriberService = new SubscriberService( + {} as any, + {} as any, + mockGateway as any, + ); }); afterEach(jest.clearAllMocks); 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', () => { it('should find subscribers, and foreach subscriber populate its corresponding labels', async () => { jest.spyOn(subscriberService, 'findOneAndPopulate');