Files
clinicpro/assets/admin/components/WalletTransactionModal.test.tsx
T
hamedandClaude Opus 4.8 ef97b2b249 feat: unify wallet with real payment methods, full transaction transparency, pay-session-from-wallet
Address wallet feedback: use the clinic's real payment infrastructure,
redesign the tab to match the admin panel, and make every wallet movement
fully auditable.

Backend:
- WalletTransaction: add createdBy (acting user) + createdByName, payment_method,
  reference, status; toArray exposes them (migration Version20260716083939).
- WalletService (Settlement): balance/charge/withdraw + settleSessionFromWallet,
  records actor/method/reason; insufficient balance throws ERR_WALLET_INSUFFICIENT.
- PatientController: charge/withdraw delegate to WalletService and accept
  payment_method/reference; PATCH /session/{uuid} with payment_method=wallet
  debits the patient's final share from the wallet (reference=session:{uuid}).
- docs/api/patient.md updated.

Frontend:
- Wallet modal redesigned to panel style (no gradient); payment method now uses
  the clinic's real bank accounts + POS devices (usePaymentMethods) plus cash.
- Wallet tab: panel balance card + DataTable ledger with columns مبلغ/نوع/روش/
  دلیل/ثبت‌کننده/تاریخ/ساعت/وضعیت + همه/واریزی/برداشت filters.
- Session card «تکمیل پرداخت» opens a payment-method chooser incl. کیف پول.

Tests: backend transparency + session-from-wallet (success/insufficient/cash);
frontend modal (real methods, toman→rials) + wallet tab + settle chooser.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 12:22:37 +03:30

63 lines
3.2 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { screen, fireEvent } from '@testing-library/react';
import { renderWithProviders } from '../test/utils';
vi.mock('../lib/api', () => ({
api: { get: vi.fn().mockResolvedValue({ success: true, data: [] }), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
ApiError: class extends Error {},
}));
import WalletTransactionModal from './WalletTransactionModal';
// موجودی نمونه: ۳۰۰٬۰۰۰ ریال = ۳۰٬۰۰۰ تومان
const BALANCE_RIALS = 300_000;
function open(onSubmit = vi.fn()) {
renderWithProviders(
<WalletTransactionModal open balanceRials={BALANCE_RIALS} onClose={vi.fn()} onSubmit={onSubmit} />,
);
return onSubmit;
}
describe('WalletTransactionModal (شارژ/برداشت کیف پول)', () => {
it('renders both mode tabs, the balance banner and the quick amounts', () => {
open();
expect(screen.getByRole('button', { name: 'شارژ کیف پول' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'برداشت از کیف پول' })).toBeInTheDocument();
expect(screen.getByText('موجودی کیف پول')).toBeInTheDocument();
// چهار چیپ مبلغ سریع، هر کدام «... تومان»
expect(screen.getAllByRole('button', { name: /تومان/ }).length).toBe(4);
});
it('submits a charge with the amount converted from toman to rials and the default cash method', () => {
const onSubmit = open();
fireEvent.change(screen.getByPlaceholderText('مبلغ دلخواه (تومان)'), { target: { value: '100000' } });
fireEvent.click(screen.getByRole('button', { name: 'ثبت تراکنش' }));
expect(onSubmit).toHaveBeenCalledWith({ mode: 'charge', amount_rials: 1_000_000, payment_method: 'cash' });
});
it('submits a withdraw (debit) when the برداشت tab is active and amount is within balance', () => {
const onSubmit = open();
fireEvent.click(screen.getByRole('button', { name: 'برداشت از کیف پول' }));
fireEvent.change(screen.getByPlaceholderText('مبلغ دلخواه (تومان)'), { target: { value: '20000' } });
fireEvent.click(screen.getByRole('button', { name: 'ثبت تراکنش' }));
expect(onSubmit).toHaveBeenCalledWith({ mode: 'withdraw', amount_rials: 200_000, payment_method: 'cash' });
});
it('blocks a withdraw above the balance and shows the insufficient-funds hint', () => {
const onSubmit = open();
fireEvent.click(screen.getByRole('button', { name: 'برداشت از کیف پول' }));
// ۴۰٬۰۰۰ تومان = ۴۰۰٬۰۰۰ ریال > موجودی ۳۰۰٬۰۰۰ ریال
fireEvent.change(screen.getByPlaceholderText('مبلغ دلخواه (تومان)'), { target: { value: '40000' } });
expect(screen.getByText('موجودی کیف پول کافی نیست')).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'ثبت تراکنش' })).toBeDisabled();
fireEvent.click(screen.getByRole('button', { name: 'ثبت تراکنش' }));
expect(onSubmit).not.toHaveBeenCalled();
});
it('keeps submit disabled when no amount is entered', () => {
open();
expect(screen.getByRole('button', { name: 'ثبت تراکنش' })).toBeDisabled();
});
});