Add migration to enhance session_payments table with payment_method_uuid and reference fields for split-payment details
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { screen, fireEvent } from '@testing-library/react';
|
||||
import { screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import { renderWithProviders } from '../../test/utils';
|
||||
|
||||
vi.mock('../../lib/api', () => ({
|
||||
@@ -7,8 +7,12 @@ vi.mock('../../lib/api', () => ({
|
||||
ApiError: class extends Error {},
|
||||
}));
|
||||
|
||||
import { api } from '../../lib/api';
|
||||
import ConfirmAppointmentModal from './ConfirmAppointmentModal';
|
||||
|
||||
const get = api.get as ReturnType<typeof vi.fn>;
|
||||
const post = api.post as ReturnType<typeof vi.fn>;
|
||||
|
||||
const appointment = {
|
||||
uuid: 'a1',
|
||||
version: 1,
|
||||
@@ -23,7 +27,17 @@ function render() {
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// payment-methods (pos / bank-accounts) و بقیهٔ GETها
|
||||
get.mockResolvedValue({ success: true, data: [] });
|
||||
post.mockResolvedValue({ success: true, data: {} });
|
||||
});
|
||||
|
||||
/** همهٔ فیلدهای مبلغ (تومان) در ردیفهای پرداخت. */
|
||||
function amountInputs() {
|
||||
return screen.getAllByPlaceholderText('0') as HTMLInputElement[];
|
||||
}
|
||||
|
||||
describe('ConfirmAppointmentModal', () => {
|
||||
it('بیمار، اقلام هزینه و جمع کل را نشان میدهد', () => {
|
||||
@@ -34,33 +48,70 @@ describe('ConfirmAppointmentModal', () => {
|
||||
expect(screen.getByText('جمع کل')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('مبلغ پرداختی پیشفرض برابر باقیمانده (کل هزینه) است', () => {
|
||||
it('ردیفِ اول پیشفرض برابر کل هزینه است و وضعیت «تسویه کامل» میشود', () => {
|
||||
render();
|
||||
// ۳٬۰۰۰٬۰۰۰ ریال = ۳۰۰٬۰۰۰ تومان
|
||||
expect(screen.getByPlaceholderText('0')).toHaveValue('۳۰۰٬۰۰۰');
|
||||
expect(amountInputs()[0]).toHaveValue('۳۰۰٬۰۰۰');
|
||||
expect(screen.getByText('تسویه کامل')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('با تغییر دستی مبلغ، باقیمانده دوباره محاسبه میشود', () => {
|
||||
it('با تغییر دستی مبلغ به کمتر از کل، وضعیت «پرداخت جزئی» میشود', () => {
|
||||
render();
|
||||
fireEvent.change(screen.getByPlaceholderText('0'), { target: { value: '100000' } });
|
||||
|
||||
fireEvent.change(amountInputs()[0], { target: { value: '100000' } });
|
||||
expect(screen.getByText('پرداخت جزئی')).toBeInTheDocument();
|
||||
// ۳۰۰٬۰۰۰ − ۱۰۰٬۰۰۰ تومان باقیمانده ⇒ ۲٬۰۰۰٬۰۰۰ ریال
|
||||
expect(screen.getByText(/باقیمانده پس از این پرداخت/)).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'بدون پرداخت' }));
|
||||
expect(screen.getByText('بدون پرداخت', { selector: 'span' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('دکمهٔ تأیید فقط با مبلغ بیشتر از جمع کل غیرفعال میشود', () => {
|
||||
it('دکمهٔ تأیید فقط با مجموعِ بیشتر از جمع کل غیرفعال میشود', () => {
|
||||
render();
|
||||
const submit = screen.getByRole('button', { name: 'تأیید و قطعی کردن' });
|
||||
expect(submit).not.toBeDisabled();
|
||||
|
||||
// مبلغ بالاتر از جمع کل (۳٬۰۰۰٬۰۰۰ ریال = ۳۰۰٬۰۰۰ تومان < کل؛ پس عدد بزرگتر میدهیم)
|
||||
fireEvent.change(screen.getByPlaceholderText('0'), { target: { value: '9000000' } });
|
||||
expect(screen.getByText('مبلغ پرداخت از جمع کل بیشتر است.')).toBeInTheDocument();
|
||||
fireEvent.change(amountInputs()[0], { target: { value: '9000000' } });
|
||||
expect(screen.getByText('مجموع پرداختها از جمع کل بیشتر است.')).toBeInTheDocument();
|
||||
expect(submit).toBeDisabled();
|
||||
});
|
||||
|
||||
it('پرداخت جزئی مجاز است و همان یک روش را ثبت میکند', async () => {
|
||||
render();
|
||||
fireEvent.change(amountInputs()[0], { target: { value: '100000' } });
|
||||
fireEvent.click(screen.getByRole('button', { name: 'تأیید و قطعی کردن' }));
|
||||
|
||||
await waitFor(() => expect(post).toHaveBeenCalledWith('/api/v1/appointment/a1/confirm', {
|
||||
version: 1,
|
||||
payments: [{ method: 'cash', amount_rials: 1_000_000 }],
|
||||
}));
|
||||
});
|
||||
|
||||
it('تقسیم پرداخت بین دو روش: مجموع ردیفها بهصورت آرایه ثبت میشود', async () => {
|
||||
render();
|
||||
// ردیف اول را به ۲۰۰٬۰۰۰ تومان کم میکنیم
|
||||
fireEvent.change(amountInputs()[0], { target: { value: '200000' } });
|
||||
// افزودن روش دوم — پیشفرض با باقیماندهٔ ۱۰۰٬۰۰۰ تومان پر میشود
|
||||
fireEvent.click(screen.getByRole('button', { name: /افزودن روش/ }));
|
||||
|
||||
const inputs = amountInputs();
|
||||
expect(inputs).toHaveLength(2);
|
||||
expect(inputs[1]).toHaveValue('۱۰۰٬۰۰۰');
|
||||
|
||||
// مجموع = ۳۰۰٬۰۰۰ تومان = کل ⇒ تسویه کامل
|
||||
expect(screen.getByText('تسویه کامل')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'تأیید و قطعی کردن' }));
|
||||
await waitFor(() => expect(post).toHaveBeenCalledWith('/api/v1/appointment/a1/confirm', {
|
||||
version: 1,
|
||||
payments: [
|
||||
{ method: 'cash', amount_rials: 2_000_000 },
|
||||
{ method: 'cash', amount_rials: 1_000_000 },
|
||||
],
|
||||
}));
|
||||
});
|
||||
|
||||
it('حذف ردیف اضافهشده مجموع را دوباره محاسبه میکند', () => {
|
||||
render();
|
||||
fireEvent.click(screen.getByRole('button', { name: /افزودن روش/ }));
|
||||
expect(amountInputs()).toHaveLength(2);
|
||||
|
||||
fireEvent.click(screen.getAllByLabelText('حذف روش پرداخت')[0]);
|
||||
expect(amountInputs()).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user