- 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.
301 lines
9.0 KiB
TypeScript
301 lines
9.0 KiB
TypeScript
"use client";
|
||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||
import { render, screen, fireEvent } from "@testing-library/react";
|
||
import React from "react";
|
||
import { ProductCard } from "../FeaturedProducts";
|
||
import { ArchiveProductCard } from "../ArchivePage";
|
||
import { Product } from "../../lib/data/products";
|
||
import { useCartStore } from "../../lib/store/cartStore";
|
||
import { usePetStore } from "../../lib/store/usePetStore";
|
||
import { useCatalogMode } from "../../lib/useCatalogMode";
|
||
import { toast } from "sonner";
|
||
|
||
const mockPush = vi.fn();
|
||
vi.mock("next/navigation", () => ({
|
||
useRouter: () => ({
|
||
push: mockPush,
|
||
}),
|
||
}));
|
||
|
||
vi.mock("sonner", () => ({
|
||
toast: {
|
||
success: vi.fn(),
|
||
error: vi.fn(),
|
||
},
|
||
}));
|
||
|
||
vi.mock("../../lib/store/settingsStore", () => ({
|
||
useSettingsStore: vi.fn((selector) => {
|
||
if (typeof selector === "function") {
|
||
return selector({ getText: (_key: string, def: string) => def });
|
||
}
|
||
return { getText: (_key: string, def: string) => def };
|
||
}),
|
||
}));
|
||
|
||
vi.mock("../../lib/store/cartStore", () => ({
|
||
useCartStore: Object.assign(
|
||
vi.fn(() => ({
|
||
addItem: vi.fn(),
|
||
items: [],
|
||
updateQuantity: vi.fn(),
|
||
})),
|
||
{
|
||
getState: () => ({
|
||
updateQuantity: vi.fn(),
|
||
addItem: vi.fn(),
|
||
items: [],
|
||
}),
|
||
}
|
||
),
|
||
}));
|
||
|
||
vi.mock("../../lib/store/usePetStore", () => ({
|
||
usePetStore: vi.fn(() => ({
|
||
getActivePet: () => null,
|
||
pets: [],
|
||
activePetId: null,
|
||
})),
|
||
}));
|
||
|
||
vi.mock("../../lib/useCatalogMode", () => ({
|
||
useCatalogMode: vi.fn(() => ({
|
||
showPrices: true,
|
||
allowCart: true,
|
||
showPreorderBtn: false,
|
||
})),
|
||
}));
|
||
|
||
const sampleProduct: Product = {
|
||
id: "prod-canina-1",
|
||
name: "Canhydrox GAG",
|
||
nameFa: "قرص کانهیدروکس گگ",
|
||
nameEn: "Canina Canhydrox GAG",
|
||
description: "مکمل تخصصی مفاصل و استخوان سگ",
|
||
shortDescription: "تقویت کننده تاندون و استخوان بندی سگ",
|
||
price: "۱,۲۵۰,۰۰۰ تومان",
|
||
priceValue: 1250000,
|
||
category: "joints",
|
||
image: "/images/canhydrox.png",
|
||
packageSize: "100",
|
||
suitableFor: "سگ",
|
||
symptoms: ["درد مفاصل", "لنگیدن"],
|
||
benefits: "تقویت بافت همبند و غضروف",
|
||
calculateDosage: vi.fn(),
|
||
};
|
||
|
||
describe("ProductCard Component (FeaturedProducts)", () => {
|
||
const mockAddItem = vi.fn();
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
vi.mocked(useCartStore).mockReturnValue({
|
||
addItem: mockAddItem,
|
||
items: [],
|
||
updateQuantity: vi.fn(),
|
||
} as any);
|
||
|
||
vi.mocked(usePetStore).mockReturnValue({
|
||
getActivePet: () => null,
|
||
pets: [],
|
||
activePetId: null,
|
||
} as any);
|
||
|
||
vi.mocked(useCatalogMode).mockReturnValue({
|
||
showPrices: true,
|
||
allowCart: true,
|
||
showPreorderBtn: false,
|
||
});
|
||
});
|
||
|
||
it("renders product Persian name, English name, and category badge", () => {
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
expect(screen.getByText("قرص کانهیدروکس گگ")).toBeInTheDocument();
|
||
expect(screen.getByText("Canina Canhydrox GAG")).toBeInTheDocument();
|
||
expect(screen.getByText("joints")).toBeInTheDocument();
|
||
expect(screen.getByText("۱,۲۵۰,۰۰۰ تومان")).toBeInTheDocument();
|
||
});
|
||
|
||
it("navigates to product detail page on card click", () => {
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
fireEvent.click(screen.getByText("قرص کانهیدروکس گگ"));
|
||
expect(mockPush).toHaveBeenCalledWith("/shop/prod-canina-1");
|
||
});
|
||
|
||
it("adds product to cart when shopping cart button is clicked", () => {
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
const cartBtn = screen.getByTitle("افزودن به سبد خرید");
|
||
fireEvent.click(cartBtn);
|
||
|
||
expect(mockAddItem).toHaveBeenCalledWith(sampleProduct, 1);
|
||
expect(toast.success).toHaveBeenCalledWith("قرص کانهیدروکس گگ به سبد خرید اضافه شد.");
|
||
});
|
||
|
||
it("renders pet compatibility alert badge when pet is not compatible with product species", () => {
|
||
vi.mocked(usePetStore).mockReturnValue({
|
||
getActivePet: () => ({
|
||
id: "pet-cat",
|
||
name: "میشا",
|
||
type: "گربه",
|
||
breed: "پرشین",
|
||
medicalConditions: [],
|
||
}),
|
||
pets: [],
|
||
activePetId: "pet-cat",
|
||
} as any);
|
||
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
expect(screen.getByText(/مخصوص سگ/)).toBeInTheDocument();
|
||
});
|
||
|
||
it("renders pet compatibility success recommendation when pet symptom matches product", () => {
|
||
vi.mocked(usePetStore).mockReturnValue({
|
||
getActivePet: () => ({
|
||
id: "pet-dog",
|
||
name: "ببری",
|
||
type: "سگ",
|
||
breed: "ژرمن",
|
||
medicalConditions: ["درد مفاصل"],
|
||
}),
|
||
pets: [],
|
||
activePetId: "pet-dog",
|
||
} as any);
|
||
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
expect(screen.getByText(/توصیه شده برای/)).toBeInTheDocument();
|
||
expect(screen.getByText(/ببری/)).toBeInTheDocument();
|
||
});
|
||
|
||
it("renders preorder button badge when showPreorderBtn is enabled", () => {
|
||
vi.mocked(useCatalogMode).mockReturnValue({
|
||
showPrices: true,
|
||
allowCart: false,
|
||
showPreorderBtn: true,
|
||
});
|
||
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
expect(screen.getByText("ثبت پیشخرید")).toBeInTheDocument();
|
||
});
|
||
|
||
it("hides price and displays fallback when showPrices is false", () => {
|
||
vi.mocked(useCatalogMode).mockReturnValue({
|
||
showPrices: false,
|
||
allowCart: false,
|
||
showPreorderBtn: false,
|
||
});
|
||
|
||
render(<ProductCard product={sampleProduct} />);
|
||
|
||
expect(screen.getByText("تماس بگیرید")).toBeInTheDocument();
|
||
expect(screen.queryByTitle('افزودن به سبد خرید')).not.toBeInTheDocument();
|
||
fireEvent.click(screen.getByText('تماس بگیرید'));
|
||
expect(mockAddItem).not.toHaveBeenCalled();
|
||
});
|
||
});
|
||
|
||
describe("ArchiveProductCard Component (ArchivePage)", () => {
|
||
const mockAddItem = vi.fn();
|
||
const mockUpdateQuantity = vi.fn();
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
(useCartStore as any).getState = () => ({
|
||
updateQuantity: mockUpdateQuantity,
|
||
addItem: mockAddItem,
|
||
items: [],
|
||
});
|
||
|
||
vi.mocked(useCartStore).mockImplementation(((selector?: any) => {
|
||
const state = {
|
||
addItem: mockAddItem,
|
||
items: [],
|
||
updateQuantity: mockUpdateQuantity,
|
||
};
|
||
if (typeof selector === "function") {
|
||
return selector(state);
|
||
}
|
||
return state;
|
||
}) as any);
|
||
|
||
vi.mocked(usePetStore).mockImplementation(((selector?: any) => {
|
||
const state = {
|
||
getActivePet: () => null,
|
||
pets: [],
|
||
activePetId: null,
|
||
};
|
||
if (typeof selector === "function") {
|
||
return selector(state);
|
||
}
|
||
return state;
|
||
}) as any);
|
||
|
||
vi.mocked(useCatalogMode).mockReturnValue({
|
||
showPrices: true,
|
||
allowCart: true,
|
||
showPreorderBtn: false,
|
||
});
|
||
});
|
||
|
||
it("renders catalog card with symptoms and add to cart button", () => {
|
||
render(<ArchiveProductCard product={sampleProduct} />);
|
||
|
||
expect(screen.getByText("قرص کانهیدروکس گگ")).toBeInTheDocument();
|
||
expect(screen.getByText("درد مفاصل")).toBeInTheDocument();
|
||
expect(screen.getByText("خرید")).toBeInTheDocument();
|
||
});
|
||
|
||
it("adds item to cart on clicking 'خرید' button", () => {
|
||
render(<ArchiveProductCard product={sampleProduct} />);
|
||
|
||
const buyBtn = screen.getByRole("button", { name: /خرید/ });
|
||
fireEvent.click(buyBtn);
|
||
|
||
expect(mockAddItem).toHaveBeenCalledWith(sampleProduct, 1);
|
||
expect(toast.success).toHaveBeenCalledWith(expect.stringContaining("به سبد خرید اضافه شد"));
|
||
});
|
||
|
||
it("renders out-of-stock badge when packageSize is 0", () => {
|
||
const outOfStockProduct = { ...sampleProduct, packageSize: "0" };
|
||
render(<ArchiveProductCard product={outOfStockProduct} />);
|
||
|
||
expect(screen.getByText("ناموجود")).toBeInTheDocument();
|
||
expect(screen.queryByRole('button', { name: 'خرید' })).not.toBeInTheDocument();
|
||
fireEvent.click(screen.getByText('ناموجود'));
|
||
expect(mockAddItem).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it("shows quantity counter and buttons when item is already in cart", () => {
|
||
vi.mocked(useCartStore).mockImplementation(((selector?: any) => {
|
||
const state = {
|
||
addItem: mockAddItem,
|
||
items: [{ product: sampleProduct, quantity: 2 }],
|
||
updateQuantity: mockUpdateQuantity,
|
||
};
|
||
if (typeof selector === "function") {
|
||
return selector(state);
|
||
}
|
||
return state;
|
||
}) as any);
|
||
|
||
render(<ArchiveProductCard product={sampleProduct} />);
|
||
|
||
const plusBtn = screen.getByRole("button", { name: "+" });
|
||
const minusBtn = screen.getByRole("button", { name: "-" });
|
||
|
||
expect(plusBtn).toBeInTheDocument();
|
||
expect(minusBtn).toBeInTheDocument();
|
||
|
||
fireEvent.click(plusBtn);
|
||
expect(mockUpdateQuantity).toHaveBeenCalledWith("prod-canina-1", 3);
|
||
|
||
fireEvent.click(minusBtn);
|
||
expect(mockUpdateQuantity).toHaveBeenCalledWith("prod-canina-1", 1);
|
||
});
|
||
});
|