- 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.
174 lines
5.8 KiB
TypeScript
174 lines
5.8 KiB
TypeScript
import React from 'react';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
import TestimonialsSection from '../TestimonialsSection';
|
|
import api from '../../lib/services/api';
|
|
import { useSettingsStore } from '../../lib/store/settingsStore';
|
|
|
|
// Mock motion/react
|
|
vi.mock('motion/react', () => ({
|
|
motion: {
|
|
div: ({ children, className, ...props }: any) => <div className={className} {...props}>{children}</div>,
|
|
},
|
|
}));
|
|
|
|
// Mock SafeImage
|
|
vi.mock('../SafeImage', () => ({
|
|
default: ({ src, alt, className }: any) => (
|
|
<img src={src} alt={alt} className={className} data-testid="safe-image" />
|
|
),
|
|
}));
|
|
|
|
// Mock api service
|
|
vi.mock('../../lib/services/api', () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
},
|
|
BASE_DOMAIN: 'https://api.canina.ir',
|
|
}));
|
|
|
|
describe('TestimonialsSection Component', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
useSettingsStore.setState({ texts: {} });
|
|
});
|
|
|
|
it('renders nothing when API returns empty testimonials array', async () => {
|
|
(api.get as any).mockResolvedValueOnce({ data: [] });
|
|
|
|
const { container } = render(<TestimonialsSection />);
|
|
|
|
await waitFor(() => {
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
});
|
|
|
|
it('renders nothing gracefully on API error', async () => {
|
|
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
(api.get as any).mockRejectedValueOnce(new Error('Network error'));
|
|
|
|
const { container } = render(<TestimonialsSection />);
|
|
|
|
await waitFor(() => {
|
|
expect(container.firstChild).toBeNull();
|
|
});
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
|
|
it('renders testimonials correctly when data is nested in data.testimonials and filters out inactive items', async () => {
|
|
const mockData = {
|
|
data: {
|
|
testimonials: [
|
|
{
|
|
id: 't-1',
|
|
authorName: 'دکتر علیرضا کاظمی',
|
|
roleTitle: 'متخصص جراحی دامپزشکی',
|
|
avatarUrl: '/uploads/avatars/kazemi.jpg',
|
|
content: 'کیفیت کلسیم و مفاصل کنینا فوقالعاده است.',
|
|
rating: 5,
|
|
isActive: true,
|
|
isFeatured: true,
|
|
},
|
|
{
|
|
id: 't-2',
|
|
authorName: 'سارا رضایی',
|
|
roleTitle: 'سرپرست ژرمن شپرد',
|
|
avatarUrl: 'https://cdn.example.com/sara.jpg',
|
|
content: 'پوشش موهای سگم بعد از یک ماه براق شد.',
|
|
rating: 4,
|
|
isActive: true,
|
|
isFeatured: false,
|
|
},
|
|
{
|
|
id: 't-3',
|
|
authorName: 'کاربر غیرفعال',
|
|
content: 'این پیام نباید نمایش داده شود.',
|
|
isActive: false,
|
|
rating: 5,
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
(api.get as any).mockResolvedValueOnce({ data: mockData });
|
|
|
|
render(<TestimonialsSection />);
|
|
|
|
// Wait for data to load
|
|
await waitFor(() => {
|
|
expect(screen.getByText('دکتر علیرضا کاظمی')).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.getByText('متخصص جراحی دامپزشکی')).toBeInTheDocument();
|
|
expect(screen.getByText(/کیفیت کلسیم و مفاصل کنینا فوقالعاده است/)).toBeInTheDocument();
|
|
expect(screen.getByText('سارا رضایی')).toBeInTheDocument();
|
|
expect(screen.getByText('سرپرست ژرمن شپرد')).toBeInTheDocument();
|
|
expect(screen.getByText(/پوشش موهای سگم بعد از یک ماه براق شد/)).toBeInTheDocument();
|
|
|
|
// Verify inactive testimonial is filtered out
|
|
expect(screen.queryByText('کاربر غیرفعال')).not.toBeInTheDocument();
|
|
|
|
// Check images (relative vs absolute url handling)
|
|
const images = screen.getAllByTestId('safe-image');
|
|
expect(images[0]).toHaveAttribute('src', 'https://api.canina.ir/uploads/avatars/kazemi.jpg');
|
|
expect(images[1]).toHaveAttribute('src', 'https://cdn.example.com/sara.jpg');
|
|
});
|
|
|
|
it('renders fallback icon when testimonial does not have avatarUrl', async () => {
|
|
const mockData = {
|
|
testimonials: [
|
|
{
|
|
id: 't-no-avatar',
|
|
authorName: 'مریم احمدی',
|
|
content: 'تجربه بسیار خوبی در خرید این مکمل داشتم.',
|
|
rating: 5,
|
|
isActive: true,
|
|
},
|
|
],
|
|
};
|
|
|
|
(api.get as any).mockResolvedValueOnce({ data: mockData });
|
|
|
|
render(<TestimonialsSection />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('مریم احمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.queryByTestId('safe-image')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('uses custom titles and badges from useSettingsStore texts', async () => {
|
|
useSettingsStore.setState({
|
|
texts: {
|
|
testimonials_badge: 'دیدگاههای معتبر',
|
|
testimonials_title_1: 'نظرات و',
|
|
testimonials_title_2: 'تجارب همکاران',
|
|
testimonials_desc: 'توضیحات سفارشی درباره نظرات دامپزشکان',
|
|
},
|
|
});
|
|
|
|
const mockData = [
|
|
{
|
|
id: 't-1',
|
|
authorName: 'دکتر نادری',
|
|
content: 'عالی بود',
|
|
rating: 5,
|
|
isActive: true,
|
|
},
|
|
];
|
|
|
|
(api.get as any).mockResolvedValueOnce({ data: mockData });
|
|
|
|
render(<TestimonialsSection />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('دیدگاههای معتبر')).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.getByText('نظرات و')).toBeInTheDocument();
|
|
expect(screen.getByText('تجارب همکاران')).toBeInTheDocument();
|
|
expect(screen.getByText('توضیحات سفارشی درباره نظرات دامپزشکان')).toBeInTheDocument();
|
|
});
|
|
});
|