Files
canina/frontend/admin-panel/src/pages/Pets.test.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

26 lines
5.0 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 Pets from './Pets';
vi.mock('../services/api', () => ({ default: { get: vi.fn(), delete: vi.fn() }, BASE_DOMAIN: 'https://api.example.com' }));
vi.mock('react-hot-toast', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
const pet = { id: 'p1', name: 'Milo', type: 'گربه', breed: 'Persian', age: 2, weight: 3, imageUrl: '/uploads/milo.jpg', user: { firstName: 'Owner', lastName: 'One', mobile: '09120000000' }, medicalConditions: [{ condition: 'Allergy' }], reminders: [{ id: 'r1', title: 'Daily pill', time: '08:00', frequency: 'daily' }], healthLogs: [{ id: 'h1', appetite: 'good', energy: 'normal', digestion: 'normal', note: 'Improved today', loggedDate: '2026-03-01', createdAt: '2026-03-01' }], prescriptions: [{ id: 'rx-12345678', status: 'APPROVED', notes: 'Patient note', adminNotes: 'Clinical note', createdAt: '2026-03-01' }] };
beforeEach(() => { vi.resetAllMocks(); vi.mocked(api.get).mockResolvedValue({ data: { data: [pet], meta: { lastPage: 2 } } }); vi.mocked(api.delete).mockResolvedValue({ data: {} }); });
async function load() { render(<Pets />); await screen.findByText('Milo'); }
describe('Pets', () => {
it('loads paginated pets with owner data and relative image URL', async () => { await load(); expect(api.get).toHaveBeenCalledWith('/admin/pets', { params: { page: 1, limit: 10, search: '' } }); expect(screen.getByText('Owner One')).toBeInTheDocument(); expect(screen.getByAltText('Milo')).toHaveAttribute('src', 'https://api.example.com/uploads/milo.jpg'); expect(screen.getByText('1 یادآور • 1 گزارش')).toBeInTheDocument(); });
it('opens complete health dossier and closes without mutation', async () => { await load(); fireEvent.click(screen.getByTitle('مشاهده پرونده کامل سلامت')); for (const text of ['Allergy', 'Daily pill', 'Improved today', 'APPROVED']) expect(screen.getByText(text)).toBeInTheDocument(); expect(screen.getByText(/Clinical note/)).toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'بستن پرونده' })); expect(screen.queryByText('Allergy')).not.toBeInTheDocument(); expect(api.delete).not.toHaveBeenCalled(); });
it('changes pages then resets page on debounced search', async () => {
await load(); fireEvent.click(screen.getByRole('button', { name: '۲' }));
await waitFor(() => expect(api.get).toHaveBeenCalledWith('/admin/pets', { params: { page: 2, limit: 10, search: '' } }), { timeout: 2000 });
fireEvent.change(screen.getByPlaceholderText('جستجو در نام همدم...'), { target: { value: 'Milo' } });
await waitFor(() => expect(api.get).toHaveBeenCalledWith('/admin/pets', { params: { page: 1, limit: 10, search: 'Milo' } }), { timeout: 2000 });
});
it('cancels delete without contacting API', async () => { await load(); fireEvent.click(screen.getByTitle('حذف پت')); fireEvent.click(screen.getByRole('button', { name: 'لغو' })); expect(api.delete).not.toHaveBeenCalled(); });
it('deletes only after confirmation and refreshes the list', async () => { await load(); fireEvent.click(screen.getByTitle('حذف پت')); vi.mocked(api.get).mockResolvedValue({ data: { data: [], meta: { lastPage: 1 } } }); fireEvent.click(screen.getByRole('button', { name: 'تایید' })); await waitFor(() => expect(api.delete).toHaveBeenCalledWith('/admin/pets/p1')); expect(await screen.findByText('هیچ حیوانی یافت نشد')).toBeInTheDocument(); expect(toast.success).toHaveBeenCalledWith('پروفایل حیوان خانگی با موفقیت حذف شد'); });
it('reports rejected deletion and retains profile', async () => { vi.mocked(api.delete).mockRejectedValue(new Error('offline')); await load(); fireEvent.click(screen.getByTitle('حذف پت')); fireEvent.click(screen.getByRole('button', { name: 'تایید' })); await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در حذف پروفایل')); expect(screen.getByText('Milo')).toBeInTheDocument(); expect(toast.success).not.toHaveBeenCalled(); });
it('clears loading after failed initial fetch', async () => { vi.mocked(api.get).mockRejectedValue(new Error('offline')); render(<Pets />); expect(await screen.findByText('هیچ حیوانی یافت نشد')).toBeInTheDocument(); expect(toast.success).not.toHaveBeenCalled(); });
it('renders missing optional dossier sections safely', async () => { vi.mocked(api.get).mockResolvedValue({ data: { data: [{ id: 'p2', name: 'Minimal' }] } }); render(<Pets />); await screen.findByText('Minimal'); expect(screen.getByText('نژاد نامشخص')).toBeInTheDocument(); fireEvent.click(screen.getByTitle('مشاهده پرونده کامل سلامت')); for (const text of ['مورد پزشکی خاصی ثبت نشده است.', 'یادآوری فعالی ثبت نشده است.', 'گزارش سلامتی ثبت نشده است.', 'نسخه‌ای برای این پت ثبت نشده است.']) expect(screen.getByText(text)).toBeInTheDocument(); });
});