Files
clinicpro/assets/admin/pages/DashboardPage.test.tsx
T
hamed 62a9fd87c3 feat: Implement Jalali calendar support for dashboard charts
- Added support for Jalali calendar in the dashboard, allowing charts to display data based on the current Jalali month and year.
- Updated the API to return `patients_year`, `patients_month`, and `revenue_year` parameters for the dashboard charts.
- Refactored the dashboard controller to handle Jalali date calculations and queries.
- Modified the frontend components to utilize the new Jalali date parameters and reflect changes in the UI.
- Removed the status column from the NewAppointmentsTable as status management is now handled on the appointments page.
- Added tests to ensure the correct functioning of the new Jalali chart period features.
2026-07-18 21:44:01 +03:30

128 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen } from '@testing-library/react';
import { renderWithProviders } from '../test/utils';
vi.mock('../lib/api', () => ({
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
ApiError: class extends Error {},
}));
import { api } from '../lib/api';
import { useAuthStore } from '../stores/authStore';
import DashboardPage from './DashboardPage';
import { JALALI_MONTHS } from '../components/dashboard/TauriDashboardView';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const jalaali = require('jalaali-js') as { toJalaali: (d: Date) => { jy: number; jm: number } };
const get = api.get as ReturnType<typeof vi.fn>;
const clinicPayload = {
success: true,
data: {
clinic: { uuid: 'clinic-1', name: 'کلینیک نمونه', is_active: true, logo: null },
stats: {
total_doctors: 2,
today_appointments: 15,
this_month_appointments: 40,
pending_invitations: 0,
total_patients: 151,
revenue_period_rials: 5_600_002_000,
today_payments_rials: 5_225_000,
week_payments_rials: 12_000_000,
},
charts: {
revenue_by_day: [
{ label: '۷ خرداد', amount_rials: 60_000 },
{ label: '۸ خرداد', amount_rials: 160_000 },
{ label: '۹ خرداد', amount_rials: 90_000 },
],
revenue_by_month: [
{ label: 'فروردین', amount_rials: 60_000 },
{ label: 'اردیبهشت', amount_rials: 160_000 },
{ label: 'خرداد', amount_rials: 90_000 },
],
appointments_by_day: [
{ label: '۱', count: 15 },
{ label: '۲', count: 40 },
{ label: '۳', count: 48 },
],
},
charts_period: { patients_year: 1405, patients_month: 4, revenue_year: 1405 },
today_appointments: [
{
uuid: 'appt-1',
patient_name: 'دنیا خلیلی',
patient_mobile: '09136549874',
doctor_name: 'حمیدی',
service_name: 'ویزیت عمومی',
slot_start: 1_718_000_000,
slot_end: 1_718_001_800,
status: 'visited',
},
],
doctors: [],
period: { from: 0, to: 0 },
},
};
describe('DashboardPage (ported clinic dashboard)', () => {
beforeEach(() => {
get.mockReset();
useAuthStore.setState({ primaryRole: 'clinic', dbUuid: 'clinic-1', context: null } as never);
get.mockImplementation((url: string) => {
if (url.startsWith('/api/v1/dashboard/clinic')) return Promise.resolve(clinicPayload);
return Promise.resolve({ success: true, data: [] });
});
});
it('renders the ported stat cards, charts and new-appointments list', async () => {
renderWithProviders(<DashboardPage />, { route: '/admin/dashboard' });
// stat card labels (ported 1:1)
expect(await screen.findByText('تعداد کل مراجعین')).toBeInTheDocument();
expect(screen.getByText('کل پرداختی‌ها')).toBeInTheDocument();
expect(screen.getByText('پرداختی‌های امروز')).toBeInTheDocument();
expect(screen.getByText('تعداد نوبت‌های امروز')).toBeInTheDocument();
// chart titles
expect(screen.getByText('نمودار تعداد بیماران')).toBeInTheDocument();
expect(screen.getByText('میزان درآمد')).toBeInTheDocument();
// new-appointments list + row data (mobile/service columns come from the extended API)
expect(screen.getByText('لیست نوبت‌های جدید')).toBeInTheDocument();
expect(screen.getByText('دنیا خلیلی')).toBeInTheDocument();
expect(screen.getByText('09136549874')).toBeInTheDocument();
expect(screen.getByText('ویزیت عمومی')).toBeInTheDocument();
// ستون «وضعیت» از داشبورد حذف شده — تغییر وضعیت فقط در صفحه نوبت‌هاست
expect(screen.queryByText('وضعیت')).not.toBeInTheDocument();
expect(screen.queryByText('ویزیت شده')).not.toBeInTheDocument();
});
it('calls the real clinic dashboard endpoint', async () => {
renderWithProviders(<DashboardPage />, { route: '/admin/dashboard' });
await screen.findByText('تعداد کل مراجعین');
expect(get).toHaveBeenCalledWith(expect.stringContaining('/api/v1/dashboard/clinic'));
});
it('نمودارها را روی ماه و سال شمسی جاری درخواست می‌کند', async () => {
renderWithProviders(<DashboardPage />, { route: '/admin/dashboard' });
await screen.findByText('تعداد کل مراجعین');
const url = get.mock.calls.map(c => String(c[0])).find(u => u.startsWith('/api/v1/dashboard/clinic'))!;
const params = new URLSearchParams(url.split('?')[1]);
const now = jalaali.toJalaali(new Date());
expect(params.get('patients_year')).toBe(String(now.jy));
expect(params.get('patients_month')).toBe(String(now.jm));
expect(params.get('revenue_year')).toBe(String(now.jy));
// سلکتورها همان دوره را نشان می‌دهند
expect(screen.getByText(JALALI_MONTHS[now.jm - 1])).toBeInTheDocument();
expect(
screen.getByText(`سال ${new Intl.NumberFormat('fa-IR', { useGrouping: false }).format(now.jy)}`)
).toBeInTheDocument();
});
});