Files
clinicpro/assets/admin/components/schedule/ScheduleSection.test.tsx
T
hamedandClaude Opus 5 a3fc7f5c8b feat(appointment): let a platform admin change a doctor's locked booking mode
The booking mode locks after the first save because existing appointments were
computed under that mode's rules. A lock with no key, though, traps a practice
that picked the wrong mode on day one, so ROLE_ADMIN can now open it.

The first attempt is still refused when the doctor has active appointments in
the next year, and says how many; the admin repeats the request with
force_mode_change to confirm they know what happens to those. The flag does
nothing for anyone else. GET now returns booking_mode_changeable so the panel
enables the toggle from the server's answer rather than guessing from the role.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-20 17:32:52 +03:30

99 lines
4.9 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { screen } 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 { status = 500; },
}));
import { api } from '../../lib/api';
import { WeeklyScheduleTab } from './ScheduleSection';
import type { AddressData } from './ScheduleSection';
const get = api.get as ReturnType<typeof vi.fn>;
/** بدون حداقل یک مکان، تب اصلاً فرم را رندر نمی‌کند. */
const ADDRESSES: AddressData[] = [{
id: '1', uuid: 'addr-1', type: 'personal', clinic_id: null, clinic_name: null,
name: 'مطب', address: 'تهران', telephone: null,
map: { latitude: null, longitude: null }, city: null, province: null,
}];
/** پاسخ برنامهٔ هفتگی؛ فقط `meta` بین تست‌ها فرق می‌کند. */
function mockSchedule(meta: Record<string, unknown>, locked = false, changeable = false) {
get.mockImplementation((url?: string) => {
if (typeof url === 'string' && url.includes('weekly-schedule')) {
return Promise.resolve({
success: true,
data: {
uuid: 'sch-1',
doctor_uuid: 'd1',
schedule: {},
meta: { online_booking_enabled: true, booking_window_value: 3, booking_window_unit: 'month', buffer_minutes: 0, ...meta },
booking_mode_locked: locked,
booking_mode_changeable: changeable,
},
});
}
return Promise.resolve({ success: true, data: [] });
});
}
beforeEach(() => get.mockReset());
describe('WeeklyScheduleTab — روش نوبت‌دهی', () => {
/**
* نوبت‌دهی منبع‌محور از تنظیمات پزشک برداشته شده: انتخابش برگشت‌ناپذیر بود و
* منابع مسیر نوبت‌دهی مستقل خودشان را دارند.
*/
it('گزینهٔ «نوبت‌دهی منبع‌محور» دیگر قابل انتخاب نیست', async () => {
mockSchedule({ booking_mode: 'slot' });
renderWithProviders(<WeeklyScheduleTab doctorUuid="d1" addresses={ADDRESSES} />);
expect(await screen.findByText('نوبت‌دهی اسلاتی')).toBeInTheDocument();
expect(screen.getByText('نوبت‌دهی سرویسی')).toBeInTheDocument();
expect(screen.queryByText('نوبت‌دهی منبع‌محور')).toBeNull();
// فیلدهای مخصوص همان حالت هم رفته‌اند.
expect(screen.queryByText('گام جستجوی وقت')).toBeNull();
expect(screen.queryByText('ترتیب انتخاب منابع')).toBeNull();
});
it('استراتژی‌ها و شرط‌های آمادگیِ منبع‌محور دیگر درخواست نمی‌شوند', async () => {
mockSchedule({ booking_mode: 'slot' });
renderWithProviders(<WeeklyScheduleTab doctorUuid="d1" addresses={ADDRESSES} />);
await screen.findByText('نوبت‌دهی اسلاتی');
const urls = get.mock.calls.map((c: unknown[]) => c[0]).filter((u): u is string => typeof u === 'string');
expect(urls.some(u => u.includes('resource-strategies'))).toBe(false);
expect(urls.some(u => u.includes('/api/v1/service-items'))).toBe(false);
});
/** رکورد قدیمیِ منبع‌محور باید روشِ ثبت‌شده‌اش را بگوید، نه اینکه بی‌حالت دیده شود. */
it('رکورد قدیمیِ منبع‌محور فقط به‌صورت خواندنی اعلام می‌شود', async () => {
mockSchedule({ booking_mode: 'resource' }, true);
renderWithProviders(<WeeklyScheduleTab doctorUuid="d1" addresses={ADDRESSES} />);
expect(await screen.findByText(/روش ثبت‌شدهٔ این پزشک/)).toBeInTheDocument();
expect(screen.queryByRole('button', { name: /نوبت‌دهی منبع‌محور/ })).toBeNull();
});
});
describe('WeeklyScheduleTab — قفل نوع نوبت‌دهی', () => {
it('برای کاربر عادی، نوعِ ثبت‌شده قفل است', async () => {
mockSchedule({ booking_mode: 'slot' }, true);
renderWithProviders(<WeeklyScheduleTab doctorUuid="d1" addresses={ADDRESSES} />);
expect(await screen.findByText('نوع نوبت‌دهی ثبت شده و دیگر قابل تغییر نیست.')).toBeInTheDocument();
expect(screen.getByText('نوبت‌دهی سرویسی').closest('button')).toBeDisabled();
});
it('برای ادمین، همان نوع قابل تغییر است و هشدارش را می‌گوید', async () => {
mockSchedule({ booking_mode: 'slot' }, true, true);
renderWithProviders(<WeeklyScheduleTab doctorUuid="d1" addresses={ADDRESSES} />);
expect(await screen.findByText(/فقط ادمین می‌تواند عوضش کند/)).toBeInTheDocument();
expect(screen.getByText('نوبت‌دهی سرویسی').closest('button')).not.toBeDisabled();
});
});