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 }) => ( {alt} ), })); // 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 = `

درمان مشکلات حرکتی در نژادهای بزرگ:

[product canhydrox-gag]

همچنین برای درخشش مو:

[محصول petvital-derm-caps]

`; 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 = `

اگر سگ شما دچار درد مفاصل است، این مکمل را تهیه نمایید:

[product canhydrox-gag]

پس از ۲ هفته بهبود قابل مشاهده خواهد بود.

`; render(); 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 = `

توصیه ویژه:

`; render(); expect(screen.getByTestId("article-product-card")).toBeInTheDocument(); expect(screen.getByText("قرص کانی‌هیدروکس GAG")).toBeInTheDocument(); }); });