- 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.
262 lines
9.9 KiB
TypeScript
262 lines
9.9 KiB
TypeScript
import { fireEvent,render,screen,waitFor } from '@testing-library/react';
|
|
import { toast } from 'react-hot-toast';
|
|
import { beforeEach,describe,expect,it,vi } from 'vitest';
|
|
import api from '../services/api';
|
|
import TestimonialsManager from './TestimonialsManager';
|
|
|
|
vi.mock('../services/api', () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
patch: vi.fn(),
|
|
delete: vi.fn(),
|
|
},
|
|
BASE_DOMAIN: 'https://api.example.com',
|
|
}));
|
|
|
|
vi.mock('react-hot-toast', () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('../components/ui/MediaSelector', () => ({
|
|
default: ({ isOpen, onClose, onSelect }: any) => {
|
|
if (!isOpen) return null;
|
|
return (
|
|
<div data-testid="media-selector-modal">
|
|
<button onClick={() => { onSelect('/uploads/selected-avatar.jpg'); onClose(); }}>
|
|
انتخاب عکس از گالری
|
|
</button>
|
|
<button onClick={onClose}>بستن مدال رسانه</button>
|
|
</div>
|
|
);
|
|
},
|
|
}));
|
|
|
|
describe('TestimonialsManager Component', () => {
|
|
const mockTestimonials = [
|
|
{
|
|
id: 't-1',
|
|
authorName: 'سارا احمدی',
|
|
roleTitle: 'سرپرست سگ ژرمن شپرد',
|
|
avatarUrl: '/uploads/sara.jpg',
|
|
content: 'مکمل مفصلساز کانیهیدروکس معجزه کرد و سگ من دوباره راحت راه میرود.',
|
|
rating: 5,
|
|
isFeatured: true,
|
|
isActive: true,
|
|
showOnHome: true,
|
|
order: 1,
|
|
createdAt: '2026-03-01T12:00:00Z',
|
|
},
|
|
{
|
|
id: 't-2',
|
|
authorName: 'دکتر علیرضا کاظمی',
|
|
roleTitle: 'دامپزشک جراح',
|
|
avatarUrl: '',
|
|
content: 'توصیه همیشگی کلینیک ما برای تقویت سیستم ایمنی استفاده از محصولات کنینا است.',
|
|
rating: 4,
|
|
isFeatured: false,
|
|
isActive: false,
|
|
showOnHome: false,
|
|
order: 2,
|
|
createdAt: '2026-03-05T15:30:00Z',
|
|
},
|
|
];
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
(api.get as any).mockResolvedValue({ data: mockTestimonials });
|
|
});
|
|
|
|
it('renders testimonials list correctly', async () => {
|
|
render(<TestimonialsManager />);
|
|
|
|
expect(api.get).toHaveBeenCalledWith('/testimonials');
|
|
|
|
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();
|
|
expect(screen.getByText('خانه')).toBeInTheDocument();
|
|
});
|
|
|
|
it('toggles active status of a testimonial', async () => {
|
|
(api.patch as any).mockResolvedValue({ data: { success: true } });
|
|
|
|
render(<TestimonialsManager />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('سارا احمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const activeBadge = screen.getByText('فعال');
|
|
fireEvent.click(activeBadge);
|
|
|
|
await waitFor(() => {
|
|
expect(api.patch).toHaveBeenCalledWith('/testimonials/t-1', { isActive: false });
|
|
expect(toast.success).toHaveBeenCalledWith('وضعیت نظر تغییر کرد');
|
|
});
|
|
});
|
|
|
|
it('opens modal to create a new testimonial and submits successfully', async () => {
|
|
(api.post as any).mockResolvedValue({ data: { success: true } });
|
|
|
|
render(<TestimonialsManager />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('سارا احمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const addBtn = screen.getByRole('button', { name: /افزودن نظر جدید/ });
|
|
fireEvent.click(addBtn);
|
|
|
|
expect(screen.getByRole('heading', { name: 'افزودن نظر جدید' })).toBeInTheDocument();
|
|
|
|
const nameInput = screen.getByPlaceholderText('مثال: دکتر علیرضا رضایی');
|
|
const roleInput = screen.getByPlaceholderText('مثال: متخصص جراحی دامپزشکی');
|
|
const contentInput = screen.getByPlaceholderText('متن کامل نظر و تجربه همکاری با محصولات کنینا...');
|
|
|
|
fireEvent.change(nameInput, { target: { value: 'کامران شریفی' } });
|
|
fireEvent.change(roleInput, { target: { value: 'سرپرست هاسکی' } });
|
|
fireEvent.change(contentInput, { target: { value: 'عالی بود و تأثیر سریع داشت.' } });
|
|
|
|
const form = document.getElementById('testimonialForm') as HTMLFormElement;
|
|
fireEvent.submit(form);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith(
|
|
'/testimonials',
|
|
expect.objectContaining({
|
|
authorName: 'کامران شریفی',
|
|
roleTitle: 'سرپرست هاسکی',
|
|
content: 'عالی بود و تأثیر سریع داشت.',
|
|
rating: 5,
|
|
isFeatured: true,
|
|
isActive: true,
|
|
showOnHome: false,
|
|
})
|
|
);
|
|
expect(toast.success).toHaveBeenCalledWith('نظر جدید با موفقیت ایجاد شد');
|
|
});
|
|
});
|
|
|
|
it('opens modal to edit an existing testimonial and saves changes', async () => {
|
|
(api.patch as any).mockResolvedValue({ data: { success: true } });
|
|
|
|
render(<TestimonialsManager />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('سارا احمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const rows = screen.getAllByRole('row');
|
|
const firstRowButtons = rows[1].querySelectorAll('button');
|
|
// Button 1 is edit
|
|
fireEvent.click(firstRowButtons[1]);
|
|
|
|
expect(screen.getByText('ویرایش نظر')).toBeInTheDocument();
|
|
|
|
const nameInput = screen.getByPlaceholderText('مثال: دکتر علیرضا رضایی');
|
|
expect(nameInput).toHaveValue('سارا احمدی');
|
|
|
|
fireEvent.change(nameInput, { target: { value: 'سارا احمدی پور' } });
|
|
|
|
const form = document.getElementById('testimonialForm') as HTMLFormElement;
|
|
fireEvent.submit(form);
|
|
|
|
await waitFor(() => {
|
|
expect(api.patch).toHaveBeenCalledWith(
|
|
'/testimonials/t-1',
|
|
expect.objectContaining({
|
|
authorName: 'سارا احمدی پور',
|
|
roleTitle: 'سرپرست سگ ژرمن شپرد',
|
|
})
|
|
);
|
|
expect(toast.success).toHaveBeenCalledWith('نظر با موفقیت بروزرسانی شد');
|
|
});
|
|
});
|
|
|
|
it('deletes a testimonial after confirming modal', async () => {
|
|
(api.delete as any).mockResolvedValue({ data: { success: true } });
|
|
|
|
render(<TestimonialsManager />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('سارا احمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const rows = screen.getAllByRole('row');
|
|
const firstRowButtons = rows[1].querySelectorAll('button');
|
|
// Delete is buttons[2]
|
|
fireEvent.click(firstRowButtons[2]);
|
|
|
|
expect(screen.getByText('حذف نظر')).toBeInTheDocument();
|
|
expect(screen.getByText('آیا از حذف این نظر اطمینان دارید؟')).toBeInTheDocument();
|
|
|
|
const confirmBtn = screen.getByRole('button', { name: 'تایید' });
|
|
fireEvent.click(confirmBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.delete).toHaveBeenCalledWith('/testimonials/t-1');
|
|
expect(toast.success).toHaveBeenCalledWith('نظر با موفقیت حذف شد');
|
|
});
|
|
});
|
|
|
|
it('validates required fields on submission', async () => {
|
|
render(<TestimonialsManager />);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByRole('button', { name: /افزودن نظر جدید/ })).toBeInTheDocument();
|
|
});
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: /افزودن نظر جدید/ }));
|
|
|
|
// Empty fields
|
|
const nameInput = screen.getByPlaceholderText('مثال: دکتر علیرضا رضایی');
|
|
const contentInput = screen.getByPlaceholderText('متن کامل نظر و تجربه همکاری با محصولات کنینا...');
|
|
fireEvent.change(nameInput, { target: { value: ' ' } });
|
|
fireEvent.change(contentInput, { target: { value: ' ' } });
|
|
|
|
const form = document.getElementById('testimonialForm') as HTMLFormElement;
|
|
fireEvent.submit(form);
|
|
|
|
await waitFor(() => {
|
|
expect(toast.error).toHaveBeenCalledWith('نام نویسنده و متن نظر اجباری هستند');
|
|
});
|
|
expect(api.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('reports failed initial fetch', async () => {
|
|
vi.mocked(api.get).mockRejectedValueOnce(new Error('offline'));
|
|
render(<TestimonialsManager />);
|
|
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در دریافت لیست نظرات و گواهیها'));
|
|
});
|
|
|
|
it('does not report success or refresh when status toggle fails', async () => {
|
|
vi.mocked(api.patch).mockRejectedValueOnce(new Error('offline'));
|
|
render(<TestimonialsManager />);
|
|
fireEvent.click(await screen.findByText('فعال'));
|
|
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در تغییر وضعیت نظر'));
|
|
expect(api.get).toHaveBeenCalledTimes(1);
|
|
expect(toast.success).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('retains edited content when saving fails', async () => {
|
|
vi.mocked(api.post).mockRejectedValueOnce(new Error('offline'));
|
|
render(<TestimonialsManager />); await screen.findByText('سارا احمدی');
|
|
fireEvent.click(screen.getByRole('button', { name: /افزودن نظر جدید/ }));
|
|
fireEvent.change(screen.getByPlaceholderText('مثال: دکتر علیرضا رضایی'), { target: { value: 'Draft author' } });
|
|
fireEvent.change(screen.getByPlaceholderText('متن کامل نظر و تجربه همکاری با محصولات کنینا...'), { target: { value: 'Draft content' } });
|
|
fireEvent.submit(document.getElementById('testimonialForm')!);
|
|
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در ذخیره نظر'));
|
|
expect(screen.getByDisplayValue('Draft content')).toBeInTheDocument();
|
|
});
|
|
});
|