142 lines
4.3 KiB
TypeScript
142 lines
4.3 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { ContactService, CreateContactSubmissionDto } from "./contact.service";
|
|
import { PrismaService } from "../prisma/prisma.service";
|
|
import { SmsService } from "../common/services/sms.service";
|
|
|
|
describe("ContactService", () => {
|
|
let service: ContactService;
|
|
|
|
const mockPrisma = {
|
|
contactSubmission: {
|
|
create: jest.fn(),
|
|
findMany: jest.fn(),
|
|
count: jest.fn(),
|
|
update: jest.fn(),
|
|
},
|
|
setting: {
|
|
findMany: jest.fn(),
|
|
},
|
|
};
|
|
|
|
const mockSmsService = {
|
|
triggerEvent: jest.fn().mockResolvedValue(true),
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
ContactService,
|
|
{
|
|
provide: PrismaService,
|
|
useValue: mockPrisma,
|
|
},
|
|
{
|
|
provide: SmsService,
|
|
useValue: mockSmsService,
|
|
},
|
|
],
|
|
}).compile();
|
|
|
|
service = module.get<ContactService>(ContactService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("submitContactForm", () => {
|
|
it("should create contact submission and trigger SMS event", async () => {
|
|
const dto: CreateContactSubmissionDto = {
|
|
name: "علی حسینی",
|
|
phone: "09121112233",
|
|
message: "درخواست مشاوره مکمل سگ",
|
|
};
|
|
mockPrisma.contactSubmission.create.mockResolvedValue({
|
|
id: "sub-1",
|
|
...dto,
|
|
});
|
|
|
|
const result = await service.submitContactForm(dto);
|
|
|
|
expect(mockPrisma.contactSubmission.create).toHaveBeenCalledWith({
|
|
data: {
|
|
name: dto.name,
|
|
phone: dto.phone,
|
|
email: undefined,
|
|
subject: undefined,
|
|
message: dto.message,
|
|
},
|
|
});
|
|
expect(mockSmsService.triggerEvent).toHaveBeenCalledWith(
|
|
"CONTACT_SUBMITTED",
|
|
expect.objectContaining({
|
|
customerName: dto.name,
|
|
customerPhone: dto.phone,
|
|
}),
|
|
);
|
|
expect(result.success).toBe(true);
|
|
expect(result.submissionId).toBe("sub-1");
|
|
});
|
|
});
|
|
|
|
describe("getContactInfo", () => {
|
|
it("should return contact items from canonical settings", async () => {
|
|
mockPrisma.setting.findMany.mockResolvedValue([
|
|
{ key: "CONTACT_PHONE", value: "021-12345678" },
|
|
{ key: "CONTACT_EMAIL", value: "support@example.test" },
|
|
{ key: "CONTACT_ADDRESS", value: "آدرس تنظیمشده" },
|
|
]);
|
|
|
|
const result = await service.getContactInfo();
|
|
expect(result).toEqual([
|
|
{ key: "phone", title: "تلفنهای تماس", value: "021-12345678", icon: "phone", order: 1 },
|
|
{ key: "email", title: "پست الکترونیک", value: "support@example.test", icon: "mail", order: 2 },
|
|
{ key: "address", title: "نشانی دفتر مرکزی", value: "آدرس تنظیمشده", icon: "map-pin", order: 3 },
|
|
]);
|
|
expect(mockPrisma.setting.findMany).toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not return hardcoded contact values when settings are empty", async () => {
|
|
mockPrisma.setting.findMany.mockResolvedValue([]);
|
|
expect(await service.getContactInfo()).toEqual([]);
|
|
});
|
|
|
|
});
|
|
|
|
describe("getAllSubmissions", () => {
|
|
it("should return paginated submissions", async () => {
|
|
mockPrisma.contactSubmission.findMany.mockResolvedValue([
|
|
{ id: "sub-1" },
|
|
]);
|
|
mockPrisma.contactSubmission.count.mockResolvedValue(1);
|
|
|
|
const result = await service.getAllSubmissions(1, 10);
|
|
expect(result.total).toBe(1);
|
|
expect(result.items).toHaveLength(1);
|
|
expect(result.totalPages).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe("updateSubmissionStatus", () => {
|
|
it("should update submission status and admin notes", async () => {
|
|
mockPrisma.contactSubmission.update.mockResolvedValue({
|
|
id: "sub-1",
|
|
status: "RESOLVED",
|
|
adminNotes: "تماس گرفته شد",
|
|
});
|
|
|
|
const result = await service.updateSubmissionStatus(
|
|
"sub-1",
|
|
"RESOLVED",
|
|
"تماس گرفته شد",
|
|
);
|
|
expect(mockPrisma.contactSubmission.update).toHaveBeenCalledWith({
|
|
where: { id: "sub-1" },
|
|
data: { status: "RESOLVED", adminNotes: "تماس گرفته شد" },
|
|
});
|
|
expect(result.status).toBe("RESOLVED");
|
|
});
|
|
});
|
|
});
|