feat(blog): add material freshness and authorship

This commit is contained in:
devops committed 2026-10-20 05:36:03 +00:00
1 parent 3f5258a76c
commit 51ca8c7e34
24 files changed
+187 -52

No files matched your search

+14 -11
View File
@@ -26,6 +26,7 @@ import { productService } from "../lib/services/productService";
import { useRouter } from 'next/navigation';
import Image from 'next/image';
import SafeImage from './SafeImage';
import { BLOG_TEAM_FALLBACK, publicationDate, validContentUpdatedAt, persianDate } from '../lib/blogDates';
export interface BlogCategory {
id: string;
@@ -42,6 +43,11 @@ export interface BlogPostItem {
imageAlt?: string;
date: string;
author: string;
authorTeam?: string;
authorRole?: string;
publishedAt?: string;
createdAt?: string;
contentUpdatedAt?: string;
content: string;
category?: string;
categoryId?: string;
@@ -324,10 +330,8 @@ export default function BlogPage({
<div className="bg-canina-blue/10 px-3.5 py-1 rounded-full uppercase tracking-wider">
{featuredPost.category || "پژوهش بالینی"}
</div>
<div className="flex items-center gap-1 text-medical-gray-400">
<Calendar className="w-3.5 h-3.5" />
{featuredPost.date}
</div>
<div className="flex items-center gap-1 text-medical-gray-400"><Calendar className="w-3.5 h-3.5" />تاریخ انتشار: <time dateTime={publicationDate(featuredPost)}>{persianDate(publicationDate(featuredPost))}</time></div>
{validContentUpdatedAt(featuredPost) && <div className="text-medical-gray-400">آخرین به‌روزرسانی: <time dateTime={validContentUpdatedAt(featuredPost)}>{persianDate(validContentUpdatedAt(featuredPost))}</time></div>}
<div className="flex items-center gap-1 text-medical-gray-400">
<Clock className="w-3.5 h-3.5" />
{featuredPost.readingTime || 4} دقیقه مطالعه
@@ -349,8 +353,9 @@ export default function BlogPage({
<User className="w-5 h-5" />
</div>
<div className="text-xs">
<div className="font-black text-medical-gray-900">{featuredPost.author}</div>
<div className="font-bold text-medical-gray-400">تیم پژوهش کنینا</div>
<div className="font-black text-medical-gray-900">نویسنده: {featuredPost.author}</div>
<div className="font-bold text-medical-gray-400">تیم: {featuredPost.authorTeam || BLOG_TEAM_FALLBACK}</div>
{featuredPost.authorRole && <div className="font-bold text-medical-gray-400">{featuredPost.authorRole}</div>}
</div>
</div>
<div className="flex items-center gap-2 text-canina-blue font-black text-sm">
@@ -406,10 +411,8 @@ export default function BlogPage({
</div>
<div className="p-4 sm:p-7">
<div className="flex items-center justify-between text-[11px] font-bold text-medical-gray-400 mb-3">
<div className="flex items-center gap-1">
<Calendar className="w-3 h-3" />
{post.date}
</div>
<div className="flex items-center gap-1"><Calendar className="w-3 h-3" />تاریخ انتشار: <time dateTime={publicationDate(post)}>{persianDate(publicationDate(post))}</time></div>
{validContentUpdatedAt(post) && <div>آخرین به‌روزرسانی: <time dateTime={validContentUpdatedAt(post)}>{persianDate(validContentUpdatedAt(post))}</time></div>}
<div className="flex items-center gap-3">
<div className="flex items-center gap-1">
<Clock className="w-3 h-3" />
@@ -433,7 +436,7 @@ export default function BlogPage({
<div className="px-4 sm:px-7 pb-4 sm:pb-6 pt-2.5 border-t border-medical-gray-100 flex items-center justify-between">
<div className="text-[11px] font-bold text-medical-gray-500 flex items-center gap-1.5">
<User className="w-3.5 h-3.5 text-medical-gray-400" />
<span>{post.author}</span>
<span>نویسنده: {post.author}</span><span>تیم: {post.authorTeam || BLOG_TEAM_FALLBACK}</span>
</div>
<div className="flex items-center gap-1 text-canina-blue font-black text-xs group-hover:underline">
<span>مطالعه</span>
@@ -1,4 +1,5 @@
"use client";
import { BLOG_TEAM_FALLBACK, publicationDate, validContentUpdatedAt, persianDate } from "../lib/blogDates";
import React, { useState, useEffect } from "react";
import {
ChevronRight,
@@ -147,12 +148,8 @@ export default function BlogPostClient({ blog, publishedDate, authorName, saniti
{blog.category.name}
</span>
)}
{publishedDate && (
<div className="flex items-center gap-1">
<Calendar className="w-3.5 h-3.5 text-canina-blue" />
<span>{publishedDate}</span>
</div>
)}
{publishedDate && <div className="flex items-center gap-1"><Calendar className="w-3.5 h-3.5 text-canina-blue" />تاریخ انتشار: <time dateTime={publicationDate(blog)}>{publishedDate}</time></div>}
{validContentUpdatedAt(blog) && <div>آخرین به‌روزرسانی: <time dateTime={validContentUpdatedAt(blog)}>{persianDate(validContentUpdatedAt(blog))}</time></div>}
<div className="flex items-center gap-1">
<Clock className="w-3.5 h-3.5 text-canina-blue" />
<span>{blog.readingTime || 4} دقیقه مطالعه</span>
@@ -212,9 +209,9 @@ export default function BlogPostClient({ blog, publishedDate, authorName, saniti
<User className="w-7 h-7" />
</div>
<div>
<div className="font-black text-medical-gray-900 text-base">{authorName}</div>
<div className="font-black text-medical-gray-900 text-base">نویسنده: {authorName}</div>
<div className="font-bold text-xs text-medical-gray-400 mt-0.5">
تیم تحقیق و توسعه علمی کنینا آلمان (Canina Pharma)
تیم: {blog.authorTeam || BLOG_TEAM_FALLBACK}
</div>
</div>
</div>
@@ -7,6 +7,7 @@ import SafeImage from "./SafeImage";
import { useSettingsStore } from "../lib/store/settingsStore";
import api, { BASE_DOMAIN } from "../lib/services/api";
import { toPersian } from "../lib/utils";
import { BLOG_TEAM_FALLBACK, publicationDate, validContentUpdatedAt, persianDate } from "../lib/blogDates";
export interface BlogPost {
id: string;
@@ -16,6 +17,10 @@ export interface BlogPost {
imageUrl?: string;
viewCount: number;
createdAt: string;
publishedAt?: string;
contentUpdatedAt?: string;
authorTeam?: string;
authorRole?: string;
author?: {
firstName: string;
lastName: string;
@@ -108,7 +113,7 @@ export default function BlogPreviewSection() {
? `${blog.author.firstName || ""} ${blog.author.lastName || ""}`.trim()
: "تحریریه کنینا";
const formattedDate = new Date(blog.createdAt).toLocaleDateString("fa-IR");
const blogHref = `/blog/${encodeURIComponent(blog.slug || blog.id)}`;
@@ -134,10 +139,8 @@ export default function BlogPreviewSection() {
<div className="p-3.5 sm:p-6 flex-1 flex flex-col justify-between space-y-3 sm:space-y-4">
<div className="space-y-1.5 sm:space-y-2">
<div className="flex items-center gap-2.5 sm:gap-4 text-[10px] sm:text-xs text-medical-gray-400 font-medium">
<span className="flex items-center gap-1">
<Calendar className="w-3 h-3 sm:w-3.5 sm:h-3.5" />
{toPersian(formattedDate)}
</span>
<span className="flex items-center gap-1"><Calendar className="w-3 h-3 sm:w-3.5 sm:h-3.5" />تاریخ انتشار: <time dateTime={publicationDate(blog)}>{persianDate(publicationDate(blog))}</time></span>
{validContentUpdatedAt(blog) && <span>آخرین به‌روزرسانی: <time dateTime={validContentUpdatedAt(blog)}>{persianDate(validContentUpdatedAt(blog))}</time></span>}
<span className="flex items-center gap-1">
<Eye className="w-3 h-3 sm:w-3.5 sm:h-3.5" />
{toPersian(blog.viewCount || 0)}
@@ -157,8 +160,8 @@ export default function BlogPreviewSection() {
<div className="pt-2.5 sm:pt-4 border-t border-medical-gray-200/60 flex items-center justify-between">
<span className="text-[10px] sm:text-xs font-bold text-medical-gray-500 truncate max-w-[100px] sm:max-w-none">
{authorName}
</span>
نویسنده: {authorName}
</span><span className="text-[10px] sm:text-xs font-bold text-medical-gray-500">تیم: {blog.authorTeam || BLOG_TEAM_FALLBACK}</span>
<Link
href={blogHref}
aria-label={`مطالعه مقاله: ${blog.title}`}
@@ -21,6 +21,7 @@ const categories=[{id:'joints',name:'Joints',slug:'joints'},{id:'skin',name:'Ski
beforeEach(()=>vi.clearAllMocks());
describe('BlogPage',()=>{
it('renders featured and regular posts as semantic links',()=>{render(<BlogPage blogs={blogs} categories={categories}/>);expect(screen.getAllByRole('heading',{level:2})[0]).toHaveTextContent('Skin article');expect(screen.getByRole('link',{name:/Skin article/})).toHaveAttribute('href','/blog/skin');expect(screen.getByRole('link',{name:/Joint article/})).toHaveAttribute('href','/blog/joint');});
it('labels publication, valid material update, author and stable team on featured and regular cards',()=>{const fresh=[{...blogs[0],publishedAt:'2026-01-01T08:00:00Z',contentUpdatedAt:'2026-01-02T08:00:00Z',authorTeam:'Clinical'}, {...blogs[1],publishedAt:'2026-01-02T08:00:00Z',contentUpdatedAt:'2026-01-02T12:00:00Z'}];render(<BlogPage blogs={fresh} categories={categories}/>);expect(screen.getAllByText(/تاریخ انتشار/)).toHaveLength(2);expect(screen.getByText(/آخرین به‌روزرسانی/)).toBeInTheDocument();expect(screen.getAllByText(/نویسنده:/)).toHaveLength(2);expect(screen.getByText(/تیم: Clinical/)).toBeInTheDocument();expect(screen.getByText(/تیم: تیم پژوهش و محتوای علمی کنینا/)).toBeInTheDocument();expect(document.querySelectorAll('time[datetime]').length).toBeGreaterThanOrEqual(3);});
it('combines category, excerpt search and tags then resets empty intersection',()=>{render(<BlogPage blogs={blogs} categories={categories}/>);fireEvent.click(screen.getByRole('button',{name:'Joints'}));expect(h.replace).toHaveBeenCalledWith('/blog?categoryId=joints',{scroll:false});expect(screen.queryByText('Skin article')).not.toBeInTheDocument();fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماری‌ها، ویتامین‌ها...'),{target:{value:'MOBILITY'}});expect(screen.getByText('Joint article')).toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'#coat'}));expect(screen.getByText('هیچ مقاله‌ای با معیارهای جستجوی شما یافت نشد.')).toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'پاکسازی همه فیلترها'}));expect(screen.getByText('Skin article')).toBeInTheDocument();expect(screen.getByText('Joint article')).toBeInTheDocument();});
it('syncs category with route props',()=>{const view=render(<BlogPage blogs={blogs} categories={categories} initialCategoryId="joints"/>);expect(screen.queryByText('Skin article')).not.toBeInTheDocument();view.rerender(<BlogPage blogs={blogs} categories={categories} initialCategoryId="skin"/>);expect(screen.queryByText('Joint article')).not.toBeInTheDocument();});
it('clears search and toggles tags off',()=>{render(<BlogPage blogs={blogs}/>);fireEvent.click(screen.getByRole('button',{name:'#health'}));expect(screen.queryByText('Skin article')).not.toBeInTheDocument();fireEvent.click(screen.getByRole('button',{name:'#health'}));expect(screen.getByText('Skin article')).toBeInTheDocument();fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماری‌ها، ویتامین‌ها...'),{target:{value:'missing'}});fireEvent.click(screen.getByRole('button',{name:'پاک کردن'}));expect(screen.getByText('Skin article')).toBeInTheDocument();});
@@ -89,7 +89,7 @@ describe("BlogPostClient Component", () => {
// 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.getByText("۱۵ فروردین ۱۴۰۵")).toBeInTheDocument();
expect(screen.getAllByText(/دقیقه مطالعه/).length).toBeGreaterThan(0);
@@ -110,6 +110,15 @@ describe("BlogPostClient Component", () => {
expect(screen.getByText("بسته‌بندی قوطی کانی‌هیدروکس")).toBeInTheDocument();
});
it("renders semantic publication, valid update and explicit author/team labels", () => {
render(<BlogPostClient blog={{...mockBlog,publishedAt:"2026-01-01T00:00:00Z",contentUpdatedAt:"2026-01-02T00:00:00Z",authorTeam:"Science"}} sanitizedContent={mockBlog.content} publishedDate="۱۲ دی ۱۴۰۴" authorName="دکتر بکر" />);
expect(screen.getByText(/تاریخ انتشار/)).toBeInTheDocument();
expect(screen.getByText(/آخرین به‌روزرسانی/)).toBeInTheDocument();
expect(screen.getByText(/نویسنده: دکتر بکر/)).toBeInTheDocument();
expect(screen.getByText(/تیم: Science/)).toBeInTheDocument();
expect(document.querySelectorAll("time[datetime]").length).toBe(2);
});
it("registers blog view count on mount via API", async () => {
render(
<BlogPostClient
@@ -110,9 +110,9 @@ describe("BlogPreviewSection Component", () => {
});
// Author display
expect(screen.getByText("دکتر مریم اکبری")).toBeInTheDocument();
expect(screen.getByText("نویسنده: دکتر مریم اکبری")).toBeInTheDocument();
// Default fallback author
expect(screen.getByText("تحریریه کنینا")).toBeInTheDocument();
expect(screen.getByText("نویسنده: تحریریه کنینا")).toBeInTheDocument();
// Verify aria-label links with full title context
const link1 = screen.getByLabelText("مطالعه مقاله: اهمیت کلسیم و مفاصل در رشد توله سگ ژرمن شپرد");
@@ -122,6 +122,15 @@ describe("BlogPreviewSection Component", () => {
expect(link2).toHaveAttribute("href", "/blog/cat-hair-loss-solutions");
});
it("uses publication freshness and explicit byline labels on homepage cards", async () => {
vi.mocked(api.get).mockResolvedValueOnce({ data: { blogs: [{ id:"b",slug:"b",title:"B",content:"",viewCount:0,createdAt:"2026-01-01T00:00:00Z",publishedAt:"2026-01-01T00:00:00Z",contentUpdatedAt:"2026-01-02T00:00:00Z",authorTeam:"Science" }] } });
render(<BlogPreviewSection />);
expect(await screen.findByText(/تاریخ انتشار/)).toBeInTheDocument();
expect(screen.getByText(/آخرین به‌روزرسانی/)).toBeInTheDocument();
expect(screen.getByText(/نویسنده:/)).toBeInTheDocument();
expect(screen.getByText(/تیم: Science/)).toBeInTheDocument();
});
it("handles api errors gracefully and renders null", async () => {
vi.mocked(api.get).mockRejectedValueOnce(new Error("Failed to load"));
const { container } = render(<BlogPreviewSection />);