- 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.
145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
"use client";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
|
import React from "react";
|
|
import ProductReviews from "../ProductReviews";
|
|
import api from "../../lib/services/api";
|
|
import { toast } from "sonner";
|
|
|
|
vi.mock("../../lib/services/api", () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("sonner", () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe("ProductReviews Component", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("should fetch and render approved reviews and rating stats", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({
|
|
data: {
|
|
success: true,
|
|
data: [
|
|
{
|
|
id: "rev-1",
|
|
userName: "رضا کریمی",
|
|
rating: 5,
|
|
comment: "تاثیر فوقالعادهای روی اشتهای سگم داشت",
|
|
adminReply: "خوشحالیم از رضایت شما!",
|
|
createdAt: "2026-10-10T10:00:00Z",
|
|
},
|
|
],
|
|
pagination: { totalPages: 1 },
|
|
stats: { averageRating: 4.8, totalReviews: 12 },
|
|
},
|
|
});
|
|
|
|
render(
|
|
<ProductReviews
|
|
productId="prod-123"
|
|
productName="کانویتال تونیک"
|
|
/>
|
|
);
|
|
|
|
expect(screen.getByText("نظرات و تجربیات خریداران")).toBeInTheDocument();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText("رضا کریمی")).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText("تاثیر فوقالعادهای روی اشتهای سگم داشت")
|
|
).toBeInTheDocument();
|
|
expect(screen.getByText("خوشحالیم از رضایت شما!")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("should show empty review placeholder if no reviews exist", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({
|
|
data: {
|
|
success: true,
|
|
data: [],
|
|
pagination: { totalPages: 1 },
|
|
stats: { averageRating: 5, totalReviews: 0 },
|
|
},
|
|
});
|
|
|
|
render(
|
|
<ProductReviews
|
|
productId="prod-123"
|
|
productName="کانویتال تونیک"
|
|
/>
|
|
);
|
|
|
|
await waitFor(() => {
|
|
expect(
|
|
screen.getByText(/هنوز دیدگاهی برای این محصول ثبت نشده است/)
|
|
).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it("should validate and submit a new review", async () => {
|
|
vi.mocked(api.get).mockResolvedValueOnce({
|
|
data: {
|
|
success: true,
|
|
data: [],
|
|
pagination: { totalPages: 1 },
|
|
stats: { averageRating: 5, totalReviews: 0 },
|
|
},
|
|
});
|
|
|
|
vi.mocked(api.post).mockResolvedValueOnce({
|
|
data: {
|
|
success: true,
|
|
message: "دیدگاه شما با موفقیت ثبت شد",
|
|
},
|
|
});
|
|
|
|
render(
|
|
<ProductReviews
|
|
productId="prod-123"
|
|
productName="کانویتال تونیک"
|
|
/>
|
|
);
|
|
|
|
await waitFor(() => {
|
|
expect(
|
|
screen.getByText(/هنوز دیدگاهی برای این محصول ثبت نشده است/)
|
|
).toBeInTheDocument();
|
|
});
|
|
|
|
// Open form button: "ثبت دیدگاه جدید"
|
|
const openFormBtn = screen.getByText("ثبت دیدگاه جدید");
|
|
fireEvent.click(openFormBtn);
|
|
|
|
const commentInput = screen.getByPlaceholderText(
|
|
"تجربه خود را از نحوه مصرف، تغییرات بالینی پت و اثربخشی این مکمل بنویسید..."
|
|
);
|
|
fireEvent.change(commentInput, {
|
|
target: { value: "کیفیت بسیار عالی و بستهبندی ارجینال آلمانی" },
|
|
});
|
|
|
|
const submitBtn = screen.getByText("ثبت و ارسال دیدگاه");
|
|
fireEvent.click(submitBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith(
|
|
"/products/prod-123/reviews",
|
|
expect.objectContaining({
|
|
comment: "کیفیت بسیار عالی و بستهبندی ارجینال آلمانی",
|
|
rating: 5,
|
|
})
|
|
);
|
|
expect(toast.success).toHaveBeenCalledWith("دیدگاه شما با موفقیت ثبت شد");
|
|
});
|
|
});
|
|
});
|