canina/tests/e2e/admin/admin-blogs-crud.spec.ts

132 lines
4.8 KiB
TypeScript

import { test, expect } from '../../fixtures';
test.describe('سناریوی مدیریت مقالات و وبلاگ در پنل ادمین (Admin Blogs & CMS Flow)', () => {
test('ورود به بخش مقالات -> باز کردن فرم ایجاد -> پر کردن عنوان و ذخیره نهایی', async ({ page }) => {
let blogCreated = false;
// Intercept and mock Admin Auth & Blogs API calls
await page.route('**/api/auth/admin-login*', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: {
accessToken: 'mock_e2e_admin_jwt_token',
user: { id: 'admin-1', email: 'admin@canina.ir', role: 'SUPER_ADMIN' },
},
}),
});
});
await page.route('**/api/auth/refresh*', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: {
accessToken: 'mock_e2e_admin_jwt_token',
user: { id: 'admin-1', email: 'admin@canina.ir', role: 'SUPER_ADMIN' },
},
}),
});
});
await page.route('**/api/users/profile*', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: { id: 'admin-1', email: 'admin@canina.ir', role: 'SUPER_ADMIN' },
}),
});
});
await page.route('**/api/admin/blogs/categories*', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ success: true, data: [] }),
});
});
await page.route('**/api/admin/blogs/tags*', async (route) => {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ success: true, data: [] }),
});
});
await page.route('**/api/admin/blogs*', async (route) => {
if (route.request().method() === 'GET') {
await route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
success: true,
data: [
{
id: 'blog-1',
title: 'راهنمای جامع سلامت مفاصل سگ و گربه',
slug: 'dog-joint-health-guide',
status: 'PUBLISHED',
views: 1250,
},
],
meta: { total: 1, lastPage: 1 },
}),
});
} else if (route.request().method() === 'POST') {
blogCreated = true;
await route.fulfill({
status: 201,
contentType: 'application/json',
body: JSON.stringify({ success: true, message: 'مقاله جدید با موفقیت ذخیره شد' }),
});
} else {
await route.continue();
}
});
// 1. لاگین ادمین
await page.goto('/login', { waitUntil: 'domcontentloaded' });
const emailInput = page.locator('input[type="email"]');
const passwordInput = page.locator('input[type="password"]');
await expect(emailInput).toBeVisible({ timeout: 10000 });
await emailInput.fill('admin@canina.ir');
await passwordInput.fill('Admin@123456');
const submitBtn = page.getByRole('button', { name: /ورود با رمز عبور|ورود به پنل|ورود/i }).first();
await submitBtn.click();
await page.waitForTimeout(500);
// 2. ورود به صفحه مدیریت مقالات وبلاگ
await page.goto('/blogs', { waitUntil: 'domcontentloaded' });
await expect(page).toHaveURL(/\/blogs/);
// 3. راستی‌آزمایی حضور هدر و دکمه مقاله جدید
const blogsHeading = page.locator('h2').filter({ hasText: 'مدیریت جامع وبلاگ' });
await expect(blogsHeading).toBeVisible({ timeout: 15000 });
const newBlogBtn = page.getByRole('button', { name: 'مقاله جدید' });
await expect(newBlogBtn).toBeVisible();
await newBlogBtn.click();
// 4. پر کردن عنوان در فرم مودال
const blogTitleInput = page.locator('[data-testid="blog-title-input"]').first();
await expect(blogTitleInput).toBeVisible({ timeout: 10000 });
await blogTitleInput.fill('مقاله آموزشی تستی پلی‌رایت');
// 5. ذخیره فرم با دکمه ذخیره مقاله
const saveBlogBtn = page.locator('[data-testid="blog-save-btn"]').first();
await expect(saveBlogBtn).toBeVisible({ timeout: 10000 });
await saveBlogBtn.click();
// راستی‌آزمایی دقیق ارسال موفقیت‌آمیز درخواست ساخت مقاله به سرور
await expect.poll(() => blogCreated, { timeout: 10000 }).toBe(true);
});
});