test: add unit tests for comment liking and enriched comment data
- Added tests for comment liking (`like` and `unlike` methods). - Improved `findAllByContentId` tests to cover enriched comment data (likes count, isLiked, and user avatar URL resolution). - Mocked new dependencies (`CommentLikesRepository` and `S3Service`) in `CommentsService` unit tests.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
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 { S3Service } from "../s3/s3.service";
|
||||||
import { CommentsService } from "./comments.service";
|
import { CommentsService } from "./comments.service";
|
||||||
|
import { CommentLikesRepository } from "./repositories/comment-likes.repository";
|
||||||
import { CommentsRepository } from "./repositories/comments.repository";
|
import { CommentsRepository } from "./repositories/comments.repository";
|
||||||
|
|
||||||
describe("CommentsService", () => {
|
describe("CommentsService", () => {
|
||||||
@@ -14,11 +16,25 @@ describe("CommentsService", () => {
|
|||||||
delete: jest.fn(),
|
delete: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mockCommentLikesRepository = {
|
||||||
|
addLike: jest.fn(),
|
||||||
|
removeLike: jest.fn(),
|
||||||
|
countByCommentId: jest.fn(),
|
||||||
|
isLikedByUser: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockS3Service = {
|
||||||
|
getPublicUrl: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks();
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
CommentsService,
|
CommentsService,
|
||||||
{ provide: CommentsRepository, useValue: mockCommentsRepository },
|
{ provide: CommentsRepository, useValue: mockCommentsRepository },
|
||||||
|
{ provide: CommentLikesRepository, useValue: mockCommentLikesRepository },
|
||||||
|
{ provide: S3Service, useValue: mockS3Service },
|
||||||
],
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
@@ -34,7 +50,7 @@ describe("CommentsService", () => {
|
|||||||
it("should create a comment", async () => {
|
it("should create a comment", async () => {
|
||||||
const userId = "user1";
|
const userId = "user1";
|
||||||
const contentId = "content1";
|
const contentId = "content1";
|
||||||
const dto = { text: "Nice meme" };
|
const dto = { text: "Nice meme", parentId: undefined };
|
||||||
mockCommentsRepository.create.mockResolvedValue({ id: "c1", ...dto });
|
mockCommentsRepository.create.mockResolvedValue({ id: "c1", ...dto });
|
||||||
|
|
||||||
const result = await service.create(userId, contentId, dto);
|
const result = await service.create(userId, contentId, dto);
|
||||||
@@ -43,16 +59,25 @@ describe("CommentsService", () => {
|
|||||||
userId,
|
userId,
|
||||||
contentId,
|
contentId,
|
||||||
text: dto.text,
|
text: dto.text,
|
||||||
|
parentId: undefined,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("findAllByContentId", () => {
|
describe("findAllByContentId", () => {
|
||||||
it("should return comments for a content", async () => {
|
it("should return comments for a content", async () => {
|
||||||
mockCommentsRepository.findAllByContentId.mockResolvedValue([{ id: "c1" }]);
|
mockCommentsRepository.findAllByContentId.mockResolvedValue([
|
||||||
const result = await service.findAllByContentId("content1");
|
{ id: "c1", user: { avatarUrl: "path" } },
|
||||||
|
]);
|
||||||
|
mockCommentLikesRepository.countByCommentId.mockResolvedValue(5);
|
||||||
|
mockCommentLikesRepository.isLikedByUser.mockResolvedValue(true);
|
||||||
|
mockS3Service.getPublicUrl.mockReturnValue("url");
|
||||||
|
|
||||||
|
const result = await service.findAllByContentId("content1", "u1");
|
||||||
expect(result).toHaveLength(1);
|
expect(result).toHaveLength(1);
|
||||||
expect(repository.findAllByContentId).toHaveBeenCalledWith("content1");
|
expect(result[0].likesCount).toBe(5);
|
||||||
|
expect(result[0].isLiked).toBe(true);
|
||||||
|
expect(result[0].user.avatarUrl).toBe("url");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -81,4 +106,23 @@ describe("CommentsService", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("like", () => {
|
||||||
|
it("should add like", async () => {
|
||||||
|
mockCommentsRepository.findOne.mockResolvedValue({ id: "c1" });
|
||||||
|
await service.like("u1", "c1");
|
||||||
|
expect(mockCommentLikesRepository.addLike).toHaveBeenCalledWith("c1", "u1");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("unlike", () => {
|
||||||
|
it("should remove like", async () => {
|
||||||
|
mockCommentsRepository.findOne.mockResolvedValue({ id: "c1" });
|
||||||
|
await service.unlike("u1", "c1");
|
||||||
|
expect(mockCommentLikesRepository.removeLike).toHaveBeenCalledWith(
|
||||||
|
"c1",
|
||||||
|
"u1",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user