102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
import React from "react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent } from "@testing-library/react";
|
|
import ArticleProductCard from "../ArticleProductCard";
|
|
import { useCartStore } from "../../lib/store/cartStore";
|
|
|
|
// 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 useCartStore
|
|
const addItemMock = vi.fn();
|
|
vi.mock("../../lib/store/cartStore", () => {
|
|
const mockStore = (selector?: any) => {
|
|
const state = {
|
|
items: [],
|
|
addItem: addItemMock,
|
|
};
|
|
return typeof selector === "function" ? selector(state) : state;
|
|
};
|
|
(mockStore as any).getState = () => ({
|
|
items: [],
|
|
addItem: addItemMock,
|
|
});
|
|
return {
|
|
useCartStore: mockStore,
|
|
};
|
|
});
|
|
|
|
const mockProduct = {
|
|
id: "prod-canhydrox-1",
|
|
slug: "canhydrox-gag",
|
|
nameFa: "قرص کانیهیدروکس GAG کنینا",
|
|
nameEn: "Canhydrox GAG Tablets",
|
|
description: "ترمیمکننده قوی مفاصل، استخوانها و رباطهای سگ با صدف لبسبز اصل آلمان.",
|
|
category: { name: "مفاصل و استخوان" },
|
|
imageUrl: "/assets/images/canhydrox.png",
|
|
priceValue: 2450000,
|
|
stock: 15,
|
|
stockStatus: "IN_STOCK",
|
|
};
|
|
|
|
describe("ArticleProductCard Component (CAT-P2-10)", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("renders product title, price, category, German origin badge, and thumbnail", () => {
|
|
render(<ArticleProductCard product={mockProduct} />);
|
|
|
|
expect(screen.getByText("قرص کانیهیدروکس GAG کنینا")).toBeInTheDocument();
|
|
expect(screen.getByText("Canhydrox GAG Tablets")).toBeInTheDocument();
|
|
expect(screen.getByText("مفاصل و استخوان")).toBeInTheDocument();
|
|
expect(screen.getByText("اصل آلمان")).toBeInTheDocument();
|
|
expect(screen.getByText("ضمانت ۱۰۰٪ اصالت آلمان")).toBeInTheDocument();
|
|
expect(screen.getByText(/۲٬۴۵۰٬۰۰۰ تومان/)).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: "مشاهده مشخصات" })).toHaveAttribute(
|
|
"href",
|
|
"/shop/canhydrox-gag"
|
|
);
|
|
});
|
|
|
|
it("triggers direct add to cart when buying button is clicked", () => {
|
|
render(<ArticleProductCard product={mockProduct} />);
|
|
|
|
const buyBtn = screen.getByRole("button", { name: /افزودن به سبد خرید|خرید مستقیم/ });
|
|
expect(buyBtn).toBeInTheDocument();
|
|
|
|
fireEvent.click(buyBtn);
|
|
|
|
expect(addItemMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
id: "prod-canhydrox-1",
|
|
slug: "canhydrox-gag",
|
|
nameFa: "قرص کانیهیدروکس GAG کنینا",
|
|
priceValue: 2450000,
|
|
}),
|
|
1
|
|
);
|
|
|
|
// Feedback state
|
|
expect(screen.getByText("به سبد اضافه شد")).toBeInTheDocument();
|
|
});
|
|
|
|
it("disables add to cart button when product is out of stock", () => {
|
|
const outOfStockProduct = {
|
|
...mockProduct,
|
|
stock: 0,
|
|
stockStatus: "OUT_OF_STOCK",
|
|
};
|
|
|
|
render(<ArticleProductCard product={outOfStockProduct} />);
|
|
|
|
const btn = screen.getByRole("button", { name: /ناموجود/ });
|
|
expect(btn).toBeDisabled();
|
|
expect(screen.getByText("ناموجود")).toBeInTheDocument();
|
|
});
|
|
});
|