157 lines
6.9 KiB
TypeScript
157 lines
6.9 KiB
TypeScript
import React from "react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import B2BLandingClient from "../B2BLandingClient";
|
|
import api from "../../lib/services/api";
|
|
import { toast } from "sonner";
|
|
|
|
// Mock api
|
|
vi.mock("../../lib/services/api", () => ({
|
|
default: {
|
|
post: vi.fn(),
|
|
get: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock sonner toast
|
|
vi.mock("sonner", () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock settingsStore
|
|
vi.mock("../../lib/store/settingsStore", () => ({
|
|
useSettingsStore: (selector: any) =>
|
|
selector({
|
|
getText: (key: string, fallback: string) => {
|
|
if (key === "contact_phone") return "۰۹۲۱۱۲۳۱۵۱۷";
|
|
if (key === "B2B_DISCOUNT_PERCENT") return "۳۵";
|
|
return fallback;
|
|
},
|
|
}),
|
|
}));
|
|
|
|
describe("B2BLandingClient Component", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("renders B2B hero section, partner benefits, and contact phone", () => {
|
|
render(<B2BLandingClient />);
|
|
|
|
expect(screen.getByText(/تأمین عمده و همکاری تجاری مکملهای/)).toBeInTheDocument();
|
|
expect(screen.getByText("کنینا آلمان")).toBeInTheDocument();
|
|
expect(screen.getByText(/پرتال رسمی همکاران تجاری، کلینیکها و پتشاپها/)).toBeInTheDocument();
|
|
expect(screen.getByText("تا ۳۵٪ تخفیف")).toBeInTheDocument();
|
|
expect(screen.getAllByText("۰۹۲۱۱۲۳۱۵۱۷").length).toBeGreaterThan(0);
|
|
expect(screen.getByText("چرا کلینیکها و پتشاپها کنینا را انتخاب میکنند؟")).toBeInTheDocument();
|
|
expect(screen.getByText("اصالت قطعی آلمان")).toBeInTheDocument();
|
|
expect(screen.getByText("حاشیه سود رقابتی")).toBeInTheDocument();
|
|
expect(screen.getByText("ارسال سریع و مطمئن")).toBeInTheDocument();
|
|
expect(screen.getByText("پشتیبانی علمی و بالینی")).toBeInTheDocument();
|
|
});
|
|
|
|
it("toggles FAQ accordion items when clicked", () => {
|
|
render(<B2BLandingClient />);
|
|
|
|
const faqQuestion = screen.getByText("حداقل مبلغ و تعداد برای سفارش عمده B2B چقدر است؟");
|
|
expect(faqQuestion).toBeInTheDocument();
|
|
|
|
// Answer should not be visible initially
|
|
expect(
|
|
screen.queryByText(/برای اولین سفارش همکاران گرامی، حداقل سبد خرید با تخفیف همکار معادل ۵ میلیون تومان تعریف شده/)
|
|
).not.toBeInTheDocument();
|
|
|
|
// Click question button
|
|
fireEvent.click(faqQuestion);
|
|
expect(
|
|
screen.getByText(/برای اولین سفارش همکاران گرامی، حداقل سبد خرید با تخفیف همکار معادل ۵ میلیون تومان تعریف شده/)
|
|
).toBeInTheDocument();
|
|
|
|
// Click again to close
|
|
fireEvent.click(faqQuestion);
|
|
expect(
|
|
screen.queryByText(/برای اولین سفارش همکاران گرامی، حداقل سبد خرید با تخفیف همکار معادل ۵ میلیون تومان تعریف شده/)
|
|
).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("validates required fields and shows toast error when businessName or phone is missing", async () => {
|
|
const { container } = render(<B2BLandingClient />);
|
|
|
|
const form = container.querySelector("form");
|
|
expect(form).not.toBeNull();
|
|
fireEvent.submit(form!);
|
|
|
|
expect(toast.error).toHaveBeenCalledWith("لطفاً نام مجموعه و شماره تماس را وارد کنید.");
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("submits B2B inquiry form successfully and displays confirmation view", async () => {
|
|
vi.mocked(api.post).mockResolvedValueOnce({ data: { success: true } });
|
|
|
|
render(<B2BLandingClient />);
|
|
|
|
const businessNameInput = screen.getByPlaceholderText("مثلاً کلینیک تخصصی دکتر البرزی");
|
|
const businessTypeSelect = screen.getByRole("combobox");
|
|
const contactNameInput = screen.getByPlaceholderText("نام و نام خانوادگی");
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
const cityInput = screen.getByPlaceholderText("مثلاً تهران / اصفهان");
|
|
const notesInput = screen.getByPlaceholderText(
|
|
"در صورت داشتن نیاز دارویی خاص یا حجم تخمینی سفارش، در اینجا درج فرمایید..."
|
|
);
|
|
|
|
fireEvent.change(businessNameInput, { target: { value: "کلینیک تخصصی آریا" } });
|
|
fireEvent.change(businessTypeSelect, { target: { value: "بیمارستان دامپزشکی" } });
|
|
fireEvent.change(contactNameInput, { target: { value: "دکتر حسینی" } });
|
|
fireEvent.change(phoneInput, { target: { value: "09121112233" } });
|
|
fireEvent.change(cityInput, { target: { value: "تهران" } });
|
|
fireEvent.change(notesInput, { target: { value: "سفارش اولیه مکملهای حرکتی و کلسیم" } });
|
|
|
|
const submitBtn = screen.getByRole("button", { name: /ثبت درخواست و فعالسازی حساب همکار/i });
|
|
fireEvent.click(submitBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith("/b2b/inquire", expect.objectContaining({
|
|
companyName: "کلینیک تخصصی آریا",
|
|
contactName: "دکتر حسینی",
|
|
phone: "09121112233",
|
|
businessType: "بیمارستان دامپزشکی (شهر: تهران)",
|
|
message: "سفارش اولیه مکملهای حرکتی و کلسیم",
|
|
}));
|
|
});
|
|
|
|
await waitFor(() => {
|
|
expect(toast.success).toHaveBeenCalledWith(
|
|
expect.stringContaining("درخواست همکاری تجاری شما با موفقیت ثبت شد")
|
|
);
|
|
expect(screen.getByText("درخواست شما با موفقیت ثبت شد")).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText(/کارشناسان فروش سازمانی کنینا ایران جهت احراز هویت/)
|
|
).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("handles submission failure and shows error toast", async () => {
|
|
vi.mocked(api.post).mockRejectedValueOnce(new Error("Server error"));
|
|
|
|
render(<B2BLandingClient />);
|
|
|
|
const businessNameInput = screen.getByPlaceholderText("مثلاً کلینیک تخصصی دکتر البرزی");
|
|
const phoneInput = screen.getByPlaceholderText("۰۹۱۲۳۴۵۶۷۸۹");
|
|
|
|
fireEvent.change(businessNameInput, { target: { value: "پتشاپ امید" } });
|
|
fireEvent.change(phoneInput, { target: { value: "09351234567" } });
|
|
|
|
const submitBtn = screen.getByRole("button", { name: /ثبت درخواست و فعالسازی حساب همکار/i });
|
|
fireEvent.click(submitBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(toast.error).toHaveBeenCalledWith(
|
|
"خطایی در ثبت درخواست رخ داد. لطفاً با پشتیبانی تماس بگیرید."
|
|
);
|
|
});
|
|
});
|
|
});
|