import { describe, it, expect, beforeEach, vi } from 'vitest'; import { screen, fireEvent, waitFor } from '@testing-library/react'; import { renderWithProviders } from '../test/utils'; vi.mock('../lib/api', () => ({ api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() }, ApiError: class extends Error {}, })); vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } })); import { api } from '../lib/api'; import CancellationPolicyPage from './CancellationPolicyPage'; const get = api.get as ReturnType; const put = api.put as ReturnType; const policy = { uuid: 'cp1', service_uuid: null, service_name: null, free_window_hours: 24, penalty_mode: 'percent' as const, penalty_value: 50, deposit_refundable: false, credit_refundable: true, no_show_threshold: 3, risk_tag_uuid: null, active: true, created_at: 1_700_000_000, }; describe('CancellationPolicyPage', () => { beforeEach(() => { vi.clearAllMocks(); get.mockResolvedValue({ success: true, data: { default: policy, overrides: [] } }); put.mockResolvedValue({ success: true, data: policy }); }); it('loads the saved policy into the form', async () => { renderWithProviders(, { route: '/admin/cancellation-policy' }); // فیلد درصد فقط وقتی حالت «درصدی» است رندر می‌شود، پس انتظارش هم باید صبر کند. await waitFor(() => expect(screen.getByLabelText('درصد جریمه')).toHaveValue(50)); expect(screen.getByLabelText('پنجرهٔ لغو رایگان (ساعت)')).toHaveValue(24); expect(screen.getByLabelText('آستانهٔ عدم حضور')).toHaveValue(3); }); /** درصد بیرون بازه نباید تا سرور برود و ۴۲۲ بگیرد؛ فرم همان‌جا می‌گوید. */ it('blocks saving a percentage above one hundred', async () => { renderWithProviders(, { route: '/admin/cancellation-policy' }); await waitFor(() => expect(screen.getByLabelText('درصد جریمه')).toBeInTheDocument()); fireEvent.change(screen.getByLabelText('درصد جریمه'), { target: { value: '150' } }); expect(screen.getByText('درصد باید بین ۰ تا ۱۰۰ باشد.')).toBeInTheDocument(); expect(screen.getByText('ذخیرهٔ سیاست')).toBeDisabled(); expect(put).not.toHaveBeenCalled(); }); });