Files
clinicpro/assets/admin/pages/MyPaymentsPage.test.tsx
T
hamedandClaude Opus 4.8 4c29fa3274 feat(billing): patient payments list + patient invoices detail (doctor/clinic)
Port two nobat724 Figma screens into the admin SPA for the doctor/clinic
tenant panel:

- node 1 — لیست پرداخت‌ها (/admin/my-payments): per-patient payment summary
  (invoice count, paid, remaining, derived status paid/unsettled/unpaid),
  filters by national code / status / Jalali date range, pagination.
- node 2 — پرداخت‌های ثبت‌شده (/admin/my-payments/:patientUuid): a patient's
  recorded invoices with patient header, service title, total, status badge,
  and an expandable per-invoice item breakdown.

Backend (App\Billing):
- InvoiceRepository::patientPaymentSummary/countPatientPaymentSummary — DQL
  aggregation grouped by patient record (arbitrary join Invoice→PatientRecord
  →User), draft/void excluded, derived-status HAVING filters.
- InvoiceRepository::invoicesForPatient/count + InvoiceService methods that
  shape rows and derive status.
- BillingController: GET /api/v1/my/billing/patient-payments and
  GET /api/v1/my/billing/patients/{patientUuid}/invoices (thin, resolveEntity,
  tenant-scoped, 403/404). Invoice::getIssuedAt / InvoiceItem::getTitle added.
- docs/api/billing.md documents both endpoints.

Frontend: useMyPayments hooks, MyPaymentsPage, MyPaymentDetailPage, routes in
App.tsx (doctor/secretary/clinic, blockClinicScope) and a sidebar entry.
Persian strings hardcoded per existing admin convention (no i18n infra).

Tests: tests/Billing/PatientPaymentsTest.php (8), useMyPayments + both page
tests (11). Note: pre-existing LoginPage.test failures are unrelated (proven
by stashing this change).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 18:24:33 +03:30

61 lines
2.9 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen, fireEvent, waitFor } from '@testing-library/react';
import { renderWithProviders } from '../test/utils';
const navigate = vi.fn();
vi.mock('react-router-dom', async (orig) => ({
...(await orig<typeof import('react-router-dom')>()),
useNavigate: () => navigate,
}));
vi.mock('../lib/api', () => ({ api: { get: vi.fn() }, ApiError: class extends Error {} }));
import { api } from '../lib/api';
import MyPaymentsPage from './MyPaymentsPage';
const get = api.get as ReturnType<typeof vi.fn>;
const ROWS = [
{ patient_uuid: 'p1', patient_name: 'دنیا خلیلی', national_code: '1744023654',
invoice_count: 2, paid_rials: 2350000, remaining_rials: 500000, status: 'unsettled' },
{ patient_uuid: 'p2', patient_name: 'علی بدیعی', national_code: '2200112233',
invoice_count: 1, paid_rials: 2000000, remaining_rials: 0, status: 'paid' },
];
beforeEach(() => {
navigate.mockReset();
get.mockReset();
get.mockResolvedValue({ success: true, data: ROWS, meta: { totalRecords: 2, totalPages: 1, currentPage: 1 } });
});
describe('MyPaymentsPage (لیست پرداخت‌ها)', () => {
it('renders patient payment rows with derived status labels', async () => {
renderWithProviders(<MyPaymentsPage />, { route: '/admin/my-payments' });
expect(await screen.findByText('دنیا خلیلی')).toBeInTheDocument();
expect(screen.getByText('علی بدیعی')).toBeInTheDocument();
// status labels appear both as a filter <option> and as a row badge
expect(screen.getAllByText('تسویه نشده').length).toBeGreaterThan(1);
expect(screen.getAllByText('پرداخت شده').length).toBeGreaterThan(1);
expect(screen.getByText('1744023654')).toBeInTheDocument();
});
it('navigates to the patient detail on مشاهده', async () => {
renderWithProviders(<MyPaymentsPage />, { route: '/admin/my-payments' });
await screen.findByText('دنیا خلیلی');
fireEvent.click(screen.getAllByRole('button', { name: /مشاهده/ })[0]);
expect(navigate).toHaveBeenCalledWith('/admin/my-payments/p1');
});
it('shows an empty state when there are no payments', async () => {
get.mockResolvedValue({ success: true, data: [], meta: { totalRecords: 0, totalPages: 0, currentPage: 1 } });
renderWithProviders(<MyPaymentsPage />, { route: '/admin/my-payments' });
expect(await screen.findByText('پرداختی یافت نشد')).toBeInTheDocument();
});
it('sends the national_code filter to the API', async () => {
renderWithProviders(<MyPaymentsPage />, { route: '/admin/my-payments' });
await screen.findByText('دنیا خلیلی');
fireEvent.change(screen.getByPlaceholderText('کد ملی بیمار را وارد کنید...'), { target: { value: '1744' } });
await waitFor(() => expect(get.mock.calls.some(([u]) => String(u).includes('national_code=1744'))).toBe(true));
});
});