"use client"; import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen, fireEvent, waitFor } from "@testing-library/react"; import React from "react"; import FAQSection from "../FAQSection"; import api from "../../lib/services/api"; // Mock motion/react vi.mock("motion/react", () => ({ AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}, motion: { div: ({ children, ...props }: any) =>
{children}
, button: ({ children, ...props }: any) => , span: ({ children, ...props }: any) => {children}, }, })); vi.mock("../../lib/services/api", () => ({ default: { get: vi.fn(), post: vi.fn(), }, })); const mockSettings: Record = { faq_badge: "پاسخ به سوالات پرتکرار", faq_title_1: "سوالات متداول", faq_title_2: "درباره محصولات و سفارش", faq_desc: "پاسخ به متداول‌ترین سوالات درباره استانداردها، دوز مصرفی، ارسال و اصالت مکمل‌های کنینا.", }; vi.mock("../../lib/store/settingsStore", () => ({ useSettingsStore: (selector: any) => selector({ getText: (key: string, fallback: string) => mockSettings[key] || fallback, }), })); describe("FAQSection Component", () => { beforeEach(() => { vi.clearAllMocks(); }); it("renders loading state initially while fetching FAQs", () => { vi.mocked(api.get).mockReturnValue(new Promise(() => {})); const { container } = render(); const skeletonItems = container.querySelectorAll(".animate-pulse"); expect(skeletonItems.length).toBe(3); }); it("returns null when no FAQs are found after loading", async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: [] }); const { container } = render(); await waitFor(() => { expect(container.firstChild).toBeNull(); }); }); it("renders FAQ list, opens the first FAQ by default, and includes JSON-LD schema", async () => { const mockFaqs = [ { id: "faq-1", question: "آیا محصولات کنینا اصل آلمان هستند؟", answer: "بله، تمامی محصولات با ضمانت اصالت ۱۰۰٪ از کمپانی کنینا آلمان وارد می‌شوند.", order: 1, }, { id: "faq-2", question: "چگونه دوز مناسب را برای سگ خود محاسبه کنیم؟", answer: "بر اساس جدول وزنی موجود روی برچسب قوطی یا بخش مشاوره هوشمند.", order: 2, }, ]; vi.mocked(api.get).mockResolvedValueOnce({ data: mockFaqs }); const { container } = render(); await waitFor(() => { expect(screen.getByText("آیا محصولات کنینا اصل آلمان هستند؟")).toBeInTheDocument(); expect(screen.getByText("چگونه دوز مناسب را برای سگ خود محاسبه کنیم؟")).toBeInTheDocument(); }); // Check first FAQ answer is visible (openIndex defaults to 0) expect( screen.getByText("بله، تمامی محصولات با ضمانت اصالت ۱۰۰٪ از کمپانی کنینا آلمان وارد می‌شوند.") ).toBeInTheDocument(); // Check JSON-LD script is rendered const script = container.querySelector('script[type="application/ld+json"]'); expect(script).not.toBeNull(); const schema = JSON.parse(script?.innerHTML || "{}"); expect(schema["@type"]).toBe("FAQPage"); expect(schema.mainEntity).toHaveLength(2); expect(schema.mainEntity[0].name).toBe("آیا محصولات کنینا اصل آلمان هستند؟"); }); it("toggles accordion when clicking questions", async () => { const mockFaqs = [ { id: "faq-1", question: "سوال اول؟", answer: "پاسخ اول", order: 1, }, { id: "faq-2", question: "سوال دوم؟", answer: "پاسخ دوم", order: 2, }, ]; vi.mocked(api.get).mockResolvedValueOnce({ data: mockFaqs }); render(); await waitFor(() => { expect(screen.getByText("سوال اول؟")).toBeInTheDocument(); }); // Question 1 is initially open expect(screen.getByText("پاسخ اول")).toBeInTheDocument(); expect(screen.queryByText("پاسخ دوم")).not.toBeInTheDocument(); // Click Question 2 to open it fireEvent.click(screen.getByText("سوال دوم؟")); expect(screen.queryByText("پاسخ اول")).not.toBeInTheDocument(); expect(screen.getByText("پاسخ دوم")).toBeInTheDocument(); // Click Question 2 again to close it fireEvent.click(screen.getByText("سوال دوم؟")); expect(screen.queryByText("پاسخ دوم")).not.toBeInTheDocument(); }); it("handles api errors gracefully and renders null", async () => { vi.mocked(api.get).mockRejectedValueOnce(new Error("Network Error")); const { container } = render(); await waitFor(() => { expect(container.firstChild).toBeNull(); }); }); });