- 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.
180 lines
6.3 KiB
TypeScript
180 lines
6.3 KiB
TypeScript
import React from "react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import VetGallery from "../VetGallery";
|
|
import { videoService } from "../../lib/services/videoService";
|
|
|
|
// Mock motion/react
|
|
vi.mock("motion/react", () => ({
|
|
AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
|
motion: {
|
|
button: ({ children, onClick, className, ...props }: any) => (
|
|
<button onClick={onClick} className={className} {...props}>
|
|
{children}
|
|
</button>
|
|
),
|
|
div: ({ children, ...props }: any) => <div {...props}>{children}</div>,
|
|
},
|
|
}));
|
|
|
|
// Mock SafeImage
|
|
vi.mock("../SafeImage", () => ({
|
|
default: ({ src, alt, className }: { src: string; alt: string; className?: string }) => (
|
|
<img src={src} alt={alt} className={className} data-testid="safe-image" />
|
|
),
|
|
}));
|
|
|
|
// Mock VideoModalPlayer
|
|
vi.mock("../VideoModalPlayer", () => ({
|
|
default: ({ isOpen, onClose, video }: { isOpen: boolean; onClose: () => void; video: any }) => (
|
|
isOpen && video ? (
|
|
<div data-testid="video-modal-player">
|
|
<span>مدال ویدیو: {video.title}</span>
|
|
<span>پزشک: {video.doctor}</span>
|
|
<button onClick={onClose} data-testid="close-video-modal">
|
|
بستن مدال
|
|
</button>
|
|
</div>
|
|
) : null
|
|
),
|
|
}));
|
|
|
|
// Mock settingsStore
|
|
vi.mock("../../lib/store/settingsStore", () => ({
|
|
useSettingsStore: (selector: any) =>
|
|
selector({
|
|
getText: (key: string, fallback: string) => fallback,
|
|
}),
|
|
}));
|
|
|
|
// Mock videoService
|
|
vi.mock("../../lib/services/videoService", () => ({
|
|
videoService: {
|
|
getVideos: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
const mockVideos = [
|
|
{
|
|
id: "v-1",
|
|
title: "نحوه آمادهسازی کانیهیدروکس GAG",
|
|
doctor: "دکتر کلاوس هنینگ",
|
|
duration: "۰۱:۴۵",
|
|
thumbnail: "https://example.com/thumb1.jpg",
|
|
videoUrl: "https://example.com/video1.mp4",
|
|
description: "آموزش گام به گام آمادهسازی پودر Canhydrox GAG",
|
|
},
|
|
{
|
|
id: "v-2",
|
|
title: "اهمیت صدف لبسبز برای غضروفها",
|
|
doctor: "دکتر النا اشمیت",
|
|
duration: "۰۲:۲۰",
|
|
thumbnail: "https://example.com/thumb2.jpg",
|
|
videoUrl: "https://example.com/video2.mp4",
|
|
description: "نقش صدف لبسبز در سلامت مفصل سگ و گربه",
|
|
},
|
|
];
|
|
|
|
describe("VetGallery Component", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("renders nothing when API returns no videos", async () => {
|
|
vi.mocked(videoService.getVideos).mockResolvedValueOnce([]);
|
|
|
|
const { container } = render(<VetGallery />);
|
|
|
|
await waitFor(() => {
|
|
expect(videoService.getVideos).toHaveBeenCalledWith({ featured: true, limit: 3 });
|
|
});
|
|
|
|
await waitFor(() => {
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
});
|
|
|
|
it("handles API rejection gracefully by rendering null", async () => {
|
|
vi.mocked(videoService.getVideos).mockRejectedValueOnce(new Error("Network Error"));
|
|
|
|
const { container } = render(<VetGallery />);
|
|
|
|
await waitFor(() => {
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
});
|
|
|
|
it("renders header badge, title, link, and video list when videos are fetched", async () => {
|
|
vi.mocked(videoService.getVideos).mockResolvedValueOnce(mockVideos);
|
|
|
|
render(<VetGallery />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("مشاوره ویدئویی")).toBeInTheDocument();
|
|
expect(screen.getByText("دوست دامپزشک پت شما")).toBeInTheDocument();
|
|
expect(screen.getByText("نحوه آمادهسازی کانیهیدروکس GAG")).toBeInTheDocument();
|
|
expect(screen.getByText("دکتر کلاوس هنینگ")).toBeInTheDocument();
|
|
expect(screen.getByText("۰۱:۴۵")).toBeInTheDocument();
|
|
expect(screen.getByText("اهمیت صدف لبسبز برای غضروفها")).toBeInTheDocument();
|
|
expect(screen.getByText("دکتر النا اشمیت")).toBeInTheDocument();
|
|
expect(screen.getByText("۰۲:۲۰")).toBeInTheDocument();
|
|
});
|
|
|
|
const viewAllLink = screen.getByRole("link", { name: /مشاهده همه ویدئوها/ });
|
|
expect(viewAllLink).toHaveAttribute("href", "/videos");
|
|
});
|
|
|
|
it("opens video player modal when a video item card is clicked and closes it upon request", async () => {
|
|
vi.mocked(videoService.getVideos).mockResolvedValueOnce(mockVideos);
|
|
|
|
render(<VetGallery />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("نحوه آمادهسازی کانیهیدروکس GAG")).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.queryByTestId("video-modal-player")).not.toBeInTheDocument();
|
|
|
|
// Click first video card
|
|
const firstVideoCard = screen.getByText("نحوه آمادهسازی کانیهیدروکس GAG").closest("button");
|
|
expect(firstVideoCard).not.toBeNull();
|
|
fireEvent.click(firstVideoCard!);
|
|
|
|
// Modal should now be open with details
|
|
await waitFor(() => {
|
|
expect(screen.getByTestId("video-modal-player")).toBeInTheDocument();
|
|
expect(screen.getByText("مدال ویدیو: نحوه آمادهسازی کانیهیدروکس GAG")).toBeInTheDocument();
|
|
expect(screen.getByText("پزشک: دکتر کلاوس هنینگ")).toBeInTheDocument();
|
|
});
|
|
|
|
// Close modal
|
|
const closeBtn = screen.getByTestId("close-video-modal");
|
|
fireEvent.click(closeBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.queryByTestId("video-modal-player")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("shows fallback doctor name when doctor field is empty", async () => {
|
|
const videoWithoutDoctor = [
|
|
{
|
|
id: "v-3",
|
|
title: "نکات نگهداری در فصل تابستان",
|
|
doctor: "",
|
|
duration: "۰۳:۰۰",
|
|
thumbnail: "https://example.com/thumb3.jpg",
|
|
videoUrl: "https://example.com/video3.mp4",
|
|
},
|
|
];
|
|
vi.mocked(videoService.getVideos).mockResolvedValueOnce(videoWithoutDoctor as any);
|
|
|
|
render(<VetGallery />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("نکات نگهداری در فصل تابستان")).toBeInTheDocument();
|
|
expect(screen.getByText("کادر علمی کنینا")).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|