feat(blog): add admin endpoint for blog details and cache invalidation

- Implemented `adminDetail()` method in `BlogController` to retrieve blog posts of any status for admin editing.
- Introduced `BlogCacheInvalidator` service to handle cache invalidation after blog create/update/delete actions.
- Updated existing methods in `BlogController` and `RepresentationBlogController` to call cache invalidation on blog modifications.
- Enhanced `BlogFormPage` and `RepresentationBlogFormPage` to utilize the new admin endpoint for fetching blog data.
- Added tests for `BlogCacheInvalidator` to ensure proper functionality and error handling.
- Updated documentation to reflect new API endpoint and cache invalidation behavior.
This commit is contained in:
hamed
2026-07-27 18:54:35 +03:30
parent e4edaea9b8
commit 15abcb5c8a
12 changed files with 972 additions and 47 deletions
+81
View File
@@ -1,6 +1,7 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Routes, Route } from 'react-router-dom';
import { renderWithProviders } from '@/test/utils';
vi.mock('@ckeditor/ckeditor5-react', () => ({ CKEditor: () => null }));
@@ -45,3 +46,83 @@ describe('BlogFormPage — اعتبارسنجی zod (حالت ساخت)', () =>
expect(post).not.toHaveBeenCalled();
});
});
const BLOG_UUID = '56fd9a20-9594-4aa1-a651-346fa86720bd';
function renderEditPage() {
return renderWithProviders(
<Routes>
<Route path="/admin/blogs/:uuid/edit" element={<BlogFormPage />} />
</Routes>,
{ route: `/admin/blogs/${BLOG_UUID}/edit` }
);
}
describe('BlogFormPage — حالت ویرایش', () => {
it('پیش‌نویس را از اندپوینت ادمین می‌گیرد و فرم را پر می‌کند', async () => {
get.mockImplementation((url: string) =>
url.startsWith(`/api/v1/admin/blog/${BLOG_UUID}`)
? Promise.resolve({
data: {
data: {
uuid: BLOG_UUID,
title: 'عنوان پیش‌نویس',
body: '<p>محتوای تست</p>',
summary: 'خلاصهٔ تست',
status: 'draft',
tags: ['الف', 'ب'],
},
},
})
: Promise.resolve({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } })
);
renderEditPage();
expect(await screen.findByDisplayValue('عنوان پیش‌نویس')).toBeInTheDocument();
expect(screen.getByDisplayValue('خلاصهٔ تست')).toBeInTheDocument();
expect(screen.getByDisplayValue('الف, ب')).toBeInTheDocument();
// اندپوینت عمومی (که پیش‌نویس را ۴۰۴ می‌کرد) نباید صدا زده شود
expect(get).not.toHaveBeenCalledWith(`/api/v1/blog/${BLOG_UUID}`);
});
it('خطای بارگذاری → کارت خطا به‌جای فرم خالیِ قابل‌ثبت', async () => {
get.mockImplementation((url: string) =>
url.startsWith(`/api/v1/admin/blog/${BLOG_UUID}`)
? Promise.reject(new Error('مقاله یافت نشد'))
: Promise.resolve({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } })
);
renderEditPage();
expect(await screen.findByText('مقاله یافت نشد')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'بروزرسانی' })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: 'بازگشت به فهرست مقالات' })).toBeInTheDocument();
});
it('مقالهٔ سراسری با faq و کلیدواژهٔ خالی بدون خطا بارگذاری می‌شود', async () => {
get.mockImplementation((url: string) =>
url.startsWith(`/api/v1/admin/blog/${BLOG_UUID}`)
? Promise.resolve({
data: {
data: {
uuid: BLOG_UUID,
title: 'مقالهٔ سراسری',
body: '<p>x</p>',
status: 'draft',
tags: [],
faq: [],
secondary_keywords: [],
city: null,
},
},
})
: Promise.resolve({ data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } })
);
renderEditPage();
expect(await screen.findByDisplayValue('مقالهٔ سراسری')).toBeInTheDocument();
expect(screen.getByText('سوالی افزوده نشده است.')).toBeInTheDocument();
});
});