- 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.
162 lines
5.2 KiB
TypeScript
162 lines
5.2 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { CmsService } from "./cms.service";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import { NotFoundException } from "@nestjs/common";
|
|
|
|
describe("CmsService", () => {
|
|
let service: CmsService;
|
|
let prisma: PrismaService;
|
|
|
|
const mockPrisma = {
|
|
heroBanner: {
|
|
findMany: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
vetTestimonial: {
|
|
findMany: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
smartAdvisorRule: {
|
|
findMany: jest.fn(),
|
|
findUnique: jest.fn(),
|
|
create: jest.fn(),
|
|
update: jest.fn(),
|
|
delete: jest.fn(),
|
|
},
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [CmsService, { provide: PrismaService, useValue: mockPrisma }],
|
|
}).compile();
|
|
|
|
service = module.get<CmsService>(CmsService);
|
|
prisma = module.get<PrismaService>(PrismaService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("Hero Banners", () => {
|
|
it("should return all hero banners ordered by order asc", async () => {
|
|
mockPrisma.heroBanner.findMany.mockResolvedValue([
|
|
{ id: "b1", order: 1 },
|
|
]);
|
|
const res = await service.getHeroBanners();
|
|
expect(res).toEqual([{ id: "b1", order: 1 }]);
|
|
expect(mockPrisma.heroBanner.findMany).toHaveBeenCalledWith({
|
|
orderBy: { order: "asc" },
|
|
});
|
|
});
|
|
|
|
it("should create hero banner", async () => {
|
|
const dto = {
|
|
title: "New Banner",
|
|
imageUrl: "/img.png",
|
|
link: "/test",
|
|
order: 1,
|
|
isActive: true,
|
|
};
|
|
mockPrisma.heroBanner.create.mockResolvedValue({ id: "b2", ...dto });
|
|
const res = await service.createHeroBanner(dto);
|
|
expect(res.id).toBe("b2");
|
|
expect(mockPrisma.heroBanner.create).toHaveBeenCalledWith({ data: dto });
|
|
});
|
|
|
|
it("should throw NotFoundException on update if hero banner does not exist", async () => {
|
|
mockPrisma.heroBanner.findUnique.mockResolvedValue(null);
|
|
await expect(
|
|
service.updateHeroBanner("invalid", { title: "x" }),
|
|
).rejects.toThrow(NotFoundException);
|
|
});
|
|
|
|
it("should update hero banner if exists", async () => {
|
|
mockPrisma.heroBanner.findUnique.mockResolvedValue({ id: "b1" });
|
|
mockPrisma.heroBanner.update.mockResolvedValue({
|
|
id: "b1",
|
|
title: "Updated",
|
|
});
|
|
const res = await service.updateHeroBanner("b1", { title: "Updated" });
|
|
expect(res.title).toBe("Updated");
|
|
});
|
|
|
|
it("should delete hero banner", async () => {
|
|
mockPrisma.heroBanner.delete.mockResolvedValue({ id: "b1" });
|
|
const res = await service.deleteHeroBanner("b1");
|
|
expect(res.id).toBe("b1");
|
|
expect(mockPrisma.heroBanner.delete).toHaveBeenCalledWith({
|
|
where: { id: "b1" },
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Vet Testimonials", () => {
|
|
it("should return vet testimonials ordered by order asc", async () => {
|
|
mockPrisma.vetTestimonial.findMany.mockResolvedValue([
|
|
{ id: "t1", order: 1 },
|
|
]);
|
|
const res = await service.getVetTestimonials();
|
|
expect(res).toEqual([{ id: "t1", order: 1 }]);
|
|
});
|
|
|
|
it("should throw NotFoundException on update if vet testimonial does not exist", async () => {
|
|
mockPrisma.vetTestimonial.findUnique.mockResolvedValue(null);
|
|
await expect(service.updateVetTestimonial("invalid", {})).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it("should update vet testimonial if exists", async () => {
|
|
mockPrisma.vetTestimonial.findUnique.mockResolvedValue({ id: "t1" });
|
|
mockPrisma.vetTestimonial.update.mockResolvedValue({
|
|
id: "t1",
|
|
quote: "Great product",
|
|
});
|
|
const res = await service.updateVetTestimonial("t1", {
|
|
quote: "Great product",
|
|
});
|
|
expect(res.quote).toBe("Great product");
|
|
});
|
|
|
|
it("should delete vet testimonial", async () => {
|
|
mockPrisma.vetTestimonial.delete.mockResolvedValue({ id: "t1" });
|
|
const res = await service.deleteVetTestimonial("t1");
|
|
expect(res.id).toBe("t1");
|
|
});
|
|
});
|
|
|
|
describe("Smart Advisor Rules", () => {
|
|
it("should return rules including product details", async () => {
|
|
mockPrisma.smartAdvisorRule.findMany.mockResolvedValue([
|
|
{ id: "r1", product: { id: "p1" } },
|
|
]);
|
|
const res = await service.getSmartAdvisorRules();
|
|
expect(res[0].product.id).toBe("p1");
|
|
expect(mockPrisma.smartAdvisorRule.findMany).toHaveBeenCalledWith({
|
|
include: { product: true },
|
|
});
|
|
});
|
|
|
|
it("should throw NotFoundException on update if rule does not exist", async () => {
|
|
mockPrisma.smartAdvisorRule.findUnique.mockResolvedValue(null);
|
|
await expect(
|
|
service.updateSmartAdvisorRule("invalid", {}),
|
|
).rejects.toThrow(NotFoundException);
|
|
});
|
|
|
|
it("should delete smart advisor rule", async () => {
|
|
mockPrisma.smartAdvisorRule.delete.mockResolvedValue({ id: "r1" });
|
|
const res = await service.deleteSmartAdvisorRule("r1");
|
|
expect(res.id).toBe("r1");
|
|
});
|
|
});
|
|
});
|