- 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.
274 lines
8.9 KiB
TypeScript
274 lines
8.9 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { CmsController } from "./cms.controller";
|
|
import { CmsService } from "./cms.service";
|
|
import {
|
|
CreateHeroBannerDto,
|
|
CreateVetTestimonialDto,
|
|
CreateSmartAdvisorRuleDto,
|
|
} from "./dto/cms.dto";
|
|
|
|
describe("CmsController", () => {
|
|
let controller: CmsController;
|
|
let service: CmsService;
|
|
|
|
const mockCmsService = {
|
|
getHeroBanners: jest.fn(),
|
|
createHeroBanner: jest.fn(),
|
|
updateHeroBanner: jest.fn(),
|
|
deleteHeroBanner: jest.fn(),
|
|
getVetTestimonials: jest.fn(),
|
|
createVetTestimonial: jest.fn(),
|
|
updateVetTestimonial: jest.fn(),
|
|
deleteVetTestimonial: jest.fn(),
|
|
getSmartAdvisorRules: jest.fn(),
|
|
createSmartAdvisorRule: jest.fn(),
|
|
updateSmartAdvisorRule: jest.fn(),
|
|
deleteSmartAdvisorRule: jest.fn(),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
controllers: [CmsController],
|
|
providers: [
|
|
{
|
|
provide: CmsService,
|
|
useValue: mockCmsService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
controller = module.get<CmsController>(CmsController);
|
|
service = module.get<CmsService>(CmsService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(controller).toBeDefined();
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
// ==========================================
|
|
// Hero Banners
|
|
// ==========================================
|
|
describe("Hero Banners", () => {
|
|
const mockBanner = {
|
|
id: "hero-1",
|
|
title: "جشنواره مکملهای تقویتی کانینا",
|
|
subtitle: "تخفیف ویژه محصولات استخوان و مفاصل",
|
|
imageUrl: "https://canina.ir/banners/hero1.jpg",
|
|
buttonText: "مشاهده محصولات",
|
|
buttonLink: "/category/supplements",
|
|
order: 1,
|
|
isActive: true,
|
|
};
|
|
|
|
it("should get all hero banners", async () => {
|
|
mockCmsService.getHeroBanners.mockResolvedValue([mockBanner]);
|
|
|
|
const result = await controller.getHeroBanners();
|
|
|
|
expect(service.getHeroBanners).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual([mockBanner]);
|
|
});
|
|
|
|
it("should create a new hero banner", async () => {
|
|
const dto: CreateHeroBannerDto = {
|
|
title: "جشنواره مکملهای تقویتی کانینا",
|
|
subtitle: "تخفیف ویژه",
|
|
imageUrl: "https://canina.ir/banners/hero1.jpg",
|
|
buttonText: "مشاهده",
|
|
buttonLink: "/category/supplements",
|
|
order: 1,
|
|
isActive: true,
|
|
};
|
|
|
|
mockCmsService.createHeroBanner.mockResolvedValue(mockBanner);
|
|
|
|
const result = await controller.createHeroBanner(dto);
|
|
|
|
expect(service.createHeroBanner).toHaveBeenCalledWith(dto);
|
|
expect(result).toEqual(mockBanner);
|
|
});
|
|
|
|
it("should update an existing hero banner", async () => {
|
|
const updateDto: Partial<CreateHeroBannerDto> = {
|
|
title: "عنوان ویرایش شده",
|
|
isActive: false,
|
|
};
|
|
const updatedBanner = { ...mockBanner, ...updateDto };
|
|
|
|
mockCmsService.updateHeroBanner.mockResolvedValue(updatedBanner);
|
|
|
|
const result = await controller.updateHeroBanner("hero-1", updateDto);
|
|
|
|
expect(service.updateHeroBanner).toHaveBeenCalledWith(
|
|
"hero-1",
|
|
updateDto,
|
|
);
|
|
expect(result).toEqual(updatedBanner);
|
|
});
|
|
|
|
it("should delete a hero banner", async () => {
|
|
mockCmsService.deleteHeroBanner.mockResolvedValue({
|
|
success: true,
|
|
id: "hero-1",
|
|
});
|
|
|
|
const result = await controller.deleteHeroBanner("hero-1");
|
|
|
|
expect(service.deleteHeroBanner).toHaveBeenCalledWith("hero-1");
|
|
expect(result).toEqual({ success: true, id: "hero-1" });
|
|
});
|
|
});
|
|
|
|
// ==========================================
|
|
// Vet Testimonials
|
|
// ==========================================
|
|
describe("Vet Testimonials", () => {
|
|
const mockTestimonial = {
|
|
id: "vet-1",
|
|
vetName: "دکتر رامین کیانی",
|
|
clinicName: "کلینیک مرکزی دامپزشکی آریا",
|
|
imageUrl: "https://canina.ir/vets/kiani.jpg",
|
|
quote: "اثربخشی فرمولاسیون گگ در بازسازی غضروفها بینظیر است.",
|
|
rating: 5,
|
|
order: 1,
|
|
isActive: true,
|
|
};
|
|
|
|
it("should get all vet testimonials", async () => {
|
|
mockCmsService.getVetTestimonials.mockResolvedValue([mockTestimonial]);
|
|
|
|
const result = await controller.getVetTestimonials();
|
|
|
|
expect(service.getVetTestimonials).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual([mockTestimonial]);
|
|
});
|
|
|
|
it("should create a vet testimonial", async () => {
|
|
const dto: CreateVetTestimonialDto = {
|
|
vetName: "دکتر رامین کیانی",
|
|
clinicName: "کلینیک مرکزی دامپزشکی آریا",
|
|
imageUrl: "https://canina.ir/vets/kiani.jpg",
|
|
quote: "اثربخشی فرمولاسیون گگ در بازسازی غضروفها بینظیر است.",
|
|
rating: 5,
|
|
order: 1,
|
|
isActive: true,
|
|
};
|
|
|
|
mockCmsService.createVetTestimonial.mockResolvedValue(mockTestimonial);
|
|
|
|
const result = await controller.createVetTestimonial(dto);
|
|
|
|
expect(service.createVetTestimonial).toHaveBeenCalledWith(dto);
|
|
expect(result).toEqual(mockTestimonial);
|
|
});
|
|
|
|
it("should update a vet testimonial", async () => {
|
|
const updateDto: Partial<CreateVetTestimonialDto> = {
|
|
quote: "ویرایش متن تجربه بالینی",
|
|
rating: 4,
|
|
};
|
|
const updatedTestimonial = { ...mockTestimonial, ...updateDto };
|
|
|
|
mockCmsService.updateVetTestimonial.mockResolvedValue(updatedTestimonial);
|
|
|
|
const result = await controller.updateVetTestimonial("vet-1", updateDto);
|
|
|
|
expect(service.updateVetTestimonial).toHaveBeenCalledWith(
|
|
"vet-1",
|
|
updateDto,
|
|
);
|
|
expect(result).toEqual(updatedTestimonial);
|
|
});
|
|
|
|
it("should delete a vet testimonial", async () => {
|
|
mockCmsService.deleteVetTestimonial.mockResolvedValue({
|
|
success: true,
|
|
id: "vet-1",
|
|
});
|
|
|
|
const result = await controller.deleteVetTestimonial("vet-1");
|
|
|
|
expect(service.deleteVetTestimonial).toHaveBeenCalledWith("vet-1");
|
|
expect(result).toEqual({ success: true, id: "vet-1" });
|
|
});
|
|
});
|
|
|
|
// ==========================================
|
|
// Smart Advisor Rules
|
|
// ==========================================
|
|
describe("Smart Advisor Rules", () => {
|
|
const mockRule = {
|
|
id: "rule-1",
|
|
condition: "لنگش و سفتی مفاصل پا پس از خواب یا استراحت",
|
|
targetPetType: "DOG",
|
|
recommendedProduct: "123e4567-e89b-12d3-a456-426614174000",
|
|
reason:
|
|
"حاوی گلیکوزآمینوگلیکان و هیدروکسی آپاتیت برای تقویت غضروف و تاندون",
|
|
};
|
|
|
|
it("should get all smart advisor rules", async () => {
|
|
mockCmsService.getSmartAdvisorRules.mockResolvedValue([mockRule]);
|
|
|
|
const result = await controller.getSmartAdvisorRules();
|
|
|
|
expect(service.getSmartAdvisorRules).toHaveBeenCalledTimes(1);
|
|
expect(result).toEqual([mockRule]);
|
|
});
|
|
|
|
it("should create a smart advisor rule", async () => {
|
|
const dto: CreateSmartAdvisorRuleDto = {
|
|
condition: "لنگش و سفتی مفاصل پا پس از خواب یا استراحت",
|
|
targetPetType: "DOG",
|
|
recommendedProduct: "123e4567-e89b-12d3-a456-426614174000",
|
|
reason:
|
|
"حاوی گلیکوزآمینوگلیکان و هیدروکسی آپاتیت برای تقویت غضروف و تاندون",
|
|
};
|
|
|
|
mockCmsService.createSmartAdvisorRule.mockResolvedValue(mockRule);
|
|
|
|
const result = await controller.createSmartAdvisorRule(dto);
|
|
|
|
expect(service.createSmartAdvisorRule).toHaveBeenCalledWith(dto);
|
|
expect(result).toEqual(mockRule);
|
|
});
|
|
|
|
it("should update a smart advisor rule", async () => {
|
|
const updateDto: Partial<CreateSmartAdvisorRuleDto> = {
|
|
targetPetType: "BOTH",
|
|
reason: "بهبود دوز مصرفی",
|
|
};
|
|
const updatedRule = { ...mockRule, ...updateDto };
|
|
|
|
mockCmsService.updateSmartAdvisorRule.mockResolvedValue(updatedRule);
|
|
|
|
const result = await controller.updateSmartAdvisorRule(
|
|
"rule-1",
|
|
updateDto,
|
|
);
|
|
|
|
expect(service.updateSmartAdvisorRule).toHaveBeenCalledWith(
|
|
"rule-1",
|
|
updateDto,
|
|
);
|
|
expect(result).toEqual(updatedRule);
|
|
});
|
|
|
|
it("should delete a smart advisor rule", async () => {
|
|
mockCmsService.deleteSmartAdvisorRule.mockResolvedValue({
|
|
success: true,
|
|
id: "rule-1",
|
|
});
|
|
|
|
const result = await controller.deleteSmartAdvisorRule("rule-1");
|
|
|
|
expect(service.deleteSmartAdvisorRule).toHaveBeenCalledWith("rule-1");
|
|
expect(result).toEqual({ success: true, id: "rule-1" });
|
|
});
|
|
});
|
|
});
|