- 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.
135 lines
5.4 KiB
TypeScript
135 lines
5.4 KiB
TypeScript
"use client";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import React from "react";
|
|
import ContactFormClient from "../ContactFormClient";
|
|
import api from "../../lib/services/api";
|
|
|
|
vi.mock("../../lib/services/api", () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe("ContactFormClient Component", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("fetches and renders contact info items", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({
|
|
data: [
|
|
{ key: "phone", title: "شماره تماس", value: "021-12345678" },
|
|
{ key: "email", title: "پست الکترونیک", value: "support@canina.ir" },
|
|
],
|
|
});
|
|
|
|
render(<ContactFormClient />);
|
|
|
|
expect(api.get).toHaveBeenCalledWith("/contact/info");
|
|
await waitFor(() => {
|
|
expect(screen.getByText("021-12345678")).toBeInTheDocument();
|
|
expect(screen.getByText("support@canina.ir")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("shows error when required fields are empty on submit", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({ data: [] });
|
|
const { container } = render(<ContactFormClient />);
|
|
|
|
const form = container.querySelector("form");
|
|
expect(form).not.toBeNull();
|
|
fireEvent.submit(form!);
|
|
|
|
expect(
|
|
screen.getByText("لطفاً نام، شماره موبایل و متن پیام را وارد کنید.")
|
|
).toBeInTheDocument();
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("validates Iranian mobile phone number format", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({ data: [] });
|
|
const { container } = render(<ContactFormClient />);
|
|
|
|
const nameInput = screen.getByPlaceholderText("مثال: علی محمدی");
|
|
const phoneInput = screen.getByPlaceholderText("09123456789");
|
|
const messageInput = screen.getByPlaceholderText("متن درخواست یا پیام خود را بنویسید...");
|
|
const form = container.querySelector("form");
|
|
|
|
fireEvent.change(nameInput, { target: { value: "علی محمدی" } });
|
|
fireEvent.change(phoneInput, { target: { value: "123456" } }); // Invalid phone
|
|
fireEvent.change(messageInput, { target: { value: "سلام، من سوالی درباره مکمل مفاصل دارم." } });
|
|
|
|
fireEvent.submit(form!);
|
|
|
|
expect(
|
|
screen.getByText("شماره همراه وارد شده معتبر نیست. (مثال: ۰۹۱۲۳۴۵۶۷۸۹)")
|
|
).toBeInTheDocument();
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("successfully submits form and displays success state with reset button", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({ data: [] });
|
|
vi.mocked(api.post).mockResolvedValueOnce({ data: { success: true } });
|
|
|
|
const { container } = render(<ContactFormClient />);
|
|
|
|
const nameInput = screen.getByPlaceholderText("مثال: علی محمدی");
|
|
const phoneInput = screen.getByPlaceholderText("09123456789");
|
|
const subjectInput = screen.getByPlaceholderText("مثال: مشاوره تخصصی مکمل یا خرید عمده");
|
|
const messageInput = screen.getByPlaceholderText("متن درخواست یا پیام خود را بنویسید...");
|
|
const form = container.querySelector("form");
|
|
|
|
fireEvent.change(nameInput, { target: { value: "سارا حسینی" } });
|
|
fireEvent.change(phoneInput, { target: { value: "09129876543" } });
|
|
fireEvent.change(subjectInput, { target: { value: "درخواست نمایندگی" } });
|
|
fireEvent.change(messageInput, { target: { value: "با سلام، شرایط اخذ نمایندگی چیست؟" } });
|
|
|
|
fireEvent.submit(form!);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith("/contact", {
|
|
name: "سارا حسینی",
|
|
phone: "09129876543",
|
|
subject: "درخواست نمایندگی",
|
|
message: "با سلام، شرایط اخذ نمایندگی چیست؟",
|
|
});
|
|
});
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("پیام شما با موفقیت ثبت شد")).toBeInTheDocument();
|
|
});
|
|
|
|
// Reset and send another message
|
|
const sendAnotherBtn = screen.getByText("ارسال پیام جدید");
|
|
fireEvent.click(sendAnotherBtn);
|
|
|
|
expect(screen.getByTestId("contact-form-submit-btn")).toBeInTheDocument();
|
|
});
|
|
|
|
it("handles submission failure and displays error message", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({ data: [] });
|
|
vi.mocked(api.post).mockRejectedValueOnce({
|
|
response: { data: { message: "خطا در سرور، لطفاً بعداً تلاش کنید." } },
|
|
});
|
|
|
|
const { container } = render(<ContactFormClient />);
|
|
|
|
const nameInput = screen.getByPlaceholderText("مثال: علی محمدی");
|
|
const phoneInput = screen.getByPlaceholderText("09123456789");
|
|
const messageInput = screen.getByPlaceholderText("متن درخواست یا پیام خود را بنویسید...");
|
|
const form = container.querySelector("form");
|
|
|
|
fireEvent.change(nameInput, { target: { value: "سارا حسینی" } });
|
|
fireEvent.change(phoneInput, { target: { value: "09129876543" } });
|
|
fireEvent.change(messageInput, { target: { value: "متن پیام" } });
|
|
|
|
fireEvent.submit(form!);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("خطا در سرور، لطفاً بعداً تلاش کنید.")).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|