fix(blog): improve attribution layout and freshness
Deploy Canina / deploy (push) Successful in 4m4s
Deploy Canina / deploy (push) Successful in 4m4s
This commit is contained in:
1 parent
24362b89d7
commit
28485d51f7
6 files changed
+49
-20
No files matched your search
@@ -328,6 +328,18 @@ describe("Admin BlogsService", () => {
|
||||
expect(payload).not.toHaveProperty("markContentUpdated");
|
||||
expect(payload).not.toHaveProperty("createdAt");
|
||||
});
|
||||
it("automatically marks freshness when material article content changes", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Old content" });
|
||||
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
await service.updateBlog("b1", { content: "Meaningfully revised content" });
|
||||
expect(mockPrisma.blog.update.mock.calls[0][0].data.contentUpdatedAt).toEqual(expect.any(Date));
|
||||
});
|
||||
it("does not mark freshness for attribution-only changes", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post", content: "Same content" });
|
||||
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
await service.updateBlog("b1", { authorTeam: "Clinical team", authorRole: "Review" });
|
||||
expect(mockPrisma.blog.update.mock.calls[0][0].data).not.toHaveProperty("contentUpdatedAt");
|
||||
});
|
||||
it("allows explicitly clearing contentUpdatedAt", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
|
||||
@@ -312,7 +312,18 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
if (markContentUpdated) {
|
||||
const materialContentFields = [
|
||||
"title",
|
||||
"content",
|
||||
"excerpt",
|
||||
"imageUrl",
|
||||
"imageAlt",
|
||||
"imageCaption",
|
||||
] as const;
|
||||
const hasMaterialContentChange = materialContentFields.some(
|
||||
(field) => data[field] !== undefined && data[field] !== blog[field],
|
||||
);
|
||||
if (markContentUpdated || (contentUpdatedAt === undefined && hasMaterialContentChange)) {
|
||||
(updatePayload as Record<string, unknown>).contentUpdatedAt = now;
|
||||
} else if (contentUpdatedAt !== undefined) {
|
||||
if (contentUpdatedAt === null || contentUpdatedAt === "") {
|
||||
|
||||
@@ -77,9 +77,14 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => {
|
||||
expect(faqJson.mainEntity[0].name).toBe('آیا این مکمل برای سگ مسن مناسب است؟');
|
||||
});
|
||||
|
||||
it('omits dateModified for invalid technical updates and uses matching Organization fallback author', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ok:true,json:async()=>({slug:'fresh',title:'Fresh',content:'<p>x</p>',publishedAt:'2026-01-01T01:00:00Z',contentUpdatedAt:'2026-01-01T12:00:00Z'})}));
|
||||
const jsx=await BlogPostPage({params:Promise.resolve({slug:'fresh'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBeUndefined(); expect(json.author['@type']).toBe('Organization'); expect(json.author.name).toBe('کنینا');
|
||||
it('uses explicit same-day material freshness for dateModified and ignores technical updatedAt', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ok:true,json:async()=>({slug:'fresh',title:'Fresh',content:'<p>x</p>',publishedAt:'2026-01-01T01:00:00Z',updatedAt:'2026-01-01T10:00:00Z',contentUpdatedAt:'2026-01-01T12:00:00Z'})}));
|
||||
const jsx=await BlogPostPage({params:Promise.resolve({slug:'fresh'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBe('2026-01-01T12:00:00.000Z'); expect(json.author['@type']).toBe('Organization'); expect(json.author.name).toBe('کنینا');
|
||||
});
|
||||
|
||||
it('omits dateModified when only technical updatedAt exists', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ok:true,json:async()=>({slug:'technical',title:'Technical',content:'<p>x</p>',publishedAt:'2026-01-01T01:00:00Z',updatedAt:'2026-01-01T12:00:00Z',contentUpdatedAt:null})}));
|
||||
const jsx=await BlogPostPage({params:Promise.resolve({slug:'technical'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBeUndefined();
|
||||
});
|
||||
|
||||
it('uses the stable Organization fallback visibly and in metadata', async () => {
|
||||
|
||||
@@ -347,18 +347,18 @@ export default function BlogPage({
|
||||
<p className="text-sm sm:text-base text-medical-gray-500 mb-8 leading-relaxed line-clamp-3">
|
||||
{featuredPost.excerpt}
|
||||
</p>
|
||||
<div className="flex items-center justify-between pt-6 border-t border-medical-gray-100">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 bg-medical-gray-100 rounded-full flex items-center justify-center text-medical-gray-400">
|
||||
<div data-testid="featured-footer" className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-5 pt-6 border-t border-medical-gray-100">
|
||||
<div className="flex min-w-0 items-start gap-3">
|
||||
<div className="w-10 h-10 shrink-0 bg-medical-gray-100 rounded-full flex items-center justify-center text-medical-gray-400">
|
||||
<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">تیم: {featuredPost.authorTeam || BLOG_TEAM_FALLBACK}</div>
|
||||
{featuredPost.authorRole && <div className="font-bold text-medical-gray-400">{featuredPost.authorRole}</div>}
|
||||
<div data-testid="featured-attribution" className="flex min-w-0 flex-col gap-1 text-xs">
|
||||
<div data-testid="featured-author-name" className="font-black text-medical-gray-900 whitespace-nowrap overflow-hidden text-ellipsis">نویسنده: {featuredPost.author}</div>
|
||||
<div data-testid="featured-team-name" className="font-bold text-medical-gray-400 whitespace-nowrap overflow-hidden text-ellipsis">تیم: {featuredPost.authorTeam || BLOG_TEAM_FALLBACK}</div>
|
||||
{featuredPost.authorRole && <div className="font-bold text-medical-gray-400 whitespace-nowrap overflow-hidden text-ellipsis">{featuredPost.authorRole}</div>}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-canina-blue font-black text-sm">
|
||||
<div data-testid="featured-read-more" className="flex shrink-0 items-center gap-2 self-end sm:self-auto text-canina-blue font-black text-sm whitespace-nowrap">
|
||||
<span>مطالعه مقاله کامل</span>
|
||||
<ArrowLeft className="w-4 h-4 group-hover:-translate-x-2 transition-transform" />
|
||||
</div>
|
||||
@@ -433,12 +433,12 @@ export default function BlogPage({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<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.authorTeam || BLOG_TEAM_FALLBACK}</span>
|
||||
<div className="px-4 sm:px-7 pb-4 sm:pb-6 pt-2.5 border-t border-medical-gray-100 flex items-end justify-between gap-3">
|
||||
<div data-testid={`regular-attribution-${post.id}`} className="min-w-0 text-[11px] font-bold text-medical-gray-500 flex flex-col gap-1">
|
||||
<div className="flex min-w-0 items-center gap-1.5"><User className="w-3.5 h-3.5 shrink-0 text-medical-gray-400" /><span className="truncate">نویسنده: {post.author}</span></div>
|
||||
<div className="truncate pr-5">تیم: {post.authorTeam || BLOG_TEAM_FALLBACK}</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-canina-blue font-black text-xs group-hover:underline">
|
||||
<div className="flex shrink-0 items-center gap-1 text-canina-blue font-black text-xs whitespace-nowrap group-hover:underline">
|
||||
<span>مطالعه</span>
|
||||
<ArrowLeft className="w-3.5 h-3.5 group-hover:-translate-x-1 transition-transform" />
|
||||
</div>
|
||||
|
||||
@@ -21,7 +21,9 @@ 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('labels publication, including a same-day 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.getAllByText(/آخرین بهروزرسانی/)).toHaveLength(2);expect(screen.getAllByText(/نویسنده:/)).toHaveLength(2);expect(screen.getByText(/تیم: Clinical/)).toBeInTheDocument();expect(screen.getByText(/تیم: پژوهش و محتوای علمی کنینا/)).toBeInTheDocument();expect(document.querySelectorAll('time[datetime]').length).toBeGreaterThanOrEqual(4);});
|
||||
it('stacks author and team on separate rows and keeps featured action on one line',()=>{render(<BlogPage blogs={blogs} categories={categories}/>);const featuredMeta=screen.getByTestId('featured-attribution');expect(featuredMeta).toHaveClass('flex-col');expect(screen.getByTestId('featured-read-more')).toHaveClass('whitespace-nowrap');const regularMeta=screen.getByTestId('regular-attribution-1');expect(regularMeta).toHaveClass('flex-col');expect(regularMeta.children).toHaveLength(2);});
|
||||
it('keeps the featured footer responsive without forcing attribution or action to wrap',()=>{render(<BlogPage blogs={blogs} categories={categories}/>);expect(screen.getByTestId('featured-footer')).toHaveClass('flex-col','sm:flex-row');expect(screen.getByTestId('featured-author-name')).toHaveClass('whitespace-nowrap');expect(screen.getByTestId('featured-team-name')).toHaveClass('whitespace-nowrap');});
|
||||
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();});
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export const BLOG_TEAM_FALLBACK = "پژوهش و محتوای علمی کنینا";
|
||||
export function publicationDate(blog:any): string | undefined { return blog?.publishedAt || blog?.createdAt; }
|
||||
function persianDay(value:string): string { return new Intl.DateTimeFormat("fa-IR-u-ca-persian-nu-latn",{year:"numeric",month:"2-digit",day:"2-digit",timeZone:"Asia/Tehran"}).format(new Date(value)); }
|
||||
export function validContentUpdatedAt(blog:any): string | undefined { const published=publicationDate(blog), updated=blog?.contentUpdatedAt; if(!published||!updated) return undefined; const p=new Date(published),u=new Date(updated); return Number.isFinite(p.getTime())&&Number.isFinite(u.getTime())&&u>p&&persianDay(updated)!==persianDay(published)?updated:undefined; }
|
||||
export function validContentUpdatedAt(blog:any): string | undefined { const published=publicationDate(blog), updated=blog?.contentUpdatedAt; if(!published||!updated) return undefined; const p=new Date(published),u=new Date(updated); return Number.isFinite(p.getTime())&&Number.isFinite(u.getTime())&&u>p?updated:undefined; }
|
||||
export function persianDate(value?:string): string { return value ? new Intl.DateTimeFormat("fa-IR-u-ca-persian", { year: "numeric", month: "numeric", day: "numeric", timeZone: "Asia/Tehran" }).format(new Date(value)) : ""; }
|
||||
Reference in new issue
Block a user