test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -0,0 +1,257 @@
|
||||
import React from "react";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import BlogPostClient from "../BlogPostClient";
|
||||
import api from "../../lib/services/api";
|
||||
|
||||
// Mock api
|
||||
vi.mock("../../lib/services/api", () => ({
|
||||
default: {
|
||||
post: vi.fn(),
|
||||
get: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// 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" />
|
||||
),
|
||||
}));
|
||||
|
||||
const mockBlog = {
|
||||
id: "blog-100",
|
||||
slug: "canhydrox-gag-guide",
|
||||
title: "راهنمای جامع کانیهیدروکس GAG و ترمیم مفاصل سگها",
|
||||
excerpt: "این مکمل تخصصی با تحریک ساخت کلاژن و تقویت استخوانها نقش اساسی دارد.",
|
||||
content: "<p>محتوای تخصصی مقاله کنینا درباره فرمولاسیون مفاصل و صدف لبسبز.</p>",
|
||||
imageUrl: "https://example.com/blog-hero.jpg",
|
||||
imageAlt: "تصویر کانیهیدروکس GAG",
|
||||
imageCaption: "بستهبندی قوطی کانیهیدروکس",
|
||||
readingTime: 6,
|
||||
viewCount: 1540,
|
||||
allowComments: true,
|
||||
category: {
|
||||
id: "cat-joints",
|
||||
name: "سلامت استخوان و مفاصل",
|
||||
},
|
||||
tags: [
|
||||
{ id: "t-1", name: "مفصل_ساز" },
|
||||
{ id: "t-2", name: "کلسیم" },
|
||||
"صدف_لب_سبز",
|
||||
],
|
||||
comments: [
|
||||
{
|
||||
id: "c-1",
|
||||
authorName: "دکتر مریم احمدی",
|
||||
content: "برای سگ ژرمن ۴ ماهه دوز پیشنهادی به چه شکل است؟",
|
||||
createdAt: "2026-03-01T10:00:00Z",
|
||||
},
|
||||
],
|
||||
relatedProducts: [
|
||||
{
|
||||
id: "prod-1",
|
||||
slug: "canhydrox-gag-tablets",
|
||||
nameFa: "قرص کانیهیدروکس GAG",
|
||||
category: { name: "مفاصل" },
|
||||
imageUrl: "/assets/images/canhydrox.png",
|
||||
priceValue: 1850000,
|
||||
},
|
||||
],
|
||||
relatedBlogs: [
|
||||
{
|
||||
id: "rel-blog-1",
|
||||
slug: "calcium-dosages",
|
||||
title: "دوز مصرفی کلسیم در نژادهای بزرگ",
|
||||
readingTime: 4,
|
||||
imageUrl: "/assets/images/calcium.png",
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
describe("BlogPostClient Component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
sessionStorage.clear();
|
||||
vi.mocked(api.post).mockResolvedValue({ data: { viewCount: 1541 } });
|
||||
});
|
||||
|
||||
it("renders blog title, author, category, reading time, excerpt, sanitized content, and tags", async () => {
|
||||
render(
|
||||
<BlogPostClient
|
||||
blog={mockBlog}
|
||||
publishedDate="۱۵ فروردین ۱۴۰۵"
|
||||
authorName="دکتر توماس بکر"
|
||||
/>
|
||||
);
|
||||
|
||||
// Breadcrumb and headers
|
||||
expect(screen.getByRole("heading", { level: 1, name: /راهنمای جامع کانیهیدروکس GAG/ })).toBeInTheDocument();
|
||||
expect(screen.getAllByText("سلامت استخوان و مفاصل").length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("دکتر توماس بکر")).toBeInTheDocument();
|
||||
expect(screen.getByText("۱۵ فروردین ۱۴۰۵")).toBeInTheDocument();
|
||||
expect(screen.getAllByText(/دقیقه مطالعه/).length).toBeGreaterThan(0);
|
||||
|
||||
// Excerpt and HTML content
|
||||
expect(
|
||||
screen.getByText("این مکمل تخصصی با تحریک ساخت کلاژن و تقویت استخوانها نقش اساسی دارد.")
|
||||
).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText("محتوای تخصصی مقاله کنینا درباره فرمولاسیون مفاصل و صدف لبسبز.")
|
||||
).toBeInTheDocument();
|
||||
|
||||
// Tags
|
||||
expect(screen.getByText("#مفصل_ساز")).toBeInTheDocument();
|
||||
expect(screen.getByText("#کلسیم")).toBeInTheDocument();
|
||||
expect(screen.getByText("#صدف_لب_سبز")).toBeInTheDocument();
|
||||
|
||||
// Image caption
|
||||
expect(screen.getByText("بستهبندی قوطی کانیهیدروکس")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("registers blog view count on mount via API", async () => {
|
||||
render(
|
||||
<BlogPostClient
|
||||
blog={mockBlog}
|
||||
publishedDate="۱۵ فروردین ۱۴۰۵"
|
||||
authorName="دکتر توماس بکر"
|
||||
/>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.post).toHaveBeenCalledWith(
|
||||
`/blogs/${encodeURIComponent(mockBlog.slug)}/view`
|
||||
);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText(/۱٬۵۴۱ بازدید/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("renders related products and related blogs in sidebars", () => {
|
||||
render(
|
||||
<BlogPostClient
|
||||
blog={mockBlog}
|
||||
publishedDate="۱۵ فروردین ۱۴۰۵"
|
||||
authorName="دکتر توماس بکر"
|
||||
/>
|
||||
);
|
||||
|
||||
// Related Products
|
||||
expect(screen.getByText("مکملهای مرتبط این مقاله")).toBeInTheDocument();
|
||||
expect(screen.getByText("قرص کانیهیدروکس GAG")).toBeInTheDocument();
|
||||
expect(screen.getByText(/۱٬۸۵۰٬۰۰۰ تومان/)).toBeInTheDocument();
|
||||
|
||||
// Related Blogs
|
||||
expect(screen.getByText("مقالات پیشنهادی")).toBeInTheDocument();
|
||||
expect(screen.getByText("دوز مصرفی کلسیم در نژادهای بزرگ")).toBeInTheDocument();
|
||||
expect(screen.getAllByText(/دقیقه مطالعه/).length).toBeGreaterThan(0);
|
||||
|
||||
// Scientific Consultation CTA
|
||||
expect(screen.getByText("نیاز به راهنمایی و دوز درمانی دارید؟")).toBeInTheDocument();
|
||||
expect(screen.getByRole("link", { name: /مشاوره هوشمند دارویی/ })).toHaveAttribute(
|
||||
"href",
|
||||
"/smart-advisor"
|
||||
);
|
||||
});
|
||||
|
||||
it("renders existing comments and handles new comment submission successfully", async () => {
|
||||
vi.mocked(api.post).mockImplementation(async (url: string) => {
|
||||
if (url.includes("/view")) {
|
||||
return { data: { viewCount: 1541 } };
|
||||
}
|
||||
if (url.includes("/comments")) {
|
||||
return { data: { success: true } };
|
||||
}
|
||||
return { data: {} };
|
||||
});
|
||||
|
||||
render(
|
||||
<BlogPostClient
|
||||
blog={mockBlog}
|
||||
publishedDate="۱۵ فروردین ۱۴۰۵"
|
||||
authorName="دکتر توماس بکر"
|
||||
/>
|
||||
);
|
||||
|
||||
// Check existing comment
|
||||
expect(screen.getByText("دکتر مریم احمدی")).toBeInTheDocument();
|
||||
expect(
|
||||
screen.getByText("برای سگ ژرمن ۴ ماهه دوز پیشنهادی به چه شکل است؟")
|
||||
).toBeInTheDocument();
|
||||
|
||||
// Submit new comment
|
||||
const nameInput = screen.getByPlaceholderText("نام و نام خانوادگی...");
|
||||
const emailInput = screen.getByPlaceholderText("آدرس ایمیل...");
|
||||
const textarea = screen.getByPlaceholderText("متن دیدگاه شما...");
|
||||
const submitBtn = screen.getByRole("button", { name: /ارسال نظر/ });
|
||||
|
||||
fireEvent.change(nameInput, { target: { value: "علی رضایی" } });
|
||||
fireEvent.change(emailInput, { target: { value: "ali@example.com" } });
|
||||
fireEvent.change(textarea, { target: { value: "آیا برای گربهها هم قابل استفاده است؟" } });
|
||||
|
||||
fireEvent.click(submitBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.post).toHaveBeenCalledWith(
|
||||
`/blogs/${encodeURIComponent(mockBlog.slug)}/comments`,
|
||||
{
|
||||
userName: "علی رضایی",
|
||||
email: "ali@example.com",
|
||||
content: "آیا برای گربهها هم قابل استفاده است؟",
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
screen.getByText(/دیدگاه شما با موفقیت ثبت شد و پس از بررسی و تایید تیم علمی/)
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("handles empty fields without submitting comments", async () => {
|
||||
render(
|
||||
<BlogPostClient
|
||||
blog={mockBlog}
|
||||
publishedDate="۱۵ فروردین ۱۴۰۵"
|
||||
authorName="دکتر توماس بکر"
|
||||
/>
|
||||
);
|
||||
|
||||
const submitBtn = screen.getByRole("button", { name: /ارسال نظر/ });
|
||||
fireEvent.click(submitBtn);
|
||||
|
||||
expect(api.post).not.toHaveBeenCalledWith(
|
||||
expect.stringContaining("/comments"),
|
||||
expect.anything()
|
||||
);
|
||||
});
|
||||
|
||||
it("copies URL to clipboard when navigator.share is unavailable", async () => {
|
||||
const writeTextMock = vi.fn().mockResolvedValue(undefined);
|
||||
Object.assign(navigator, {
|
||||
clipboard: {
|
||||
writeText: writeTextMock,
|
||||
},
|
||||
share: undefined,
|
||||
});
|
||||
|
||||
render(
|
||||
<BlogPostClient
|
||||
blog={mockBlog}
|
||||
publishedDate="۱۵ فروردین ۱۴۰۵"
|
||||
authorName="دکتر توماس بکر"
|
||||
/>
|
||||
);
|
||||
|
||||
const shareBtn = screen.getByRole("button", { name: /اشتراکگذاری/ });
|
||||
fireEvent.click(shareBtn);
|
||||
|
||||
expect(writeTextMock).toHaveBeenCalled();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("لینک کپی شد!")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user