From 8eb0cba0508fc246b54c5c804c135b46bbc57913 Mon Sep 17 00:00:00 2001 From: Mathis HERRIOT <197931332+0x485254@users.noreply.github.com> Date: Thu, 29 Jan 2026 15:54:16 +0100 Subject: [PATCH] test: improve unit tests with new mocks and WebSocket validation - Added `markAsRead` and `countUnreadMessages` mocks to `MessagesService` tests. - Included enriched comment retrieval and WebSocket notification validation in `CommentsService` tests. - Updated dependency injection to include `EventsGateway` in `CommentsService` tests. --- backend/src/comments/comments.service.spec.ts | 18 +++++++++++++++++- backend/src/messages/messages.service.spec.ts | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/src/comments/comments.service.spec.ts b/backend/src/comments/comments.service.spec.ts index e7b5bae..7c9b3a6 100644 --- a/backend/src/comments/comments.service.spec.ts +++ b/backend/src/comments/comments.service.spec.ts @@ -1,5 +1,6 @@ import { ForbiddenException, NotFoundException } from "@nestjs/common"; import { Test, TestingModule } from "@nestjs/testing"; +import { EventsGateway } from "../realtime/events.gateway"; import { S3Service } from "../s3/s3.service"; import { CommentsService } from "./comments.service"; import { CommentLikesRepository } from "./repositories/comment-likes.repository"; @@ -13,6 +14,7 @@ describe("CommentsService", () => { create: jest.fn(), findAllByContentId: jest.fn(), findOne: jest.fn(), + findOneEnriched: jest.fn(), delete: jest.fn(), }; @@ -27,6 +29,10 @@ describe("CommentsService", () => { getPublicUrl: jest.fn(), }; + const mockEventsGateway = { + sendToContent: jest.fn(), + }; + beforeEach(async () => { jest.clearAllMocks(); const module: TestingModule = await Test.createTestingModule({ @@ -35,6 +41,7 @@ describe("CommentsService", () => { { provide: CommentsRepository, useValue: mockCommentsRepository }, { provide: CommentLikesRepository, useValue: mockCommentLikesRepository }, { provide: S3Service, useValue: mockS3Service }, + { provide: EventsGateway, useValue: mockEventsGateway }, ], }).compile(); @@ -51,7 +58,11 @@ describe("CommentsService", () => { const userId = "user1"; const contentId = "content1"; const dto = { text: "Nice meme", parentId: undefined }; - mockCommentsRepository.create.mockResolvedValue({ id: "c1", ...dto }); + const createdComment = { id: "c1", ...dto, user: { username: "u1" } }; + mockCommentsRepository.create.mockResolvedValue(createdComment); + mockCommentsRepository.findOneEnriched.mockResolvedValue(createdComment); + mockCommentLikesRepository.countByCommentId.mockResolvedValue(0); + mockCommentLikesRepository.isLikedByUser.mockResolvedValue(false); const result = await service.create(userId, contentId, dto); expect(result.id).toBe("c1"); @@ -61,6 +72,11 @@ describe("CommentsService", () => { text: dto.text, parentId: undefined, }); + expect(mockEventsGateway.sendToContent).toHaveBeenCalledWith( + contentId, + "new_comment", + expect.any(Object), + ); }); }); diff --git a/backend/src/messages/messages.service.spec.ts b/backend/src/messages/messages.service.spec.ts index e357295..f4814f2 100644 --- a/backend/src/messages/messages.service.spec.ts +++ b/backend/src/messages/messages.service.spec.ts @@ -17,6 +17,8 @@ describe("MessagesService", () => { findAllConversations: jest.fn(), isParticipant: jest.fn(), findMessagesByConversationId: jest.fn(), + markAsRead: jest.fn(), + countUnreadMessages: jest.fn(), }; const mockEventsGateway = {