85 lines
3.2 KiB
TypeScript
85 lines
3.2 KiB
TypeScript
import React from "react";
|
|
import { describe, it, expect, vi } from "vitest";
|
|
import { render, screen } from "@testing-library/react";
|
|
import ArticleContentRenderer, { parseContentWithProducts } from "../ArticleContentRenderer";
|
|
|
|
// 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
|
|
vi.mock("../../lib/store/cartStore", () => ({
|
|
useCartStore: (selector?: any) => {
|
|
const state = { items: [], addItem: vi.fn() };
|
|
return typeof selector === "function" ? selector(state) : state;
|
|
},
|
|
}));
|
|
|
|
const mockProducts = [
|
|
{
|
|
id: "prod-1",
|
|
slug: "canhydrox-gag",
|
|
nameFa: "قرص کانیهیدروکس GAG",
|
|
category: { name: "مفاصل" },
|
|
priceValue: 2450000,
|
|
},
|
|
{
|
|
id: "prod-2",
|
|
slug: "petvital-derm-caps",
|
|
nameFa: "کپسول درم پتویتال",
|
|
category: { name: "پوست و مو" },
|
|
priceValue: 1200000,
|
|
},
|
|
];
|
|
|
|
describe("ArticleContentRenderer and parseContentWithProducts", () => {
|
|
it("parses [product slug] markers in content into rich product items", () => {
|
|
const html = `
|
|
<p>درمان مشکلات حرکتی در نژادهای بزرگ:</p>
|
|
<p>[product canhydrox-gag]</p>
|
|
<p>همچنین برای درخشش مو:</p>
|
|
<p>[محصول petvital-derm-caps]</p>
|
|
`;
|
|
|
|
const parsed = parseContentWithProducts(html, mockProducts);
|
|
expect(parsed.length).toBe(4);
|
|
expect(parsed[0].type).toBe("html");
|
|
expect(parsed[1].type).toBe("product");
|
|
expect(parsed[1].product?.nameFa).toBe("قرص کانیهیدروکس GAG");
|
|
expect(parsed[2].type).toBe("html");
|
|
expect(parsed[3].type).toBe("product");
|
|
expect(parsed[3].product?.nameFa).toBe("کپسول درم پتویتال");
|
|
});
|
|
|
|
it("renders in-article product cards inside rendered HTML body", () => {
|
|
const html = `
|
|
<p>اگر سگ شما دچار درد مفاصل است، این مکمل را تهیه نمایید:</p>
|
|
<p>[product canhydrox-gag]</p>
|
|
<p>پس از ۲ هفته بهبود قابل مشاهده خواهد بود.</p>
|
|
`;
|
|
|
|
render(<ArticleContentRenderer contentHtml={html} relatedProducts={mockProducts} />);
|
|
|
|
expect(screen.getByText(/اگر سگ شما دچار درد مفاصل است/)).toBeInTheDocument();
|
|
expect(screen.getByTestId("article-product-card")).toBeInTheDocument();
|
|
expect(screen.getByText("قرص کانیهیدروکس GAG")).toBeInTheDocument();
|
|
expect(screen.getByText(/۲٬۴۵۰٬۰۰۰ تومان/)).toBeInTheDocument();
|
|
expect(screen.getByText(/پس از ۲ هفته بهبود قابل مشاهده خواهد بود/)).toBeInTheDocument();
|
|
});
|
|
|
|
it("handles data-product attribute markup as well", () => {
|
|
const html = `
|
|
<p>توصیه ویژه:</p>
|
|
<div data-product="canhydrox-gag"></div>
|
|
`;
|
|
|
|
render(<ArticleContentRenderer contentHtml={html} relatedProducts={mockProducts} />);
|
|
|
|
expect(screen.getByTestId("article-product-card")).toBeInTheDocument();
|
|
expect(screen.getByText("قرص کانیهیدروکس GAG")).toBeInTheDocument();
|
|
});
|
|
});
|