Files
canina/backend/src/admin/blogs.controller.spec.ts
T

324 lines
11 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { BlogsController } from "./blogs.controller";
import { BlogsService } from "./blogs.service";
import { PaginationDto } from "../common/dto/pagination.dto";
describe("BlogsController (Admin)", () => {
let controller: BlogsController;
let service: BlogsService;
const mockBlogsService = {
getBlogs: jest.fn(),
getAuthors: jest.fn(),
getBlogCategories: jest.fn(),
createBlogCategory: jest.fn(),
updateBlogCategory: jest.fn(),
deleteBlogCategory: jest.fn(),
getBlogTags: jest.fn(),
createBlogTag: jest.fn(),
deleteBlogTag: jest.fn(),
getBlogById: jest.fn(),
createBlog: jest.fn(),
updateBlog: jest.fn(),
deleteBlog: jest.fn(),
getComments: jest.fn(),
updateCommentStatus: jest.fn(),
deleteComment: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [BlogsController],
providers: [
{
provide: BlogsService,
useValue: mockBlogsService,
},
],
}).compile();
controller = module.get<BlogsController>(BlogsController);
service = module.get<BlogsService>(BlogsService);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
describe("getBlogs", () => {
it("should return paginated blogs with search and filters", async () => {
const query: PaginationDto & {
status?: string;
categoryId?: string;
tagId?: string;
} = {
page: 1,
limit: 10,
search: "تغذیه سگ",
status: "PUBLISHED",
categoryId: "cat-blog-1",
};
const expectedResult = {
data: [
{
id: "blog-1",
title: "راهنمای جامع تغذیه سگ‌های مسن",
slug: "senior-dog-nutrition-guide",
status: "PUBLISHED",
categoryId: "cat-blog-1",
},
],
meta: {
total: 1,
page: 1,
limit: 10,
lastPage: 1,
},
};
mockBlogsService.getBlogs.mockResolvedValue(expectedResult);
const result = await controller.getBlogs(query);
expect(service.getBlogs).toHaveBeenCalledWith(query);
expect(result).toEqual(expectedResult);
});
});
describe("getAuthors", () => {
it("should return authors list wrapped with success: true", async () => {
const authors = [
{
id: "user-1",
firstName: "دکتر",
lastName: "حسینی",
email: "vet@canina.ir",
},
];
mockBlogsService.getAuthors.mockResolvedValue(authors);
const result = await controller.getAuthors();
expect(service.getAuthors).toHaveBeenCalledTimes(1);
expect(result).toEqual({ success: true, data: authors });
});
});
describe("Blog Categories", () => {
it("getBlogCategories should return categories wrapped in success: true", async () => {
const categories = [
{ id: "bcat-1", name: "سلامت مفاصل", slug: "joint-health" },
];
mockBlogsService.getBlogCategories.mockResolvedValue(categories);
const result = await controller.getBlogCategories();
expect(service.getBlogCategories).toHaveBeenCalledTimes(1);
expect(result).toEqual({ success: true, data: categories });
});
it("createBlogCategory should create blog category and return success: true", async () => {
const body = {
name: "بهداشت و مراقبت",
slug: "care-hygiene",
description: "مقالات مرتبط با مراقبت روزانه حیوانات",
};
const created = { id: "bcat-2", ...body };
mockBlogsService.createBlogCategory.mockResolvedValue(created);
const result = await controller.createBlogCategory(body);
expect(service.createBlogCategory).toHaveBeenCalledWith(body);
expect(result).toEqual({ success: true, data: created });
});
it("updateBlogCategory should update category and return success: true", async () => {
const id = "bcat-1";
const body = { name: "سلامت مفاصل و استخوان" };
const updated = { id, slug: "joint-health", ...body };
mockBlogsService.updateBlogCategory.mockResolvedValue(updated);
const result = await controller.updateBlogCategory(id, body);
expect(service.updateBlogCategory).toHaveBeenCalledWith(id, body);
expect(result).toEqual({ success: true, data: updated });
});
it("deleteBlogCategory should delete category and return success: true", async () => {
const id = "bcat-1";
mockBlogsService.deleteBlogCategory.mockResolvedValue(undefined);
const result = await controller.deleteBlogCategory(id);
expect(service.deleteBlogCategory).toHaveBeenCalledWith(id);
expect(result).toEqual({ success: true });
});
});
describe("Blog Tags", () => {
it("getBlogTags should return tags wrapped in success: true", async () => {
const tags = [{ id: "tag-1", name: "سگ", slug: "dog" }];
mockBlogsService.getBlogTags.mockResolvedValue(tags);
const result = await controller.getBlogTags();
expect(service.getBlogTags).toHaveBeenCalledTimes(1);
expect(result).toEqual({ success: true, data: tags });
});
it("createBlogTag should create tag and return success: true", async () => {
const body = { name: "مکمل تقویتی", slug: "supplements" };
const created = { id: "tag-2", ...body };
mockBlogsService.createBlogTag.mockResolvedValue(created);
const result = await controller.createBlogTag(body);
expect(service.createBlogTag).toHaveBeenCalledWith(body);
expect(result).toEqual({ success: true, data: created });
});
it("deleteBlogTag should delete tag and return success: true", async () => {
const id = "tag-1";
mockBlogsService.deleteBlogTag.mockResolvedValue(undefined);
const result = await controller.deleteBlogTag(id);
expect(service.deleteBlogTag).toHaveBeenCalledWith(id);
expect(result).toEqual({ success: true });
});
});
describe("Blog Articles CRUD", () => {
it("getBlogById should return single blog article", async () => {
const blogId = "blog-1";
const blogData = {
id: blogId,
title: "مزایای پودر صدف سبز",
slug: "green-lipped-mussel-benefits",
content: "<p>محتوای مقاله...</p>",
status: "PUBLISHED",
};
mockBlogsService.getBlogById.mockResolvedValue(blogData);
const result = await controller.getBlogById(blogId);
expect(service.getBlogById).toHaveBeenCalledWith(blogId);
expect(result).toEqual({ success: true, data: blogData });
});
it("createBlog should create new blog with author user id", async () => {
const req = { user: { id: "admin-user-id" } };
const body = {
title: "ویتامین‌های ضروری برای دوران رشد توله سگ",
slug: "puppy-vitamins",
content: "توضیحات پیرامون ویتامین D3 و کلسیم",
status: "DRAFT",
categoryId: "bcat-1",
};
const createdBlog = { id: "blog-10", ...body, authorId: req.user.id };
mockBlogsService.createBlog.mockResolvedValue(createdBlog);
const result = await controller.createBlog(body, req);
expect(service.createBlog).toHaveBeenCalledWith(body, req.user.id);
expect(result).toEqual({ success: true, data: createdBlog });
});
it("propagates create validation failures", async () => {
const req = { user: { id: "admin-user-id" } };
const body = { title: "Bad date", contentUpdatedAt: "invalid", createdAt: "2000-01-01" };
mockBlogsService.createBlog.mockRejectedValueOnce(new Error("Invalid contentUpdatedAt"));
await expect(controller.createBlog(body, req)).rejects.toThrow("Invalid contentUpdatedAt");
expect(service.createBlog).toHaveBeenCalledWith(body, req.user.id);
});
it("updateBlog should update blog details and return success: true", async () => {
const blogId = "blog-10";
const body = {
status: "PUBLISHED",
publishedAt: new Date().toISOString(),
};
const updatedBlog = { id: blogId, ...body };
mockBlogsService.updateBlog.mockResolvedValue(updatedBlog);
const result = await controller.updateBlog(blogId, body);
expect(service.updateBlog).toHaveBeenCalledWith(blogId, body);
expect(result).toEqual({ success: true, data: updatedBlog });
});
it("propagates invalid content timestamp rejection without accepting createdAt", async () => {
const body = { contentUpdatedAt: "invalid", createdAt: "2000-01-01" };
mockBlogsService.updateBlog.mockRejectedValueOnce(new Error("Invalid contentUpdatedAt"));
await expect(controller.updateBlog("blog-10", body)).rejects.toThrow("Invalid contentUpdatedAt");
expect(service.updateBlog).toHaveBeenCalledWith("blog-10", body);
});
it("deleteBlog should delete blog and return success: true", async () => {
const blogId = "blog-10";
mockBlogsService.deleteBlog.mockResolvedValue(undefined);
const result = await controller.deleteBlog(blogId);
expect(service.deleteBlog).toHaveBeenCalledWith(blogId);
expect(result).toEqual({ success: true });
});
});
describe("Blog Comments", () => {
it("getComments should return paginated comments based on query filters", async () => {
const query = { page: 1, limit: 10, status: "PENDING", blogId: "blog-1" };
const commentsResult = {
data: [
{
id: "comm-1",
content: "آیا این مکمل برای توله‌های زیر ۶ ماه هم مناسبه؟",
status: "PENDING",
blogId: "blog-1",
user: { firstName: "مریم", lastName: "کریمی" },
},
],
meta: { total: 1, page: 1, limit: 10, lastPage: 1 },
};
mockBlogsService.getComments.mockResolvedValue(commentsResult);
const result = await controller.getComments(query);
expect(service.getComments).toHaveBeenCalledWith(query);
expect(result).toEqual(commentsResult);
});
it("updateCommentStatus should update status to APPROVED or REJECTED", async () => {
const commentId = "comm-1";
const body = { status: "APPROVED" as const };
const updatedComment = { id: commentId, status: "APPROVED" };
mockBlogsService.updateCommentStatus.mockResolvedValue(updatedComment);
const result = await controller.updateCommentStatus(commentId, body);
expect(service.updateCommentStatus).toHaveBeenCalledWith(
commentId,
"APPROVED",
);
expect(result).toEqual(updatedComment);
});
it("deleteComment should delete comment and return success: true", async () => {
const commentId = "comm-1";
mockBlogsService.deleteComment.mockResolvedValue(undefined);
const result = await controller.deleteComment(commentId);
expect(service.deleteComment).toHaveBeenCalledWith(commentId);
expect(result).toEqual({ success: true });
});
});
});