- 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.
221 lines
7.6 KiB
TypeScript
221 lines
7.6 KiB
TypeScript
import React from 'react';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
|
import IngredientWiki from '../IngredientWiki';
|
|
import api from '../../lib/services/api';
|
|
import { productService } from '../../lib/services/productService';
|
|
|
|
const mockPush = vi.fn();
|
|
const mockSearchParamsGet = vi.fn();
|
|
|
|
// Mock next/navigation
|
|
vi.mock('next/navigation', () => ({
|
|
useRouter: () => ({
|
|
push: mockPush,
|
|
}),
|
|
useSearchParams: () => ({
|
|
get: mockSearchParamsGet,
|
|
}),
|
|
}));
|
|
|
|
// Mock motion/react
|
|
vi.mock('motion/react', () => ({
|
|
motion: {
|
|
div: ({ children, className, ...props }: any) => <div className={className} {...props}>{children}</div>,
|
|
},
|
|
}));
|
|
|
|
// Mock next/image
|
|
vi.mock('next/image', () => ({
|
|
default: ({ src, alt, className }: any) => <img src={src} alt={alt} className={className} data-testid="next-image" />,
|
|
}));
|
|
|
|
// Mock next/link
|
|
vi.mock('next/link', () => ({
|
|
default: ({ children, href, className }: any) => (
|
|
<a href={href} className={className}>
|
|
{children}
|
|
</a>
|
|
),
|
|
}));
|
|
|
|
// Mock productService and api
|
|
vi.mock('../../lib/services/productService', () => ({
|
|
productService: {
|
|
getProducts: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('../../lib/services/api', () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe('IngredientWiki Component', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockSearchParamsGet.mockReturnValue(null);
|
|
});
|
|
|
|
it('renders default static wiki content when api call fails or returns empty', async () => {
|
|
(productService.getProducts as any).mockRejectedValueOnce(new Error('fail'));
|
|
(api.get as any).mockRejectedValueOnce(new Error('fail'));
|
|
|
|
render(<IngredientWiki />);
|
|
|
|
// Main header check using role heading or getAllByText
|
|
const titles = screen.getAllByText(/دانشنامه مواد/);
|
|
expect(titles.length).toBeGreaterThanOrEqual(1);
|
|
expect(screen.getByText('علم در خدمت کیفیت')).toBeInTheDocument();
|
|
expect(screen.getByText(/۱۰۰٪ فاقد قند افزوده/)).toBeInTheDocument();
|
|
expect(screen.getByText(/مطابق استانداردهای دارویی و بالینی آلمان/)).toBeInTheDocument();
|
|
|
|
// Default static wiki ingredient should be rendered (e.g. green-mussel / صدف لبسبز)
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/صدف لبسبز/)).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it('renders ingredients fetched from API and connects products correctly', async () => {
|
|
const mockIngredientsFromApi = [
|
|
{
|
|
id: 'biotin',
|
|
slug: 'biotin',
|
|
nameFa: 'بیوتین',
|
|
scientificName: 'Vitamin H',
|
|
description: 'ویتامین ضروری برای سلامت پوست و موی حیوانات خانگی.',
|
|
benefits: ['کاهش ریزش مو', 'تقویت بافت شاخی پنجه'],
|
|
},
|
|
{
|
|
id: 'salmon-oil',
|
|
slug: 'salmon-oil',
|
|
nameFa: 'روغن سالمون وحشی',
|
|
nameEn: 'Wild Salmon Oil',
|
|
description: 'منبع غنی اسیدهای چرب امگا ۳ و امگا ۶.',
|
|
benefits: ['سلامت قلب و عروق', 'درخشندگی پوست'],
|
|
},
|
|
];
|
|
|
|
const mockProductsFromApi = [
|
|
{
|
|
id: 'p-1',
|
|
slug: 'biotin-tabs',
|
|
name: 'قرص بیوتین قوی سگ و گربه',
|
|
main_ingredients: ['بیوتین', 'مخمر'],
|
|
benefitsList: ['جلوگیری از کچلی و ریزش'],
|
|
image: '/images/biotin-tabs.png',
|
|
},
|
|
{
|
|
id: 'p-2',
|
|
slug: 'salmon-oil-bottle',
|
|
name: 'روغن ماهی سالمون خالص',
|
|
main_ingredients: ['سالمون'],
|
|
image: '/images/salmon.png',
|
|
},
|
|
];
|
|
|
|
(api.get as any).mockResolvedValueOnce({ data: mockIngredientsFromApi });
|
|
(productService.getProducts as any).mockResolvedValueOnce({ data: mockProductsFromApi });
|
|
|
|
render(<IngredientWiki />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/بیوتین \(Vitamin H\)/)).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.getByText('ویتامین ضروری برای سلامت پوست و موی حیوانات خانگی.')).toBeInTheDocument();
|
|
expect(screen.getByText('کاهش ریزش مو')).toBeInTheDocument();
|
|
expect(screen.getByText('تقویت بافت شاخی پنجه')).toBeInTheDocument();
|
|
|
|
// Check linked products
|
|
expect(screen.getByText('قرص بیوتین قوی سگ و گربه')).toBeInTheDocument();
|
|
expect(screen.getByText('روغن ماهی سالمون خالص')).toBeInTheDocument();
|
|
});
|
|
|
|
it('filters ingredients through search query input', async () => {
|
|
(productService.getProducts as any).mockResolvedValueOnce({ data: [] });
|
|
(api.get as any).mockResolvedValueOnce({
|
|
data: [
|
|
{
|
|
id: 'collagen',
|
|
slug: 'collagen',
|
|
nameFa: 'کلاژن هیدرولیز شده',
|
|
description: 'پروتئین ساختاری مفاصل و غضروفها.',
|
|
benefits: ['انعطاف مفاصل'],
|
|
},
|
|
{
|
|
id: 'silver',
|
|
slug: 'silver',
|
|
nameFa: 'میکروسیلور',
|
|
description: 'نقره ضد باکتری برای مراقبت از لثه و دندان.',
|
|
benefits: ['کاهش جرم دندان'],
|
|
},
|
|
],
|
|
});
|
|
|
|
render(<IngredientWiki />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('کلاژن هیدرولیز شده')).toBeInTheDocument();
|
|
});
|
|
expect(screen.getByText('میکروسیلور')).toBeInTheDocument();
|
|
|
|
// Type "کلاژن" into search box
|
|
const searchInput = screen.getByPlaceholderText(/جستجوی ماده مؤثره/);
|
|
fireEvent.change(searchInput, { target: { value: 'کلاژن' } });
|
|
|
|
expect(screen.getByText('کلاژن هیدرولیز شده')).toBeInTheDocument();
|
|
expect(screen.queryByText('میکروسیلور')).not.toBeInTheDocument();
|
|
|
|
// Search something that does not exist
|
|
fireEvent.change(searchInput, { target: { value: 'ماده ناموجود ۱۲۳' } });
|
|
expect(screen.getByText('مادهای یافت نشد')).toBeInTheDocument();
|
|
expect(screen.getByText('با عبارت جستجو شده ماده مؤثرهای مطابقت ندارد.')).toBeInTheDocument();
|
|
});
|
|
|
|
it('navigates when back button or breadcrumb is clicked', () => {
|
|
(productService.getProducts as any).mockResolvedValueOnce({ data: [] });
|
|
(api.get as any).mockResolvedValueOnce({ data: [] });
|
|
|
|
render(<IngredientWiki />);
|
|
|
|
const backButton = screen.getByRole('button', { name: /بازگشت/ });
|
|
fireEvent.click(backButton);
|
|
expect(mockPush).toHaveBeenCalledWith('/');
|
|
|
|
const homeCrumb = screen.getByText('خانه');
|
|
fireEvent.click(homeCrumb);
|
|
expect(mockPush).toHaveBeenCalledWith('/');
|
|
});
|
|
|
|
it('handles termParam query string to scroll to specific ingredient', async () => {
|
|
mockSearchParamsGet.mockImplementation((param: string) => {
|
|
if (param === 'term') return 'colostrum';
|
|
return null;
|
|
});
|
|
|
|
const mockScrollTo = vi.fn();
|
|
window.scrollTo = mockScrollTo;
|
|
|
|
(productService.getProducts as any).mockResolvedValueOnce({ data: [] });
|
|
(api.get as any).mockResolvedValueOnce({
|
|
data: [
|
|
{
|
|
id: 'colostrum',
|
|
slug: 'colostrum',
|
|
nameFa: 'آغوز گاو (Colostrum)',
|
|
description: 'تقویت کننده سیستم ایمنی',
|
|
benefits: ['حاوی آنتی بادی'],
|
|
},
|
|
],
|
|
});
|
|
|
|
render(<IngredientWiki />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/آغوز گاو/)).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|