Files
canina/frontend/application/app/blog/[slug]/blog-slug-page.test.tsx
T
devops f8f9329c50
Deploy Canina / deploy (push) Successful in 2m10s
feat(seo): implement FAQPage schema, sub-sitemaps and local Lalezar WOFF2 font (SEO-P1-07, SEO-P1-10, SEO-P2-16)
- SEO-P1-07: extract FAQs and accordion items from blog content and inject JSON-LD FAQPage schema
- SEO-P1-10: add dynamic sub-sitemaps /sitemap-blog.xml and /sitemap-wiki.xml and register in robots.ts
- SEO-P2-16: host Lalezar font locally as optimized WOFF2 with preload enabled in lib/fonts.ts
- Add full regression test suites with 100% pass rate
2026-10-17 20:38:09 +00:00

105 lines
3.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import React from 'react';
import BlogPostPage from './page';
vi.mock('../../../lib/seo', () => ({
getSeoConfig: vi.fn().mockResolvedValue({
brandNameFa: 'کنینا ایران',
canonicalBaseUrl: 'https://canina.ir',
ogImageUrl: '/assets/og.jpg',
defaultMetaDescription: 'توضیحات پیش‌فرض',
titleSeparator: '|',
titlePosition: 'suffix',
}),
formatPageTitle: vi.fn((title) => `${title} | کنینا ایران`),
}));
vi.mock('../../../components/BlogPostClient', () => ({
default: () => <div data-testid="blog-post-client" />,
}));
describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => {
beforeEach(() => {
vi.stubEnv('INTERNAL_API_URL', 'http://backend:3000/api');
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://canina.ir');
});
afterEach(() => {
vi.unstubAllGlobals();
vi.unstubAllEnvs();
});
it('injects FAQPage JSON-LD schema when article contains questions and answers', async () => {
const blogData = {
slug: 'joint-health',
title: 'سلامت مفاصل سگ',
content: `
<article>
<p>مقدمه‌ای درباره مفاصل سگ.</p>
<details>
<summary>آیا این مکمل برای سگ مسن مناسب است؟</summary>
<p>بله، بسیار توصیه می‌شود و روند تخریب مفصل را کند می‌کند.</p>
</details>
</article>
`,
createdAt: '2026-01-01',
publishedAt: '2026-01-01',
};
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: async () => blogData,
})
);
const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'joint-health' }) });
expect(jsx).toBeDefined();
// Verify scripts in JSX tree
const scripts = React.Children.toArray(jsx.props.children).filter(
(child: any) => child?.type === 'script' && child?.props?.['type'] === 'application/ld+json'
);
// There should be 3 schemas: Article, Breadcrumb, and FAQPage
expect(scripts.length).toBe(3);
const faqScript: any = scripts.find((s: any) => s.props['data-testid'] === 'faq-schema');
expect(faqScript).toBeDefined();
const faqJson = JSON.parse(faqScript.props.dangerouslySetInnerHTML.__html);
expect(faqJson['@type']).toBe('FAQPage');
expect(faqJson.mainEntity).toHaveLength(1);
expect(faqJson.mainEntity[0].name).toBe('آیا این مکمل برای سگ مسن مناسب است؟');
});
it('does not inject FAQPage schema when article does not have FAQs', async () => {
const blogData = {
slug: 'nutrition',
title: 'تغذیه سگ',
content: '<p>یک مقاله ساده بدون سوال و جواب یا آکاردئون.</p>',
createdAt: '2026-01-01',
publishedAt: '2026-01-01',
};
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: async () => blogData,
})
);
const jsx = await BlogPostPage({ params: Promise.resolve({ slug: 'nutrition' }) });
const scripts = React.Children.toArray(jsx.props.children).filter(
(child: any) => child?.type === 'script' && child?.props?.['type'] === 'application/ld+json'
);
// Only Article and Breadcrumb schemas
expect(scripts.length).toBe(2);
const faqScript = scripts.find((s: any) => s.props['data-testid'] === 'faq-schema');
expect(faqScript).toBeUndefined();
});
});