Files
clinicpro/assets/admin/pages/BlogFormPage.test.tsx
T
hamed a4b07c2f80 feat(blog): add city_id to blogs for city-specific scoping
- Introduced a new nullable city_id column in the blogs table to allow scoping of blog posts to specific cities.
- Updated Blog entity to include a ManyToOne relationship with the City entity.
- Enhanced BlogController to handle city_id in the request, allowing filtering of posts by city.
- Modified BlogRepository to support querying published posts based on city_id.
- Added tests to ensure correct behavior for city-scoped and nationwide posts, including creation and updating of posts with city associations.
2026-07-19 08:23:57 +03:30

48 lines
2.1 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWithProviders } from '@/test/utils';
vi.mock('@ckeditor/ckeditor5-react', () => ({ CKEditor: () => null }));
vi.mock('@ckeditor/ckeditor5-build-classic', () => ({ default: {} }));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
vi.mock('@/components/ui/SearchableSelect', () => ({ default: () => null }));
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 BlogFormPage from '@/pages/BlogFormPage';
const post = api.post as ReturnType<typeof vi.fn>;
const get = api.get as ReturnType<typeof vi.fn>;
beforeEach(() => {
post.mockReset();
// فرم شهرها را برای انتخابگر «سراسری / شهر» می‌گیرد
get.mockReset();
get.mockResolvedValue({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } });
});
describe('BlogFormPage — اعتبارسنجی zod (حالت ساخت)', () => {
it('submit با فیلدهای خالی → خطای عنوان و جلوگیری از فراخوانی API', async () => {
renderWithProviders(<BlogFormPage />, { route: '/admin/blogs/new' });
await userEvent.click(screen.getByRole('button', { name: 'ذخیره' }));
expect(await screen.findByText('عنوان الزامی است')).toBeInTheDocument();
expect(post).not.toHaveBeenCalled();
});
it('عنوان کوتاه‌تر از ۳ کاراکتر هم خطا می‌دهد', async () => {
renderWithProviders(<BlogFormPage />, { route: '/admin/blogs/new' });
await userEvent.type(screen.getByPlaceholderText('عنوان جذاب بنویسید...'), 'اب');
await userEvent.click(screen.getByRole('button', { name: 'ذخیره' }));
expect(await screen.findByText('عنوان الزامی است')).toBeInTheDocument();
expect(post).not.toHaveBeenCalled();
});
});