import { fireEvent,render,screen,waitFor } from "@testing-library/react"; import { toast } from "react-hot-toast"; import { beforeEach,describe,expect,it,vi } from "vitest"; import api from "../services/api"; import WholesaleApplications from "./WholesaleApplications"; vi.mock("../services/api", () => ({ default: { get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), }, })); vi.mock("react-hot-toast", () => ({ toast: { success: vi.fn(), error: vi.fn(), }, })); describe("WholesaleApplications Page", () => { const mockRequests = [ { id: "req-1", fullName: "پت‌شاپ آریا", email: "info@ariapet.com", phoneNumber: "02188889999", mobile: "09123456789", role: "User", createdAt: "2025-01-15T10:30:00Z", }, { id: "req-2", fullName: "کلینیک دکتر راد", email: "dr.rad@clinic.ir", phoneNumber: "02122334455", mobile: "09351112233", role: "User_Wholesale", createdAt: "2025-01-10T14:20:00Z", }, { id: "req-3", fullName: "", email: "noname@store.com", phoneNumber: "02155667788", role: "User", createdAt: "2025-02-01T09:00:00Z", }, ]; beforeEach(() => { vi.clearAllMocks(); }); it("renders loading state initially and then displays applications list", async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: mockRequests }); render(); expect(screen.getByText("در حال بارگذاری درخواست‌ها...")).toBeInTheDocument(); await waitFor(() => { expect(screen.queryByText("در حال بارگذاری درخواست‌ها...")).not.toBeInTheDocument(); }); expect(screen.getByText("درخواست‌های ارتقا به همکار B2B")).toBeInTheDocument(); expect(screen.getByText("پت‌شاپ آریا")).toBeInTheDocument(); expect(screen.getByText("info@ariapet.com")).toBeInTheDocument(); expect(screen.getByText("کلینیک دکتر راد")).toBeInTheDocument(); expect(screen.getByText("dr.rad@clinic.ir")).toBeInTheDocument(); expect(screen.getAllByText("noname@store.com").length).toBeGreaterThan(0); expect(screen.getAllByText("در انتظار بررسی B2B").length).toBe(2); expect(screen.getByText("خریدار عمده تاییدشده")).toBeInTheDocument(); }); it("displays empty state when no requests are returned", async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: [] }); render(); await waitFor(() => { expect(screen.getByText("هیچ درخواستی در انتظار بررسی یافت نشد.")).toBeInTheDocument(); }); }); it("handles approve B2B flow with modal confirmation and refresh", async () => { vi.mocked(api.get) .mockResolvedValueOnce({ data: mockRequests }) .mockResolvedValueOnce({ data: [ { ...mockRequests[0], role: "User_Wholesale" }, mockRequests[1], mockRequests[2], ], }); vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true } }); render(); await waitFor(() => { expect(screen.getByText("پت‌شاپ آریا")).toBeInTheDocument(); }); const approveButtons = screen.getAllByRole("button", { name: /تایید و اعطای لایسنس B2B/i }); expect(approveButtons.length).toBe(2); // Only for req-1 and req-3 since req-2 is already User_Wholesale fireEvent.click(approveButtons[0]); expect(screen.getByText("تایید حساب همکار B2B")).toBeInTheDocument(); expect( screen.getByText("آیا از تایید حساب همکار و اعطای قیمت‌های عمده-۳۰٪ مطمئن هستید؟") ).toBeInTheDocument(); const confirmBtn = screen.getByRole("button", { name: "تایید و اعطا" }); fireEvent.click(confirmBtn); await waitFor(() => { expect(api.put).toHaveBeenCalledWith("/wholesale/approve/req-1"); expect(toast.success).toHaveBeenCalledWith( "کاربر با موفقیت به نقش خریدار عمده (User_Wholesale) ارتقا یافت." ); }); await waitFor(() => { expect(api.get).toHaveBeenCalledTimes(2); }); }); it("handles cancel on approve modal without calling api.put", async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: mockRequests }); render(); await waitFor(() => { expect(screen.getByText("پت‌شاپ آریا")).toBeInTheDocument(); }); const approveButtons = screen.getAllByRole("button", { name: /تایید و اعطای لایسنس B2B/i }); fireEvent.click(approveButtons[0]); expect(screen.getByText("تایید حساب همکار B2B")).toBeInTheDocument(); const cancelBtn = screen.getByRole("button", { name: "لغو" }); fireEvent.click(cancelBtn); expect(screen.queryByText("تایید حساب همکار B2B")).not.toBeInTheDocument(); expect(api.put).not.toHaveBeenCalled(); }); it("handles approve failure with error toast", async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: mockRequests }); vi.mocked(api.put).mockRejectedValueOnce(new Error("Network Error")); render(); await waitFor(() => { expect(screen.getByText("پت‌شاپ آریا")).toBeInTheDocument(); }); const approveButtons = screen.getAllByRole("button", { name: /تایید و اعطای لایسنس B2B/i }); fireEvent.click(approveButtons[0]); const confirmBtn = screen.getByRole("button", { name: "تایید و اعطا" }); fireEvent.click(confirmBtn); await waitFor(() => { expect(api.put).toHaveBeenCalledWith("/wholesale/approve/req-1"); expect(toast.error).toHaveBeenCalledWith("خطا در ارتقای نقش کاربر"); }); }); it("handles reject flow with confirmation modal and error fallback", async () => { vi.mocked(api.get) .mockResolvedValueOnce({ data: mockRequests }) .mockResolvedValueOnce({ data: [mockRequests[1], mockRequests[2]] }); vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true } }); render(); await waitFor(() => { expect(screen.getByText("پت‌شاپ آریا")).toBeInTheDocument(); }); const rejectButtons = screen.getAllByRole("button", { name: "رد درخواست" }); fireEvent.click(rejectButtons[0]); expect(screen.getByText("رد درخواست همکاری")).toBeInTheDocument(); expect(screen.getByText("آیا از رد درخواست همکاری مطمئن هستید؟")).toBeInTheDocument(); // Confirm reject const confirmRejectBtns = screen.getAllByRole("button", { name: "رد درخواست" }); const modalConfirmBtn = confirmRejectBtns[confirmRejectBtns.length - 1]; fireEvent.click(modalConfirmBtn); await waitFor(() => { expect(api.put).toHaveBeenCalledWith("/wholesale/reject/req-1"); expect(toast.success).toHaveBeenCalledWith("درخواست همکاری رد شد."); }); }); it("handles reject failure with error toast", async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: mockRequests }); vi.mocked(api.put).mockRejectedValueOnce(new Error("Server error")); render(); await waitFor(() => { expect(screen.getByText("پت‌شاپ آریا")).toBeInTheDocument(); }); const rejectButtons = screen.getAllByRole("button", { name: "رد درخواست" }); fireEvent.click(rejectButtons[0]); const confirmRejectBtns = screen.getAllByRole("button", { name: "رد درخواست" }); const modalConfirmBtn = confirmRejectBtns[confirmRejectBtns.length - 1]; fireEvent.click(modalConfirmBtn); await waitFor(() => { expect(api.put).toHaveBeenCalledWith("/wholesale/reject/req-1"); expect(toast.error).toHaveBeenCalledWith("خطا در رد درخواست همکاری"); }); }); });