90 lines
4.1 KiB
TypeScript
90 lines
4.1 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
||
import { screen, fireEvent } from '@testing-library/react';
|
||
import { renderWithProviders } from '../test/utils';
|
||
import InsuranceModal, {
|
||
buildInsurancePayload, contractToForm, EMPTY_FORM, type Contract, type InsuranceOption,
|
||
} from './InsuranceModal';
|
||
|
||
const mkContract = (over: Partial<Contract> = {}): Contract => ({
|
||
uuid: 'c-1', insurance_id: 3, insurance_name: 'بیمه ایران', insurance_kind: 'basic',
|
||
version: 1, is_active: true, coverage_percent: 70, franchise_rials: 500_000,
|
||
annual_ceiling_rials: 20_000_000, kind: 'basic', effective_from: 1_700_000_000,
|
||
effective_to: null, ...over,
|
||
});
|
||
|
||
const options: InsuranceOption[] = [
|
||
{ insurance_id: 3, insurance_name: 'بیمه ایران', type: 'basic' },
|
||
{ insurance_id: 5, insurance_name: 'بیمه آسیا', type: 'supplementary' },
|
||
];
|
||
|
||
describe('buildInsurancePayload', () => {
|
||
it('converts toman → rials, percent, and Y-m-d → unix', () => {
|
||
const payload = buildInsurancePayload({
|
||
...EMPTY_FORM, insuranceId: '3', kind: 'supplementary',
|
||
coverage: '80', franchise: '50000', ceiling: '2000000',
|
||
effectiveFrom: '2024-01-01', effectiveTo: '2025-01-01',
|
||
});
|
||
expect(payload.insurance_id).toBe(3);
|
||
expect(payload.kind).toBe('supplementary');
|
||
expect(payload.coverage_percent).toBe(80);
|
||
expect(payload.franchise_rials).toBe(500_000); // 50000 toman × 10
|
||
expect(payload.annual_ceiling_rials).toBe(20_000_000);
|
||
expect(typeof payload.effective_from).toBe('number');
|
||
expect(payload.effective_to).toBeGreaterThan(payload.effective_from!);
|
||
});
|
||
|
||
it('empty ceiling → null (بینهایت), empty dates → null', () => {
|
||
const payload = buildInsurancePayload({ ...EMPTY_FORM, insuranceId: '3', coverage: '50' });
|
||
expect(payload.annual_ceiling_rials).toBeNull();
|
||
expect(payload.effective_from).toBeNull();
|
||
expect(payload.effective_to).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe('contractToForm', () => {
|
||
it('maps rials → toman and uses contract kind', () => {
|
||
const form = contractToForm(mkContract({ franchise_rials: 300_000, kind: 'supplementary' }));
|
||
expect(form.franchise).toBe('30000');
|
||
expect(form.kind).toBe('supplementary');
|
||
expect(form.coverage).toBe('70');
|
||
});
|
||
});
|
||
|
||
describe('InsuranceModal', () => {
|
||
it('renders the fields in add mode with no manual kind select', () => {
|
||
renderWithProviders(
|
||
<InsuranceModal open editContract={null} options={options} kind="basic" onClose={() => {}} onSubmit={() => {}} />,
|
||
);
|
||
expect(screen.getByText('افزودن بیمه')).toBeInTheDocument();
|
||
expect(screen.getByText('نام بیمه')).toBeInTheDocument();
|
||
// Manual "نوع بیمه" select is gone; kind is shown as a read-only chip from the tab.
|
||
expect(screen.queryByText('نوع بیمه')).not.toBeInTheDocument();
|
||
expect(screen.getByText('پایه')).toBeInTheDocument();
|
||
expect(screen.getByText('تاریخ شروع قرارداد')).toBeInTheDocument();
|
||
expect(screen.getByText('تاریخ پایان قرارداد')).toBeInTheDocument();
|
||
expect(screen.getByText('درصد پوشش')).toBeInTheDocument();
|
||
expect(screen.getByText('فرانشیز (تومان)')).toBeInTheDocument();
|
||
expect(screen.getByText('سقف تعهد (تومان)')).toBeInTheDocument();
|
||
expect(screen.getByText('ثبت بیمه')).toBeInTheDocument();
|
||
});
|
||
|
||
it('shows the tab kind chip and carries it into a new payload', () => {
|
||
const onSubmit = vi.fn();
|
||
renderWithProviders(
|
||
<InsuranceModal open editContract={null} options={options} kind="supplementary" onClose={() => {}} onSubmit={onSubmit} />,
|
||
);
|
||
expect(screen.getByText('تکمیلی')).toBeInTheDocument();
|
||
});
|
||
|
||
it('submits the built payload for an edited contract', () => {
|
||
const onSubmit = vi.fn();
|
||
renderWithProviders(
|
||
<InsuranceModal open editContract={mkContract()} options={options} kind="basic" onClose={() => {}} onSubmit={onSubmit} />,
|
||
);
|
||
fireEvent.click(screen.getByText('ثبت بیمه'));
|
||
expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({
|
||
insurance_id: 3, coverage_percent: 70, franchise_rials: 500_000, kind: 'basic',
|
||
}));
|
||
});
|
||
});
|