- 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.
141 lines
4.7 KiB
TypeScript
141 lines
4.7 KiB
TypeScript
import { render,screen,waitFor } from '@testing-library/react';
|
|
import { MemoryRouter } from 'react-router-dom';
|
|
import { beforeEach,describe,expect,it,vi } from 'vitest';
|
|
import api from '../services/api';
|
|
import Dashboard from './Dashboard';
|
|
|
|
vi.mock('../services/api', () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock recharts to avoid JSDOM SVG / ResizeObserver dimensions issues
|
|
vi.mock('recharts', () => ({
|
|
ResponsiveContainer: ({ children }: any) => <div data-testid="responsive-container">{children}</div>,
|
|
LineChart: ({ children, data }: any) => (
|
|
<div data-testid="line-chart" data-chart-len={data?.length}>
|
|
{children}
|
|
</div>
|
|
),
|
|
Line: () => <div data-testid="line" />,
|
|
XAxis: () => <div data-testid="x-axis" />,
|
|
YAxis: () => <div data-testid="y-axis" />,
|
|
CartesianGrid: () => <div data-testid="cartesian-grid" />,
|
|
Tooltip: () => <div data-testid="tooltip" />,
|
|
PieChart: ({ children }: any) => <div data-testid="pie-chart">{children}</div>,
|
|
Pie: ({ children }: any) => <div data-testid="pie">{children}</div>,
|
|
Cell: () => <div data-testid="cell" />,
|
|
}));
|
|
|
|
describe('Dashboard Page Component', () => {
|
|
const mockStats = {
|
|
revenue: 45000000,
|
|
newOrders: 18,
|
|
users: 124,
|
|
todayVisits: 850,
|
|
categoriesDistribution: [
|
|
{ name: 'مکمل سگ', value: 15 },
|
|
{ name: 'مکمل گربه', value: 10 },
|
|
{ name: 'بهداشتی', value: 7 },
|
|
],
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(api.get).mockResolvedValue({
|
|
data: {
|
|
success: true,
|
|
data: mockStats,
|
|
},
|
|
});
|
|
});
|
|
|
|
const renderDashboard = () => {
|
|
return render(
|
|
<MemoryRouter>
|
|
<Dashboard />
|
|
</MemoryRouter>
|
|
);
|
|
};
|
|
|
|
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();
|
|
|
|
await waitFor(() => {
|
|
expect(api.get).toHaveBeenCalledWith('/admin/dashboard/stats');
|
|
});
|
|
});
|
|
|
|
it('fetches dashboard stats from API and renders calculated values', async () => {
|
|
renderDashboard();
|
|
|
|
await waitFor(() => {
|
|
expect(api.get).toHaveBeenCalledWith('/admin/dashboard/stats');
|
|
});
|
|
|
|
// Verify formatted values in cards
|
|
expect(await screen.findByText(`${(45000000).toLocaleString()} تومان`)).toBeInTheDocument();
|
|
expect(screen.getByText('18')).toBeInTheDocument();
|
|
expect(screen.getByText('124')).toBeInTheDocument();
|
|
expect(screen.getByText('850')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders quick action navigation links with appropriate routes', async () => {
|
|
renderDashboard();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('18')).toBeInTheDocument();
|
|
});
|
|
|
|
const ordersLink = screen.getByText('سفارشات جدید').closest('a');
|
|
expect(ordersLink).toHaveAttribute('href', '/orders');
|
|
|
|
const usersLink = screen.getByText('کاربران فعال').closest('a');
|
|
expect(usersLink).toHaveAttribute('href', '/users');
|
|
|
|
const revenueCard = screen.getByText('درآمد کل').closest('a');
|
|
expect(revenueCard).toHaveAttribute('href', '/reports');
|
|
});
|
|
|
|
it('renders sales chart and category distribution sections', async () => {
|
|
renderDashboard();
|
|
|
|
expect(screen.getByText('روند بازدید و فروش (۷ روز گذشته)')).toBeInTheDocument();
|
|
expect(screen.getByText('دستهبندی محصولات')).toBeInTheDocument();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('مکمل سگ')).toBeInTheDocument();
|
|
expect(screen.getByText('مکمل گربه')).toBeInTheDocument();
|
|
expect(screen.getByText('بهداشتی')).toBeInTheDocument();
|
|
});
|
|
|
|
expect(screen.getByTestId('line-chart')).toBeInTheDocument();
|
|
expect(screen.getByTestId('pie-chart')).toBeInTheDocument();
|
|
});
|
|
|
|
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'));
|
|
|
|
renderDashboard();
|
|
|
|
await waitFor(() => {
|
|
expect(api.get).toHaveBeenCalledWith('/admin/dashboard/stats');
|
|
});
|
|
|
|
// Default zero state should remain rendered
|
|
expect(screen.getByText('0 تومان')).toBeInTheDocument();
|
|
expect(screen.getAllByText('0').length).toBeGreaterThan(0);
|
|
|
|
consoleSpy.mockRestore();
|
|
});
|
|
});
|