Files
clinicpro/assets/admin/components/ui/BackButton.test.tsx
T
hamed 6876135a53 feat: add BlogBodySanitizer for HTML sanitization on article save
- Implemented BlogBodySanitizer to clean HTML content before saving articles, ensuring security against XSS attacks.
- Added tests for BlogBodySanitizer to verify that unsafe tags and attributes are stripped from the content.
- Introduced ApiLeastPrivilegeTest to ensure that unauthorized users cannot access sensitive API routes, maintaining strict access control.
2026-08-07 21:13:38 +03:30

91 lines
3.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { screen, fireEvent, render } from '@testing-library/react';
import { MemoryRouter, Route, Routes, Link } from 'react-router';
import BackButton from './BackButton';
import PageHeader from './PageHeader';
/**
* رفتار «بازگشت» باید در همهٔ صفحات یکی باشد: یک قدم عقب در تاریخچهٔ پنل، و وقتی
* صفحه مستقیم باز شده (تاریخچه‌ای نیست) رفتن به صفحهٔ والد.
*/
function Detail({ fallback = '/admin/clinic-services' }: { fallback?: string }) {
return (
<>
<span>صفحهٔ جزئیات</span>
<BackButton fallback={fallback} />
</>
);
}
function List() {
return (
<>
<span>صفحهٔ لیست</span>
<Link to="/admin/clinic-services/x1">باز کردن جزئیات</Link>
</>
);
}
function renderAt(initialEntries: string[]) {
return render(
<MemoryRouter initialEntries={initialEntries}>
<Routes>
<Route path="/admin/clinic-services" element={<List />} />
<Route path="/admin/clinic-services/:uuid" element={<Detail />} />
</Routes>
</MemoryRouter>,
);
}
beforeEach(() => vi.clearAllMocks());
describe('BackButton', () => {
it('ظاهر یکسان دارد: همان دکمهٔ بازگشتِ صفحهٔ سرویس‌ها', () => {
renderAt(['/admin/clinic-services/x1']);
const btn = screen.getByRole('button', { name: /بازگشت/ });
expect(btn).toHaveClass('cp-btn-secondary');
expect(btn).toHaveAttribute('type', 'button');
});
it('وقتی از صفحهٔ دیگری آمده‌ایم، یک قدم به همان صفحه برمی‌گردد', () => {
renderAt(['/admin/clinic-services']);
fireEvent.click(screen.getByText('باز کردن جزئیات'));
expect(screen.getByText('صفحهٔ جزئیات')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /بازگشت/ }));
expect(screen.getByText('صفحهٔ لیست')).toBeInTheDocument();
});
it('در ورود مستقیم (بدون تاریخچه) به صفحهٔ والد می‌رود', () => {
renderAt(['/admin/clinic-services/x1']);
fireEvent.click(screen.getByRole('button', { name: /بازگشت/ }));
expect(screen.getByText('صفحهٔ لیست')).toBeInTheDocument();
});
});
describe('PageHeader — backTo', () => {
it('با backTo دکمهٔ بازگشت را نشان می‌دهد', () => {
render(
<MemoryRouter initialEntries={['/admin/claims/p1']}>
<PageHeader title="پروندهٔ بیمه" backTo="/admin/claims" />
</MemoryRouter>,
);
expect(screen.getByRole('button', { name: /بازگشت/ })).toBeInTheDocument();
});
it('بدون backTo دکمه‌ای رندر نمی‌شود (صفحات سطح‌اول)', () => {
render(
<MemoryRouter>
<PageHeader title="مطالبات بیمه" />
</MemoryRouter>,
);
expect(screen.queryByRole('button', { name: /بازگشت/ })).not.toBeInTheDocument();
});
});