import { Test, TestingModule } from "@nestjs/testing"; import { ContactController } from "./contact.controller"; import { ContactService } from "./contact.service"; describe("ContactController", () => { let controller: ContactController; let service: ContactService; const mockContactService = { submitContactForm: jest.fn(), getContactInfo: jest.fn(), getAllSubmissions: jest.fn(), updateSubmissionStatus: jest.fn(), updateContactInfoItems: jest.fn(), }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [ContactController], providers: [ { provide: ContactService, useValue: mockContactService, }, ], }).compile(); controller = module.get(ContactController); service = module.get(ContactService); }); afterEach(() => { jest.clearAllMocks(); }); it("should be defined", () => { expect(controller).toBeDefined(); expect(service).toBeDefined(); }); describe("submitContact", () => { it("should submit a contact message successfully", async () => { const dto = { name: "رضا محمدی", phone: "09123456789", email: "reza@example.com", subject: "مشاوره مصرف مکمل کنین هیدروکس", message: "لطفا در مورد نحوه مصرف کانهیدروکس برای سگ روتوایلر راهنمایی کنید.", }; const expectedResponse = { success: true, message: "پیام شما با موفقیت ثبت شد و به‌زودی کارشناسان ما با شما تماس خواهند گرفت.", submissionId: "sub-12345", }; mockContactService.submitContactForm.mockResolvedValue(expectedResponse); const result = await controller.submitContact(dto); expect(service.submitContactForm).toHaveBeenCalledWith(dto); expect(result).toEqual(expectedResponse); }); }); describe("getContactInfo", () => { it("should return the configured contact info cards", async () => { const mockContactInfo = [ { id: "info-1", key: "branch_info", title: "اطلاعات نمایندگی", value: "تلفن‌های تماس ۰۲۱-۸۸۸۸۴۴۴۴", icon: "phone", order: 1, }, { id: "info-2", key: "email_info", title: "پست الکترونیک", value: "info@canina.ir", icon: "mail", order: 2, }, ]; mockContactService.getContactInfo.mockResolvedValue(mockContactInfo); const result = await controller.getContactInfo(); expect(service.getContactInfo).toHaveBeenCalledTimes(1); expect(result).toEqual(mockContactInfo); }); }); describe("getAllSubmissions", () => { it("should return paginated submissions with default page 1 and limit 20", async () => { const mockResult = { items: [ { id: "sub-1", name: "سارا احمدی", phone: "09901112233", status: "PENDING", createdAt: new Date(), }, ], total: 1, page: 1, limit: 20, totalPages: 1, }; mockContactService.getAllSubmissions.mockResolvedValue(mockResult); const result = await controller.getAllSubmissions(); expect(service.getAllSubmissions).toHaveBeenCalledWith(1, 20, undefined); expect(result).toEqual(mockResult); }); it("should parse string query parameters for page, limit, and status", async () => { const mockResult = { items: [], total: 0, page: 2, limit: 10, totalPages: 0, }; mockContactService.getAllSubmissions.mockResolvedValue(mockResult); const result = await controller.getAllSubmissions("2", "10", "ANSWERED"); expect(service.getAllSubmissions).toHaveBeenCalledWith(2, 10, "ANSWERED"); expect(result).toEqual(mockResult); }); }); describe("updateSubmissionStatus", () => { it("should update submission status and admin notes", async () => { const subId = "sub-1"; const updatePayload = { status: "RESOLVED", adminNotes: "با مشتری تماس گرفته شد و مشاوره ارائه گردید.", }; const updatedRecord = { id: subId, name: "علی کریمی", phone: "09121111111", status: "RESOLVED", adminNotes: "با مشتری تماس گرفته شد و مشاوره ارائه گردید.", updatedAt: new Date(), }; mockContactService.updateSubmissionStatus.mockResolvedValue( updatedRecord, ); const result = await controller.updateSubmissionStatus( subId, updatePayload, ); expect(service.updateSubmissionStatus).toHaveBeenCalledWith( subId, updatePayload.status, updatePayload.adminNotes, ); expect(result).toEqual(updatedRecord); }); }); describe("updateContactInfo", () => { it("should update contact info items list", async () => { const itemsPayload = { items: [ { key: "address_info", title: "نشانی جدید دفتر مرکزی", value: "تهران، خیابان ولیعصر", icon: "map-pin", order: 1, }, ], }; mockContactService.updateContactInfoItems.mockResolvedValue( itemsPayload.items, ); const result = await controller.updateContactInfo(itemsPayload); expect(service.updateContactInfoItems).toHaveBeenCalledWith( itemsPayload.items, ); expect(result).toEqual(itemsPayload.items); }); }); });