feat(admin): configure vitest test runner and fix dashboard audit-logs array guards [FE-003]
This commit is contained in:
1 parent
7879ead486
commit
f91502026b
3 files changed
+23
-12
No files matched your search
@@ -7,7 +7,10 @@
|
||||
"dev": "vite --port 3400 --host 127.0.0.1",
|
||||
"build": "vite build",
|
||||
"lint": "eslint .",
|
||||
"preview": "vite preview"
|
||||
"preview": "vite preview",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tanstack/react-query": "^5.101.0",
|
||||
@@ -42,4 +45,4 @@
|
||||
"typescript-eslint": "^8.59.2",
|
||||
"vitest": "^4.1.11"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,11 +43,14 @@ describe('Dashboard Page Component', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(api.get).mockResolvedValue({
|
||||
data: {
|
||||
success: true,
|
||||
data: mockStats,
|
||||
},
|
||||
vi.mocked(api.get).mockImplementation((url: string) => {
|
||||
if (url.includes('/admin/audit-logs')) {
|
||||
return Promise.resolve({ data: { success: true, data: [] } } as any);
|
||||
}
|
||||
if (url.includes('/admin/approvals')) {
|
||||
return Promise.resolve({ data: { success: true, meta: { pendingCount: 0 } } } as any);
|
||||
}
|
||||
return Promise.resolve({ data: { success: true, data: mockStats } } as any);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,8 +65,8 @@ describe('Dashboard Page Component', () => {
|
||||
it('renders dashboard headers and initial zero or placeholder stats', async () => {
|
||||
renderDashboard();
|
||||
|
||||
expect(screen.getByText('داشبورد')).toBeInTheDocument();
|
||||
expect(screen.getByText('خلاصهای از وضعیت فروشگاه کنینا')).toBeInTheDocument();
|
||||
expect(screen.getByText('داشبورد فرماندهی کنینا')).toBeInTheDocument();
|
||||
expect(screen.getByText('خلاصه عملیات، وضعیت دیتابیس و نظارت بر وظایف خودکار سازمان')).toBeInTheDocument();
|
||||
expect(screen.getByText('درآمد کل')).toBeInTheDocument();
|
||||
expect(screen.getByText('سفارشات جدید')).toBeInTheDocument();
|
||||
expect(screen.getByText('کاربران فعال')).toBeInTheDocument();
|
||||
@@ -123,7 +126,12 @@ describe('Dashboard Page Component', () => {
|
||||
|
||||
it('falls back gracefully when API returns error', async () => {
|
||||
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
vi.mocked(api.get).mockRejectedValueOnce(new Error('Network error'));
|
||||
vi.mocked(api.get).mockImplementation((url: string) => {
|
||||
if (url.includes('/admin/dashboard/stats')) {
|
||||
return Promise.reject(new Error('Network error'));
|
||||
}
|
||||
return Promise.resolve({ data: { success: true, data: [] } } as any);
|
||||
});
|
||||
|
||||
renderDashboard();
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ export default function Dashboard() {
|
||||
try {
|
||||
const res = await api.get('/admin/audit-logs?limit=5');
|
||||
if (res.data?.success) {
|
||||
setRecentLogs(res.data.data);
|
||||
setRecentLogs(Array.isArray(res.data.data) ? res.data.data : (Array.isArray(res.data.data?.logs) ? res.data.data.logs : []));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch audit logs widget', e);
|
||||
@@ -217,7 +217,7 @@ export default function Dashboard() {
|
||||
رکوردی در تاریخچه ثبت نشده است.
|
||||
</div>
|
||||
) : (
|
||||
recentLogs.map((log) => (
|
||||
(Array.isArray(recentLogs) ? recentLogs : []).map((log) => (
|
||||
<div key={log.id} className="p-3 bg-gray-50 rounded-xl flex items-center justify-between text-xs hover:bg-purple-50/40 transition-colors">
|
||||
<div className="flex items-center gap-2.5 overflow-hidden">
|
||||
<span className="w-2 h-2 rounded-full bg-purple-500 shrink-0" />
|
||||
|
||||
Reference in new issue
Block a user