feat: add tests for ResourceServicesModal with validation and save functionality
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { renderWithProviders } from '../../test/utils';
|
||||
|
||||
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
|
||||
|
||||
import ResourceServicesModal from './ResourceServicesModal';
|
||||
|
||||
const resource = { uuid: 'r-1', name: 'دستگاه شمارهٔ ۲' } as never;
|
||||
|
||||
const services = [
|
||||
{ uuid: 's-1', name: 'لیزر پا', duration_minutes: 30, price_rials: 8_000_000 },
|
||||
{ uuid: 's-2', name: 'لیزر دست', duration_minutes: 20, price_rials: 5_000_000 },
|
||||
];
|
||||
|
||||
/** ردیفی که مدتش را خودِ منبع گفته ولی قیمتش را از سرویس ارث برده. */
|
||||
const offerings = [
|
||||
{
|
||||
service_uuid: 's-1',
|
||||
service_name: 'لیزر پا',
|
||||
duration_minutes: 15,
|
||||
price_rials: null,
|
||||
active: true,
|
||||
effective_duration_minutes: 15,
|
||||
effective_price_rials: 8_000_000,
|
||||
duration_source: 'resource_option',
|
||||
price_source: 'service_default',
|
||||
},
|
||||
];
|
||||
|
||||
function props(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
resource,
|
||||
offerings: offerings as never,
|
||||
services: services as never,
|
||||
saving: false,
|
||||
onClose: vi.fn(),
|
||||
onSave: vi.fn(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('ResourceServicesModal', () => {
|
||||
it('نشان میدهد مقدار ارثبریشده از کجا آمده', () => {
|
||||
renderWithProviders(<ResourceServicesModal {...props()} />);
|
||||
|
||||
// مدت را خودِ منبع گفته، پس در خانه است.
|
||||
expect(screen.getByDisplayValue('15')).toBeInTheDocument();
|
||||
|
||||
// قیمت تنظیم نشده: خانه خالی است و مقدار مؤثر با برچسبِ منبعش در placeholder.
|
||||
const price = screen.getByPlaceholderText(/پیشفرض سرویس/);
|
||||
expect(price).toHaveValue('');
|
||||
});
|
||||
|
||||
it('override را ذخیره میکند', async () => {
|
||||
const onSave = vi.fn();
|
||||
renderWithProviders(<ResourceServicesModal {...props({ onSave })} />);
|
||||
|
||||
const price = screen.getByPlaceholderText(/پیشفرض سرویس/);
|
||||
await userEvent.type(price, '9500000');
|
||||
await userEvent.click(screen.getByRole('button', { name: 'ذخیره' }));
|
||||
|
||||
expect(onSave).toHaveBeenCalledWith([
|
||||
{ service_uuid: 's-1', duration_minutes: '15', price_rials: '9500000', active: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it('پاککردن مقدار یعنی بازگشت به ارث، نه صفر', async () => {
|
||||
const onSave = vi.fn();
|
||||
renderWithProviders(<ResourceServicesModal {...props({ onSave })} />);
|
||||
|
||||
await userEvent.clear(screen.getByDisplayValue('15'));
|
||||
await userEvent.click(screen.getByRole('button', { name: 'ذخیره' }));
|
||||
|
||||
// رشتهٔ خالی به سرور میرود و سرور آن را `null` میفهمد — نه `0`.
|
||||
expect(onSave).toHaveBeenCalledWith([
|
||||
{ service_uuid: 's-1', duration_minutes: '', price_rials: '', active: true },
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user