Files
canina/backend/src/b2b/b2b.service.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

104 lines
3.0 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { B2BService } from "./b2b.service";
import { PrismaService } from "../prisma/prisma.service";
import { ForbiddenException, NotFoundException } from "@nestjs/common";
describe("B2BService", () => {
let service: B2BService;
const mockPrisma = {
b2BInquiry: {
create: jest.fn(),
findMany: jest.fn(),
findUnique: jest.fn(),
update: jest.fn(),
},
partnerAccount: {
findUnique: jest.fn(),
findMany: jest.fn(),
},
order: {
create: jest.fn(),
},
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [B2BService, { provide: PrismaService, useValue: mockPrisma }],
}).compile();
service = module.get<B2BService>(B2BService);
jest.clearAllMocks();
});
it("should be defined", () => {
expect(service).toBeDefined();
});
describe("updateInquiryStatus", () => {
it("should throw NotFoundException if inquiry does not exist", async () => {
mockPrisma.b2BInquiry.findUnique.mockResolvedValue(null);
await expect(
service.updateInquiryStatus("inq-404", { status: "REVIEWED" }),
).rejects.toThrow(NotFoundException);
});
it("should update inquiry status and adminNotes when found", async () => {
mockPrisma.b2BInquiry.findUnique.mockResolvedValue({ id: "inq-1" });
mockPrisma.b2BInquiry.update.mockResolvedValue({
id: "inq-1",
status: "APPROVED",
adminNotes: "مدارک تایید شد",
});
const res = await service.updateInquiryStatus("inq-1", {
status: "APPROVED",
adminNotes: "مدارک تایید شد",
});
expect(res.status).toBe("APPROVED");
});
});
describe("createWholesaleOrder", () => {
it("should throw ForbiddenException if user is not an active wholesale partner", async () => {
mockPrisma.partnerAccount.findUnique.mockResolvedValue({
userId: "u-1",
status: "PENDING",
});
await expect(
service.createWholesaleOrder("u-1", {
items: [{ productId: "prod-1", quantity: 20 }],
totalAmount: 5000000,
}),
).rejects.toThrow(ForbiddenException);
});
it("should create order when partner is active", async () => {
mockPrisma.partnerAccount.findUnique.mockResolvedValue({
userId: "u-partner",
status: "ACTIVE",
});
mockPrisma.order.create.mockResolvedValue({
id: "ord-wholesale-1",
status: "processing",
});
const res = await service.createWholesaleOrder("u-partner", {
items: [{ productId: "prod-1", quantity: 10 }],
totalAmount: 15000000,
});
expect(mockPrisma.order.create).toHaveBeenCalledWith({
data: expect.objectContaining({
userId: "u-partner",
totalAmount: 15000000,
status: "processing",
}),
include: { orderItems: true },
});
expect(res.id).toBe("ord-wholesale-1");
});
});
});