Files
canina/frontend/application/components/__tests__/FAQSection.test.tsx
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

146 lines
5.2 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 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) => <div {...props}>{children}</div>,
button: ({ children, ...props }: any) => <button {...props}>{children}</button>,
span: ({ children, ...props }: any) => <span {...props}>{children}</span>,
},
}));
vi.mock("../../lib/services/api", () => ({
default: {
get: vi.fn(),
post: vi.fn(),
},
}));
const mockSettings: Record<string, string> = {
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(<FAQSection />);
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(<FAQSection />);
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(<FAQSection />);
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(<FAQSection />);
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(<FAQSection />);
await waitFor(() => {
expect(container.firstChild).toBeNull();
});
});
});