- 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.
100 lines
3.0 KiB
TypeScript
100 lines
3.0 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { SmartAdvisorService } from "./smart-advisor.service";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import { NotFoundException } from "@nestjs/common";
|
|
|
|
describe("SmartAdvisorService", () => {
|
|
let service: SmartAdvisorService;
|
|
|
|
const mockPrisma = {
|
|
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: [
|
|
SmartAdvisorService,
|
|
{
|
|
provide: PrismaService,
|
|
useValue: mockPrisma,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<SmartAdvisorService>(SmartAdvisorService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("findAll", () => {
|
|
it("should return rules including product details", async () => {
|
|
const rules = [{ id: "rule-1", product: { id: "prod-1", name: "GAG" } }];
|
|
mockPrisma.smartAdvisorRule.findMany.mockResolvedValue(rules);
|
|
|
|
const result = await service.findAll();
|
|
expect(mockPrisma.smartAdvisorRule.findMany).toHaveBeenCalledWith({
|
|
include: { product: true },
|
|
});
|
|
expect(result).toEqual(rules);
|
|
});
|
|
});
|
|
|
|
describe("update", () => {
|
|
it("should throw NotFoundException if rule does not exist", async () => {
|
|
mockPrisma.smartAdvisorRule.findUnique.mockResolvedValue(null);
|
|
await expect(service.update("invalid-id", {})).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it("should update rule successfully when found", async () => {
|
|
mockPrisma.smartAdvisorRule.findUnique.mockResolvedValue({
|
|
id: "rule-1",
|
|
});
|
|
mockPrisma.smartAdvisorRule.update.mockResolvedValue({
|
|
id: "rule-1",
|
|
reason: "updated",
|
|
});
|
|
|
|
const result = await service.update("rule-1", { reason: "updated" });
|
|
expect(mockPrisma.smartAdvisorRule.update).toHaveBeenCalledWith({
|
|
where: { id: "rule-1" },
|
|
data: { reason: "updated" },
|
|
include: { product: true },
|
|
});
|
|
expect(result.reason).toBe("updated");
|
|
});
|
|
});
|
|
|
|
describe("remove", () => {
|
|
it("should throw NotFoundException if rule does not exist", async () => {
|
|
mockPrisma.smartAdvisorRule.findUnique.mockResolvedValue(null);
|
|
await expect(service.remove("invalid-id")).rejects.toThrow(
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
it("should delete rule successfully when found", async () => {
|
|
mockPrisma.smartAdvisorRule.findUnique.mockResolvedValue({
|
|
id: "rule-1",
|
|
});
|
|
mockPrisma.smartAdvisorRule.delete.mockResolvedValue({ id: "rule-1" });
|
|
|
|
const result = await service.remove("rule-1");
|
|
expect(mockPrisma.smartAdvisorRule.delete).toHaveBeenCalledWith({
|
|
where: { id: "rule-1" },
|
|
});
|
|
expect(result.id).toBe("rule-1");
|
|
});
|
|
});
|
|
});
|