Files
clinicpro/assets/admin/pages/BlogsPage.test.tsx
T
hamed 6084dc5d6b feat: add comprehensive tests for UI components, hooks, and API interactions
- Implement tests for Pagination, StatusBadge, ConfirmDialog, and MobileInput components.
- Add tests for useSubscription, usePaymentConfig, and usePwaInstall hooks.
- Create tests for API requests in the api module, including success and error handling.
- Add utility function tests for formatting and validating Iranian mobile numbers.
- Implement tests for BlogFormPage and BlogsPage to validate form submissions and data fetching.
- Add tests for LoginPage to ensure proper validation and state management.
- Create tests for authStore and uiStore to validate state management and functionality.
- Set up Vitest configuration and testing utilities for consistent testing environment.
2026-06-28 22:58:15 +03:30

52 lines
1.9 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen } from '@testing-library/react';
import { renderWithProviders } from '@/test/utils';
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
vi.mock('@/lib/api', () => ({
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
ApiError: class extends Error {},
}));
import { api } from '@/lib/api';
import BlogsPage from '@/pages/BlogsPage';
const get = api.get as ReturnType<typeof vi.fn>;
beforeEach(() => {
get.mockReset();
});
describe('BlogsPage — قرارداد PaginatedResponse', () => {
it('ردیف‌ها از data.data و تعداد از meta.totalRecords می‌آیند', async () => {
get.mockResolvedValue({
success: true,
data: [
{ uuid: 'b1', title: 'مقاله اول', status: 'published', tags: [], created_at: 1700000000 },
{ uuid: 'b2', title: 'مقاله دوم', status: 'draft', tags: [], created_at: 1700000000 },
],
meta: { totalRecords: 2, totalPages: 1, currentPage: 1 },
errors: [],
});
renderWithProviders(<BlogsPage />, { route: '/admin/blogs' });
expect(await screen.findByText('مقاله اول')).toBeInTheDocument();
expect(screen.getByText('مقاله دوم')).toBeInTheDocument();
expect(get).toHaveBeenCalledWith(expect.stringContaining('/api/v1/blogs'));
});
it('پاسخ خالی → پیام «هیچ مقاله‌ای یافت نشد»', async () => {
get.mockResolvedValue({
success: true,
data: [],
meta: { totalRecords: 0, totalPages: 0, currentPage: 1 },
errors: [],
});
renderWithProviders(<BlogsPage />, { route: '/admin/blogs' });
expect(await screen.findByText('هیچ مقاله‌ای یافت نشد')).toBeInTheDocument();
});
});