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.
This commit is contained in:
Mathis HERRIOT
2026-01-29 15:54:16 +01:00
parent 50787c9357
commit 8eb0cba050
2 changed files with 19 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import { ForbiddenException, NotFoundException } from "@nestjs/common"; import { ForbiddenException, NotFoundException } from "@nestjs/common";
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { EventsGateway } from "../realtime/events.gateway";
import { S3Service } from "../s3/s3.service"; import { S3Service } from "../s3/s3.service";
import { CommentsService } from "./comments.service"; import { CommentsService } from "./comments.service";
import { CommentLikesRepository } from "./repositories/comment-likes.repository"; import { CommentLikesRepository } from "./repositories/comment-likes.repository";
@@ -13,6 +14,7 @@ describe("CommentsService", () => {
create: jest.fn(), create: jest.fn(),
findAllByContentId: jest.fn(), findAllByContentId: jest.fn(),
findOne: jest.fn(), findOne: jest.fn(),
findOneEnriched: jest.fn(),
delete: jest.fn(), delete: jest.fn(),
}; };
@@ -27,6 +29,10 @@ describe("CommentsService", () => {
getPublicUrl: jest.fn(), getPublicUrl: jest.fn(),
}; };
const mockEventsGateway = {
sendToContent: jest.fn(),
};
beforeEach(async () => { beforeEach(async () => {
jest.clearAllMocks(); jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
@@ -35,6 +41,7 @@ describe("CommentsService", () => {
{ provide: CommentsRepository, useValue: mockCommentsRepository }, { provide: CommentsRepository, useValue: mockCommentsRepository },
{ provide: CommentLikesRepository, useValue: mockCommentLikesRepository }, { provide: CommentLikesRepository, useValue: mockCommentLikesRepository },
{ provide: S3Service, useValue: mockS3Service }, { provide: S3Service, useValue: mockS3Service },
{ provide: EventsGateway, useValue: mockEventsGateway },
], ],
}).compile(); }).compile();
@@ -51,7 +58,11 @@ describe("CommentsService", () => {
const userId = "user1"; const userId = "user1";
const contentId = "content1"; const contentId = "content1";
const dto = { text: "Nice meme", parentId: undefined }; 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); const result = await service.create(userId, contentId, dto);
expect(result.id).toBe("c1"); expect(result.id).toBe("c1");
@@ -61,6 +72,11 @@ describe("CommentsService", () => {
text: dto.text, text: dto.text,
parentId: undefined, parentId: undefined,
}); });
expect(mockEventsGateway.sendToContent).toHaveBeenCalledWith(
contentId,
"new_comment",
expect.any(Object),
);
}); });
}); });

View File

@@ -17,6 +17,8 @@ describe("MessagesService", () => {
findAllConversations: jest.fn(), findAllConversations: jest.fn(),
isParticipant: jest.fn(), isParticipant: jest.fn(),
findMessagesByConversationId: jest.fn(), findMessagesByConversationId: jest.fn(),
markAsRead: jest.fn(),
countUnreadMessages: jest.fn(),
}; };
const mockEventsGateway = { const mockEventsGateway = {