test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -0,0 +1,224 @@
|
||||
import { fireEvent,render,screen,waitFor } from '@testing-library/react';
|
||||
import { MemoryRouter,Route,Routes } from 'react-router-dom';
|
||||
import { beforeEach,describe,expect,it,vi } from 'vitest';
|
||||
import api from '../services/api';
|
||||
import { useAdminAuthStore } from '../store/adminAuthStore';
|
||||
import Layout from './Layout';
|
||||
import Sidebar from './Sidebar';
|
||||
import Topbar from './Topbar';
|
||||
|
||||
vi.mock('../services/api', () => ({
|
||||
default: {
|
||||
get: vi.fn().mockResolvedValue({ data: { success: true, data: { newOrders: 5 } } }),
|
||||
post: vi.fn().mockResolvedValue({ data: { success: true } }),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('Admin Panel Navigation Components', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
useAdminAuthStore.getState().clearAuth();
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
describe('Sidebar component', () => {
|
||||
it('renders navigation group headings and sub-menu items', () => {
|
||||
const setIsOpen = vi.fn();
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Sidebar isOpen={true} setIsOpen={setIsOpen} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
// Verify main group titles
|
||||
expect(screen.getByText('داشبورد و تحلیل داده')).toBeInTheDocument();
|
||||
expect(screen.getByText('سفارشات و مالی')).toBeInTheDocument();
|
||||
expect(screen.getByText('محصولات و کاتالوگ')).toBeInTheDocument();
|
||||
|
||||
// Default active group is 'main', check its items
|
||||
expect(screen.getByText('نمای کلی داشبورد')).toBeInTheDocument();
|
||||
expect(screen.getByText('گزارشات و تحلیل فروش')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('highlights the active route based on current path and auto-expands its group', () => {
|
||||
const setIsOpen = vi.fn();
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/orders']}>
|
||||
<Sidebar isOpen={true} setIsOpen={setIsOpen} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
// Group 'سفارشات و مالی' should be open
|
||||
const ordersLink = screen.getByRole('link', { name: /مدیریت سفارشات/i });
|
||||
expect(ordersLink).toBeInTheDocument();
|
||||
expect(ordersLink.className).toContain('bg-purple-600');
|
||||
});
|
||||
|
||||
it('toggles groups when clicking group accordion buttons', () => {
|
||||
const setIsOpen = vi.fn();
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Sidebar isOpen={true} setIsOpen={setIsOpen} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
// Initially products group items are collapsed
|
||||
expect(screen.queryByText('لیست محصولات')).not.toBeInTheDocument();
|
||||
|
||||
// Click group button
|
||||
const productsGroupBtn = screen.getByRole('button', { name: /محصولات و کاتالوگ/i });
|
||||
fireEvent.click(productsGroupBtn);
|
||||
|
||||
// Now items should be visible
|
||||
expect(screen.getByText('لیست محصولات')).toBeInTheDocument();
|
||||
expect(screen.getByText('دستهبندیهای تخصصی')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders badges when fetched from stats API', async () => {
|
||||
vi.mocked(api.get).mockResolvedValueOnce({
|
||||
data: { success: true, data: { newOrders: 7 } },
|
||||
});
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/orders']}>
|
||||
<Sidebar isOpen={true} setIsOpen={vi.fn()} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('7')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it('calls setIsOpen(false) when clicking an item link', () => {
|
||||
const setIsOpen = vi.fn();
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Sidebar isOpen={true} setIsOpen={setIsOpen} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const dashboardLink = screen.getByRole('link', { name: /نمای کلی داشبورد/i });
|
||||
fireEvent.click(dashboardLink);
|
||||
expect(setIsOpen).toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it('renders in collapsed mode correctly with tooltip triggers', () => {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Sidebar isOpen={false} setIsOpen={vi.fn()} isCollapsed={true} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const aside = screen.getByRole('complementary');
|
||||
expect(aside.className).toContain('lg:w-20');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Topbar component', () => {
|
||||
it('displays user profile info, role, and toggle buttons', () => {
|
||||
useAdminAuthStore.getState().setAuth('test-token', {
|
||||
id: '1',
|
||||
mobile: '09123456789',
|
||||
role: 'SUPER_ADMIN',
|
||||
firstName: 'علی',
|
||||
lastName: 'محمدی',
|
||||
});
|
||||
|
||||
const toggleMenu = vi.fn();
|
||||
const toggleCollapse = vi.fn();
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Topbar toggleMenu={toggleMenu} isCollapsed={false} toggleCollapse={toggleCollapse} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
expect(screen.getByText('علی محمدی')).toBeInTheDocument();
|
||||
expect(screen.getByText('SUPER_ADMIN')).toBeInTheDocument();
|
||||
|
||||
// Collapse toggle button
|
||||
const collapseBtn = screen.getByRole('button', { name: /کوچک کردن سایدبار/i });
|
||||
fireEvent.click(collapseBtn);
|
||||
expect(toggleCollapse).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Mobile menu button
|
||||
const mobileBtn = screen.getByRole('button', { name: /باز کردن منو/i });
|
||||
fireEvent.click(mobileBtn);
|
||||
expect(toggleMenu).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('filters searchable pages with live autocomplete dropdown', async () => {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Topbar toggleMenu={vi.fn()} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('جستجوی سریع در بخشهای پنل...');
|
||||
fireEvent.change(searchInput, { target: { value: 'سفارش' } });
|
||||
|
||||
expect(await screen.findByText('مدیریت سفارشات مشتریان')).toBeInTheDocument();
|
||||
|
||||
// Click the search result item
|
||||
fireEvent.click(screen.getByText('مدیریت سفارشات مشتریان'));
|
||||
expect(screen.queryByText('مدیریت سفارشات مشتریان')).not.toBeInTheDocument();
|
||||
expect(searchInput).toHaveValue('');
|
||||
});
|
||||
|
||||
it('handles user menu dropdown and logout trigger', async () => {
|
||||
useAdminAuthStore.getState().setAuth('test-token', {
|
||||
id: 'admin-1',
|
||||
mobile: '09123456789',
|
||||
role: 'ADMIN',
|
||||
firstName: 'مدیر',
|
||||
lastName: 'سیستم',
|
||||
});
|
||||
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/']}>
|
||||
<Topbar toggleMenu={vi.fn()} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
// Click profile button to open dropdown
|
||||
const profileBtn = screen.getByText('مدیر سیستم').closest('button');
|
||||
expect(profileBtn).not.toBeNull();
|
||||
fireEvent.click(profileBtn!);
|
||||
|
||||
expect(screen.getByText('مرکز مانیتورینگ')).toBeInTheDocument();
|
||||
expect(screen.getByText('خروج از حساب')).toBeInTheDocument();
|
||||
|
||||
// Click logout
|
||||
const logoutBtn = screen.getByText('خروج از حساب');
|
||||
fireEvent.click(logoutBtn);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.post).toHaveBeenCalledWith('/auth/logout');
|
||||
expect(useAdminAuthStore.getState().isAuthenticated).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Layout integration', () => {
|
||||
it('integrates Sidebar, Topbar and Outlet with collapse toggling and persistence', () => {
|
||||
render(
|
||||
<MemoryRouter initialEntries={['/test']}>
|
||||
<Routes>
|
||||
<Route path="/" element={<Layout />}>
|
||||
<Route path="test" element={<div>Nested Content View</div>} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Nested Content View')).toBeInTheDocument();
|
||||
expect(screen.getByRole('complementary')).toBeInTheDocument();
|
||||
|
||||
// Toggle sidebar collapse
|
||||
const collapseBtn = screen.getByRole('button', { name: /کوچک کردن سایدبار/i });
|
||||
fireEvent.click(collapseBtn);
|
||||
expect(localStorage.getItem('canina_admin_sidebar_collapsed')).toBe('true');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user