- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
213 lines
6.0 KiB
TypeScript
213 lines
6.0 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", () => {
|
|
let controller: BlogsController;
|
|
let service: BlogsService;
|
|
|
|
const mockBlogsService = {
|
|
findAll: jest.fn(),
|
|
getCategories: jest.fn(),
|
|
getTags: jest.fn(),
|
|
findHomepage: jest.fn(),
|
|
findOneBySlug: jest.fn(),
|
|
incrementView: jest.fn(),
|
|
findCommentsBySlug: jest.fn(),
|
|
addComment: 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("findAll", () => {
|
|
it("should forward pagination and search query to service.findAll", async () => {
|
|
const query: PaginationDto = {
|
|
page: 1,
|
|
limit: 10,
|
|
search: "تغذیه سگ",
|
|
};
|
|
|
|
const expectedResponse = {
|
|
data: [
|
|
{
|
|
id: "blog-1",
|
|
title: "راهنمای جامع تغذیه سگهای نژاد بزرگ",
|
|
slug: "dog-nutrition-guide",
|
|
isPublished: true,
|
|
},
|
|
],
|
|
meta: {
|
|
total: 1,
|
|
page: 1,
|
|
lastPage: 1,
|
|
limit: 10,
|
|
},
|
|
};
|
|
|
|
mockBlogsService.findAll.mockResolvedValue(expectedResponse);
|
|
|
|
const result = await controller.findAll(query);
|
|
|
|
expect(service.findAll).toHaveBeenCalledWith(query);
|
|
expect(result).toEqual(expectedResponse);
|
|
});
|
|
});
|
|
|
|
describe("findCategories", () => {
|
|
it("should return blog categories from service.getCategories", async () => {
|
|
const categories = [
|
|
{ id: "cat-1", name: "سلامت مفاصل", slug: "joint-health" },
|
|
{ id: "cat-2", name: "مکملهای تخصصی", slug: "supplements" },
|
|
];
|
|
|
|
mockBlogsService.getCategories.mockResolvedValue(categories);
|
|
|
|
const result = await controller.findCategories();
|
|
|
|
expect(service.getCategories).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual(categories);
|
|
});
|
|
});
|
|
|
|
describe("findTags", () => {
|
|
it("should return blog tags from service.getTags", async () => {
|
|
const tags = [
|
|
{ id: "tag-1", name: "کانینا", slug: "canina" },
|
|
{ id: "tag-2", name: "ویتامین", slug: "vitamins" },
|
|
];
|
|
|
|
mockBlogsService.getTags.mockResolvedValue(tags);
|
|
|
|
const result = await controller.findTags();
|
|
|
|
expect(service.getTags).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual(tags);
|
|
});
|
|
});
|
|
|
|
describe("findHomepage", () => {
|
|
it("should return curated homepage blog posts", async () => {
|
|
const homepageData = {
|
|
mode: "selected",
|
|
blogs: [
|
|
{
|
|
id: "blog-1",
|
|
title: "چرا گلوکوزامین برای مفاصل سگ حیاتی است؟",
|
|
slug: "glucosamine-benefits",
|
|
},
|
|
],
|
|
};
|
|
|
|
mockBlogsService.findHomepage.mockResolvedValue(homepageData);
|
|
|
|
const result = await controller.findHomepage();
|
|
|
|
expect(service.findHomepage).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual(homepageData);
|
|
});
|
|
});
|
|
|
|
describe("findOne", () => {
|
|
it("should return single blog details by slug", async () => {
|
|
const slug = "glucosamine-benefits";
|
|
const expectedBlog = {
|
|
id: "blog-1",
|
|
title: "چرا گلوکوزامین برای مفاصل سگ حیاتی است؟",
|
|
slug,
|
|
content: "متن کامل مقاله...",
|
|
viewCount: 120,
|
|
};
|
|
|
|
mockBlogsService.findOneBySlug.mockResolvedValue(expectedBlog);
|
|
|
|
const result = await controller.findOne(slug);
|
|
|
|
expect(service.findOneBySlug).toHaveBeenCalledWith(slug);
|
|
expect(result).toEqual(expectedBlog);
|
|
});
|
|
});
|
|
|
|
describe("incrementView", () => {
|
|
it("should register view count increment by slug", async () => {
|
|
const slug = "glucosamine-benefits";
|
|
const expectedResponse = { success: true, viewCount: 121 };
|
|
|
|
mockBlogsService.incrementView.mockResolvedValue(expectedResponse);
|
|
|
|
const result = await controller.incrementView(slug);
|
|
|
|
expect(service.incrementView).toHaveBeenCalledWith(slug);
|
|
expect(result).toEqual(expectedResponse);
|
|
});
|
|
});
|
|
|
|
describe("findComments", () => {
|
|
it("should return approved comments for a specific blog post", async () => {
|
|
const slug = "glucosamine-benefits";
|
|
const comments = [
|
|
{
|
|
id: "comm-1",
|
|
userName: "سارا احمدی",
|
|
content: "بسیار مقاله آموزندهای بود.",
|
|
status: "APPROVED",
|
|
},
|
|
];
|
|
|
|
mockBlogsService.findCommentsBySlug.mockResolvedValue(comments);
|
|
|
|
const result = await controller.findComments(slug);
|
|
|
|
expect(service.findCommentsBySlug).toHaveBeenCalledWith(slug);
|
|
expect(result).toEqual(comments);
|
|
});
|
|
});
|
|
|
|
describe("addComment", () => {
|
|
it("should submit a new comment for a blog post", async () => {
|
|
const slug = "glucosamine-benefits";
|
|
const body = {
|
|
userName: "امیرحسین رضایی",
|
|
email: "amir@example.com",
|
|
content: "آیا این مکمل برای گربهها هم قابل استفاده است؟",
|
|
userId: "user-123",
|
|
};
|
|
|
|
const createdComment = {
|
|
id: "comm-2",
|
|
blogId: "blog-1",
|
|
...body,
|
|
status: "PENDING",
|
|
};
|
|
|
|
mockBlogsService.addComment.mockResolvedValue(createdComment);
|
|
|
|
const result = await controller.addComment(slug, body);
|
|
|
|
expect(service.addComment).toHaveBeenCalledWith(slug, body);
|
|
expect(result).toEqual(createdComment);
|
|
});
|
|
});
|
|
});
|