Files
canina/backend/src/faq/faq.controller.spec.ts
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

154 lines
4.2 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { FaqController } from "./faq.controller";
import { FaqService } from "./faq.service";
describe("FaqController", () => {
let controller: FaqController;
let service: FaqService;
const mockFaqService = {
findActive: jest.fn(),
findAll: jest.fn(),
create: jest.fn(),
reorder: jest.fn(),
update: jest.fn(),
remove: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [FaqController],
providers: [
{
provide: FaqService,
useValue: mockFaqService,
},
],
}).compile();
controller = module.get<FaqController>(FaqController);
service = module.get<FaqService>(FaqService);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
describe("findActive", () => {
it("should return only active FAQs for public view", async () => {
const activeFaqs = [
{
id: "faq-1",
question: "زمان ارسال سفارش؟",
answer: "۲۴ الی ۴۸ ساعت",
order: 1,
isActive: true,
},
];
mockFaqService.findActive.mockResolvedValue(activeFaqs);
const result = await controller.findActive();
expect(service.findActive).toHaveBeenCalledTimes(1);
expect(result).toEqual(activeFaqs);
});
});
describe("findAll", () => {
it("should return all FAQs for admin view", async () => {
const allFaqs = [
{ id: "faq-1", question: "Q1", answer: "A1", order: 1, isActive: true },
{
id: "faq-2",
question: "Q2",
answer: "A2",
order: 2,
isActive: false,
},
];
mockFaqService.findAll.mockResolvedValue(allFaqs);
const result = await controller.findAll();
expect(service.findAll).toHaveBeenCalledTimes(1);
expect(result).toEqual(allFaqs);
});
});
describe("create", () => {
it("should create a new FAQ entry", async () => {
const body = {
question: "آیا مکمل‌ها گرید دارویی آلمان هستند؟",
answer:
"بله، تمام محصولات مستقیماً از شرکت Canina Pharma آلمان تهیه می‌شوند.",
order: 1,
isActive: true,
};
const createdFaq = { id: "faq-created", ...body };
mockFaqService.create.mockResolvedValue(createdFaq);
const result = await controller.create(body);
expect(service.create).toHaveBeenCalledWith(body);
expect(result).toEqual(createdFaq);
});
});
describe("reorder", () => {
it("should reorder FAQs using provided list of ids and order ranks", async () => {
const body = {
items: [
{ id: "faq-1", order: 2 },
{ id: "faq-2", order: 1 },
],
};
mockFaqService.reorder.mockResolvedValue({ success: true });
const result = await controller.reorder(body);
expect(service.reorder).toHaveBeenCalledWith(body.items);
expect(result).toEqual({ success: true });
});
});
describe("update", () => {
it("should update an existing FAQ", async () => {
const id = "faq-1";
const updateBody = {
question: "سوال ویرایش شده",
isActive: false,
};
const updatedResult = {
id,
question: "سوال ویرایش شده",
answer: "پاسخ قبلی",
order: 1,
isActive: false,
};
mockFaqService.update.mockResolvedValue(updatedResult);
const result = await controller.update(id, updateBody);
expect(service.update).toHaveBeenCalledWith(id, updateBody);
expect(result).toEqual(updatedResult);
});
});
describe("remove", () => {
it("should delete an existing FAQ", async () => {
const id = "faq-1";
const deletedFaq = { id, question: "حذف شده" };
mockFaqService.remove.mockResolvedValue(deletedFaq);
const result = await controller.remove(id);
expect(service.remove).toHaveBeenCalledWith(id);
expect(result).toEqual(deletedFaq);
});
});
});