- 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.
281 lines
9.6 KiB
TypeScript
281 lines
9.6 KiB
TypeScript
import { fireEvent,render,screen,waitFor } from "@testing-library/react";
|
|
import { toast } from "react-hot-toast";
|
|
import { MemoryRouter } from "react-router-dom";
|
|
import { beforeEach,describe,expect,it,vi } from "vitest";
|
|
import api from "../services/api";
|
|
import Wiki from "./Wiki";
|
|
|
|
vi.mock("../services/api", () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
put: vi.fn(),
|
|
delete: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("react-hot-toast", () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe("Wiki Page Component", () => {
|
|
const mockTerms = [
|
|
{
|
|
key: "glucosamine",
|
|
term: "گلوکوزامین",
|
|
definition: "ماده مغذی جهت بازسازی غضروفها و مفاصل در سگها و گربهها.",
|
|
relatedProducts: ["prod-1"],
|
|
wikiId: "general",
|
|
metaTitle: "گلوکوزامین برای مفاصل سگ",
|
|
metaDescription: "توضیحات گلوکوزامین",
|
|
keywords: "مفصل, غضروف, سگ",
|
|
},
|
|
{
|
|
key: "calcium-hydroxyapatite",
|
|
term: "هیدروکسیآپاتیت کلسیم",
|
|
definition: "ترکیب آلی کلسیم برای رشد استخوان و دندانهای حیوانات در حال رشد.",
|
|
relatedProducts: [],
|
|
wikiId: "general",
|
|
metaTitle: "هیدروکسی آپاتیت",
|
|
metaDescription: "",
|
|
keywords: "کلسیم, استخوان",
|
|
},
|
|
];
|
|
|
|
const mockProducts = [
|
|
{ id: "prod-1", nameFa: "کانهیدروکس گگ Canina", artNo: "ART-101" },
|
|
{ id: "prod-2", nameFa: "قرص کلسیم بیولوژی کانیلت", artNo: "ART-102" },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(api.get).mockImplementation((url: string) => {
|
|
if (url === "/admin/wiki") {
|
|
return Promise.resolve({
|
|
data: {
|
|
data: mockTerms,
|
|
meta: { total: 2, page: 1, lastPage: 1 },
|
|
},
|
|
});
|
|
}
|
|
if (url === "/admin/products") {
|
|
return Promise.resolve({
|
|
data: {
|
|
data: mockProducts,
|
|
},
|
|
});
|
|
}
|
|
return Promise.resolve({ data: {} });
|
|
});
|
|
});
|
|
|
|
const renderComponent = (initialEntries = ["/wiki"]) =>
|
|
render(
|
|
<MemoryRouter initialEntries={initialEntries}>
|
|
<Wiki />
|
|
</MemoryRouter>
|
|
);
|
|
|
|
it("renders terms table and search bar properly", async () => {
|
|
renderComponent();
|
|
|
|
expect(screen.getByText("دانشنامه کنینا (Wiki)")).toBeInTheDocument();
|
|
expect(screen.getByPlaceholderText("جستجو در اصطلاحات...")).toBeInTheDocument();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("گلوکوزامین")).toBeInTheDocument();
|
|
expect(screen.getByText("glucosamine")).toBeInTheDocument();
|
|
expect(screen.getByText("هیدروکسیآپاتیت کلسیم")).toBeInTheDocument();
|
|
expect(screen.getByText("calcium-hydroxyapatite")).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.getByText("ماده مغذی جهت بازسازی غضروفها و مفاصل در سگها و گربهها.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("filters terms by searching with debounced api call", async () => {
|
|
renderComponent();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("گلوکوزامین")).toBeInTheDocument();
|
|
});
|
|
|
|
const searchInput = screen.getByPlaceholderText("جستجو در اصطلاحات...");
|
|
fireEvent.change(searchInput, { target: { value: "کلسیم" } });
|
|
|
|
await waitFor(
|
|
() => {
|
|
expect(api.get).toHaveBeenCalledWith("/admin/wiki", {
|
|
params: expect.objectContaining({ search: "کلسیم" }),
|
|
});
|
|
},
|
|
{ timeout: 1500 }
|
|
);
|
|
});
|
|
|
|
it("opens create modal, adds related products and saves new term", async () => {
|
|
vi.mocked(api.post).mockResolvedValueOnce({ data: { success: true } });
|
|
|
|
renderComponent();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("گلوکوزامین")).toBeInTheDocument();
|
|
});
|
|
|
|
const createButton = screen.getByRole("button", { name: /مفهوم جدید/i });
|
|
fireEvent.click(createButton);
|
|
|
|
expect(screen.getByText("افزودن مفهوم جدید")).toBeInTheDocument();
|
|
|
|
const form = document.querySelector("#wikiForm");
|
|
expect(form).toBeInTheDocument();
|
|
|
|
const inputs = form!.querySelectorAll("input[type='text']");
|
|
const textarea = form!.querySelector("textarea");
|
|
|
|
// inputs[0] is key, inputs[1] is term
|
|
fireEvent.change(inputs[0], { target: { value: "omega-3" } });
|
|
fireEvent.change(inputs[1], { target: { value: "امگا ۳" } });
|
|
fireEvent.change(textarea!, { target: { value: "اسیدهای چرب ضروری برای پوست و مو" } });
|
|
|
|
// Select product
|
|
const productSelect = screen.getByRole("combobox");
|
|
fireEvent.change(productSelect, { target: { value: "prod-2" } });
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("قرص کلسیم بیولوژی کانیلت")).toBeInTheDocument();
|
|
});
|
|
|
|
const saveButton = screen.getByRole("button", { name: "ذخیره" });
|
|
fireEvent.click(saveButton);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith(
|
|
"/admin/wiki",
|
|
expect.objectContaining({
|
|
key: "omega-3",
|
|
term: "امگا ۳",
|
|
definition: "اسیدهای چرب ضروری برای پوست و مو",
|
|
relatedProducts: ["prod-2"],
|
|
wikiId: "general",
|
|
})
|
|
);
|
|
expect(toast.success).toHaveBeenCalledWith("اصطلاح علمی جدید با موفقیت ایجاد شد");
|
|
});
|
|
});
|
|
|
|
it("displays error toast when term save fails", async () => {
|
|
vi.mocked(api.post).mockRejectedValueOnce(new Error("Duplicate key"));
|
|
|
|
renderComponent();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("گلوکوزامین")).toBeInTheDocument();
|
|
});
|
|
|
|
const createButton = screen.getByRole("button", { name: /مفهوم جدید/i });
|
|
fireEvent.click(createButton);
|
|
|
|
const form = document.querySelector("#wikiForm");
|
|
const inputs = form!.querySelectorAll("input[type='text']");
|
|
const textarea = form!.querySelector("textarea");
|
|
|
|
fireEvent.change(inputs[0], { target: { value: "glucosamine" } });
|
|
fireEvent.change(inputs[1], { target: { value: "گلوکوزامین" } });
|
|
fireEvent.change(textarea!, { target: { value: "تکراری" } });
|
|
|
|
const saveButton = screen.getByRole("button", { name: "ذخیره" });
|
|
fireEvent.click(saveButton);
|
|
|
|
await waitFor(() => {
|
|
expect(toast.error).toHaveBeenCalledWith(
|
|
"خطا در ذخیره اصطلاح علمی. ممکن است کلید تکراری باشد."
|
|
);
|
|
});
|
|
});
|
|
|
|
it("edits an existing scientific term and updates values", async () => {
|
|
vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true } });
|
|
|
|
renderComponent();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("گلوکوزامین")).toBeInTheDocument();
|
|
});
|
|
|
|
const editBtns = document.querySelectorAll(".text-blue-600.hover\\:bg-blue-50");
|
|
expect(editBtns.length).toBeGreaterThan(0);
|
|
fireEvent.click(editBtns[0]);
|
|
|
|
expect(screen.getByText("ویرایش دانشنامه")).toBeInTheDocument();
|
|
|
|
const form = document.querySelector("#wikiForm");
|
|
const inputs = form!.querySelectorAll("input[type='text']");
|
|
expect(inputs[0]).toBeDisabled();
|
|
expect(inputs[0]).toHaveValue("glucosamine");
|
|
|
|
expect(inputs[1]).toHaveValue("گلوکوزامین");
|
|
fireEvent.change(inputs[1], { target: { value: "گلوکوزامین هیدروکلراید" } });
|
|
|
|
const saveButton = screen.getByRole("button", { name: "ذخیره" });
|
|
fireEvent.click(saveButton);
|
|
|
|
await waitFor(() => {
|
|
expect(api.put).toHaveBeenCalledWith(
|
|
"/admin/wiki/glucosamine",
|
|
expect.objectContaining({
|
|
term: "گلوکوزامین هیدروکلراید",
|
|
definition: "ماده مغذی جهت بازسازی غضروفها و مفاصل در سگها و گربهها.",
|
|
})
|
|
);
|
|
expect(toast.success).toHaveBeenCalledWith("اصطلاح علمی با موفقیت ویرایش شد");
|
|
});
|
|
});
|
|
|
|
it("deletes a scientific term after confirmation modal", async () => {
|
|
vi.mocked(api.delete).mockResolvedValueOnce({ data: { success: true } });
|
|
|
|
renderComponent();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("گلوکوزامین")).toBeInTheDocument();
|
|
});
|
|
|
|
const deleteBtns = document.querySelectorAll(".text-red-600.hover\\:bg-red-50");
|
|
expect(deleteBtns.length).toBeGreaterThan(0);
|
|
fireEvent.click(deleteBtns[0]);
|
|
|
|
expect(screen.getByText("حذف اصطلاح علمی")).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText("آیا از حذف این مورد مطمئن هستید؟ این عملیات قابل بازگشت نیست.")
|
|
).toBeInTheDocument();
|
|
|
|
const confirmBtn = screen.getByRole("button", { name: "تایید" });
|
|
fireEvent.click(confirmBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.delete).toHaveBeenCalledWith("/admin/wiki/glucosamine");
|
|
expect(toast.success).toHaveBeenCalledWith("اصطلاح علمی با موفقیت حذف شد");
|
|
});
|
|
});
|
|
|
|
it("shows empty state when no terms exist", async () => {
|
|
vi.mocked(api.get).mockImplementation((url: string) => {
|
|
if (url === "/admin/wiki") {
|
|
return Promise.resolve({
|
|
data: { data: [], meta: { total: 0, page: 1, lastPage: 1 } },
|
|
});
|
|
}
|
|
return Promise.resolve({ data: { data: [] } });
|
|
});
|
|
|
|
renderComponent();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("هیچ رکوردی یافت نشد")).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|