Port the tauri AddNoteModal notes feature into the admin patient case-file,
replacing the mislabeled «پیامها» (SMS log) tab with «یادداشتها».
Backend (src/Patient):
- PatientNote entity + repository (record-scoped, pinned-first ordering)
- CRUD endpoints on PatientController: GET /notes, POST /note,
PATCH /note/{uuid} (edit body + toggle pin), DELETE /note/{uuid}
- author display name captured server-side from the current user
- migration for patient_notes; docs/api/patient.md updated
Frontend (assets/admin):
- NotesTab: compose box, newest/oldest sort, pinned-first list with
accent rail + pin/edit/delete, edit modal, confirm-delete, empty state
- tab key/label/icon messages -> notes; onAddNote deep-links the notes tab
Tests: PatientNoteTest (create/list-order/edit/pin/delete/validation/ownership),
PatientDetailPage notes cases (render/empty/pin/create).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
311 lines
17 KiB
TypeScript
311 lines
17 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { screen, fireEvent, waitFor } from '@testing-library/react';
|
|
import { Routes, Route } from 'react-router-dom';
|
|
import { renderWithProviders } from '../test/utils';
|
|
|
|
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
|
|
vi.mock('../lib/api', () => ({
|
|
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
|
|
ApiError: class extends Error {},
|
|
}));
|
|
|
|
import { api } from '../lib/api';
|
|
import PatientDetailPage from './PatientDetailPage';
|
|
|
|
const get = api.get as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
get.mockReset();
|
|
get.mockImplementation((url: string) => {
|
|
if (url === '/api/v1/patient/r1') return Promise.resolve({ success: true, data: {
|
|
uuid: 'r1', user_name: 'ساغر صابری', record_number: 'P-1001', created_at: 1700000000,
|
|
user_mobile: '09120000000', user_national_code: '1234567890', tags: [],
|
|
profile: {
|
|
name: 'ساغر صابری', national_code: '1234567890', mobile: '09120000000',
|
|
gender: 'female', fathers_name: 'رضا', job: 'مهندس',
|
|
referral_source: 'اینستاگرام', address: 'یزد', description: 'یادداشت',
|
|
},
|
|
} });
|
|
if (url === '/api/v1/provinces') return Promise.resolve({ success: true, data: [{ id: 10, name: 'یزد' }] });
|
|
if (url.startsWith('/api/v1/cities')) return Promise.resolve({ success: true, data: [{ id: 100, name: 'یزد' }] });
|
|
if (url === '/api/v1/insurance-pricing') return Promise.resolve({ success: true, data: {
|
|
insurances: [
|
|
{ insurance_id: 1, insurance_name: 'تأمین اجتماعی', type: 'basic' },
|
|
{ insurance_id: 5, insurance_name: 'بیمه دانا', type: 'supplementary' },
|
|
],
|
|
} });
|
|
if (url === '/api/v1/patient/r1/sessions') return Promise.resolve({ success: true, data: [
|
|
{ uuid: 's1', services: [{ service_name: 'اسکیلینگ' }], doctor_name: 'دکتر فتحی', final_price_rials: 2500000, is_paid: false, patient_debt_rials: 1500000, notes: 'یادداشت', created_at: 1700000000, visit_price_rials: 0 },
|
|
{ uuid: 's2', services: [{ service_name: 'روکش' }], doctor_name: 'دکتر فتحی', final_price_rials: 2350000, is_paid: true, patient_debt_rials: 0, invoice_uuid: 'iv1', created_at: 1700000000, visit_price_rials: 0 },
|
|
], meta: { totalRecords: 2 } });
|
|
if (url === '/api/v1/billing/invoices/iv1') return Promise.resolve({ success: true, data: { data: {
|
|
uuid: 'iv1', status: 'paid', issued_at: 1700000000, total_rials: 2350000,
|
|
base_insurance_rials: 0, supplementary_rials: 0, patient_rials: 2350000,
|
|
items: [{ uuid: 'it1', title: 'روکش', quantity: 1, total_rials: 2350000, patient_rials: 2350000 }],
|
|
} } });
|
|
if (url === '/api/v1/patient/r1/payments') return Promise.resolve({ success: true, data: [
|
|
{ uuid: 'p1', amount_rials: 250000, status: 'success', gateway: 'mellat', created_at: 1700000000 },
|
|
], meta: { totalRecords: 1 } });
|
|
if (url === '/api/v1/patient/r1/wallet') return Promise.resolve({ success: true, data: {
|
|
balance_rials: 300000,
|
|
recent_transactions: [{ uuid: 't1', amount_rials: 500000, type: 'credit', description: 'شارژ', balance_after: 300000, created_at: 1700000000 }],
|
|
} });
|
|
return Promise.resolve({ success: true, data: [] });
|
|
});
|
|
});
|
|
|
|
function renderDetail() {
|
|
return renderWithProviders(
|
|
<Routes>
|
|
<Route path="/admin/patients/:uuid" element={<PatientDetailPage />} />
|
|
</Routes>,
|
|
{ route: '/admin/patients/r1' },
|
|
);
|
|
}
|
|
|
|
// name now appears in both breadcrumb and banner
|
|
const loaded = async () => (await screen.findAllByText('ساغر صابری'))[0];
|
|
|
|
describe('PatientDetailPage (پرونده تبدار)', () => {
|
|
it('renders the banner and tab bar', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
expect(screen.getByText('شماره پرونده: P-1001')).toBeInTheDocument(); // banner
|
|
expect(screen.getByText('سرویسها')).toBeInTheDocument();
|
|
expect(screen.getByText('پرونده پزشکی')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders full-width (no boxed max-width wrapper, matching tauri FilesServices)', async () => {
|
|
const { container } = renderDetail();
|
|
await loaded();
|
|
const root = container.querySelector('.fade-in') as HTMLElement;
|
|
expect(root).toBeTruthy();
|
|
expect(root.style.maxWidth).toBe('');
|
|
});
|
|
|
|
it('renders session (مراجعه) cards on the default services tab', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
expect(await screen.findByText('اسکیلینگ')).toBeInTheDocument();
|
|
expect(screen.getAllByText('دکتر فتحی').length).toBe(2);
|
|
expect(screen.getByText('تکمیل پرداخت')).toBeInTheDocument(); // unpaid card
|
|
expect(screen.getByText('مشاهده فاکتور')).toBeInTheDocument(); // paid card
|
|
});
|
|
|
|
it('opens the invoice summary on «مشاهده فاکتور»', async () => {
|
|
renderDetail();
|
|
await screen.findByText('مشاهده فاکتور');
|
|
fireEvent.click(screen.getByText('مشاهده فاکتور'));
|
|
expect(await screen.findByText('خلاصه فاکتور')).toBeInTheDocument();
|
|
expect(await screen.findByText('اطلاعات فاکتور')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders the editable «اطلاعات پرونده» form prefilled from the profile', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('اطلاعات پرونده'));
|
|
// لیبل فیلدهای فرم (معادل tauri FileInfoSection)
|
|
expect(await screen.findByText('نام و نام خانوادگی مراجعه کننده')).toBeInTheDocument();
|
|
expect(screen.getByText('کدملی')).toBeInTheDocument();
|
|
expect(screen.getByText('بیمه پایه')).toBeInTheDocument();
|
|
expect(screen.getByText('بیمه تکمیلی')).toBeInTheDocument();
|
|
// مقادیر پیشپرشده از پروفایل
|
|
expect(screen.getByDisplayValue('1234567890')).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue('ساغر صابری')).toBeInTheDocument();
|
|
expect(screen.getByDisplayValue('P-1001')).toBeInTheDocument(); // شماره پرونده readonly
|
|
// دکمهٔ ثبت
|
|
expect(screen.getByRole('button', { name: 'ثبت اطلاعات' })).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders the info form with placeholders when the profile is empty', async () => {
|
|
get.mockImplementation((url: string) => {
|
|
if (url === '/api/v1/patient/r1') return Promise.resolve({ success: true, data: {
|
|
uuid: 'r1', user_name: 'بدون پروفایل', record_number: 'P-9', created_at: 1700000000, profile: null,
|
|
} });
|
|
return Promise.resolve({ success: true, data: [] });
|
|
});
|
|
renderDetail();
|
|
fireEvent.click(await screen.findByText('اطلاعات پرونده'));
|
|
expect(await screen.findByPlaceholderText('نام و نام خانوادگی')).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'ثبت اطلاعات' })).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders the call-center tab with a register form and history', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('کال سنتر'));
|
|
expect(await screen.findByText('ثبت تماس جدید')).toBeInTheDocument();
|
|
expect(screen.getByText('تاریخچه تماسها')).toBeInTheDocument();
|
|
expect(await screen.findByText('تماسی ثبت نشده است.')).toBeInTheDocument();
|
|
// زمان تماس پیشفرض روی «الان» است، نه خالی.
|
|
const timeInput = document.querySelector('input[type="time"]') as HTMLInputElement;
|
|
expect(timeInput.value).toMatch(/^\d{2}:\d{2}$/);
|
|
});
|
|
|
|
it('links "سرویس جدید" on the services tab to the new-session route', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
const link = await screen.findByRole('link', { name: /سرویس جدید/ });
|
|
expect(link).toHaveAttribute('href', '/admin/patients/r1/session/new');
|
|
});
|
|
|
|
it('groups payments by مراجعه (session) accordions on the پرداختها tab', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('پرداختها'));
|
|
// settlement badges from the two seeded sessions (s1 unpaid, s2 paid)
|
|
expect(await screen.findByText('پرداخت شده')).toBeInTheDocument();
|
|
expect(screen.getByText('تسویه نشده')).toBeInTheDocument();
|
|
expect(screen.getByText('روکش')).toBeInTheDocument(); // paid session header
|
|
// first (unpaid) panel is open by default → empty settlement message
|
|
expect(screen.getByText('هیچ پرداختی ثبت نشده است.')).toBeInTheDocument();
|
|
});
|
|
|
|
it('shows wallet balance, charge button and transaction filters on the wallet tab', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('کیف پول'));
|
|
expect(await screen.findByText('موجودی کیف پول')).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: /شارژ کیف پول/ })).toBeInTheDocument();
|
|
// فیلترهای تراکنش
|
|
expect(screen.getByRole('button', { name: 'همه' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'واریزی' })).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: 'برداشت' })).toBeInTheDocument();
|
|
// تراکنش credit اولیه دیده میشود (سطر جدول async لود میشود)
|
|
expect(await screen.findByText('شارژ')).toBeInTheDocument();
|
|
});
|
|
|
|
it('filters out the credit transaction when the برداشت filter is selected', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('کیف پول'));
|
|
await screen.findByText('موجودی کیف پول');
|
|
fireEvent.click(screen.getByRole('button', { name: 'برداشت' }));
|
|
expect(screen.getByText('تراکنشی ثبت نشده است')).toBeInTheDocument();
|
|
});
|
|
|
|
it('opens the charge/withdraw modal and posts a charge', async () => {
|
|
const post = api.post as ReturnType<typeof vi.fn>;
|
|
post.mockResolvedValue({ success: true, data: {} });
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('کیف پول'));
|
|
await screen.findByText('موجودی کیف پول');
|
|
fireEvent.click(screen.getByRole('button', { name: /شارژ کیف پول/ }));
|
|
// مودال باز شد → تب برداشت هم دیده میشود
|
|
expect(await screen.findByRole('button', { name: 'برداشت از کیف پول' })).toBeInTheDocument();
|
|
fireEvent.change(screen.getByPlaceholderText('مبلغ دلخواه (تومان)'), { target: { value: '50000' } });
|
|
fireEvent.click(screen.getByRole('button', { name: 'ثبت تراکنش' }));
|
|
await waitFor(() => expect(post).toHaveBeenCalledWith('/api/v1/patient/r1/wallet/charge', { amount_rials: 500000, payment_method: 'cash' }));
|
|
});
|
|
|
|
it('settles a session from the wallet via the payment-method chooser', async () => {
|
|
const patch = api.patch as ReturnType<typeof vi.fn>;
|
|
patch.mockResolvedValue({ success: true, data: {} });
|
|
renderDetail();
|
|
await loaded();
|
|
// تب سرویسها پیشفرض است؛ کارت پرداختنشده → «تکمیل پرداخت»
|
|
fireEvent.click(await screen.findByText('تکمیل پرداخت'));
|
|
expect(await screen.findByText('روش پرداخت مراجعه')).toBeInTheDocument();
|
|
fireEvent.click(screen.getByRole('button', { name: 'کیف پول بیمار' }));
|
|
await waitFor(() => expect(patch).toHaveBeenCalledWith('/api/v1/session/s1', { payment_method: 'wallet' }));
|
|
});
|
|
|
|
it('renders the notes tab with a compose box and empty state', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('یادداشتها'));
|
|
expect(await screen.findByText('افزودن یادداشت جدید')).toBeInTheDocument();
|
|
expect(screen.getByPlaceholderText('یادداشت خود را بنویسید...')).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: /ذخیره یادداشت/ })).toBeInTheDocument();
|
|
// حالت خالی (API لیست خالی برمیگرداند)
|
|
expect(await screen.findByText('یادداشتی ثبت نشده است.')).toBeInTheDocument();
|
|
});
|
|
|
|
it('renders notes pinned-first with author, date and a pin control', async () => {
|
|
get.mockImplementation((url: string) => {
|
|
if (url === '/api/v1/patient/r1') return Promise.resolve({ success: true, data: {
|
|
uuid: 'r1', user_name: 'ساغر صابری', record_number: 'P-1001', created_at: 1700000000, profile: null,
|
|
} });
|
|
if (url === '/api/v1/patient/r1/notes') return Promise.resolve({ success: true, data: [
|
|
{ uuid: 'n-pin', body: 'یادداشت پینشده', pinned: true, author: 'دکتر احمدی', created_at: 1700000000, updated_at: null },
|
|
{ uuid: 'n-2', body: 'یادداشت عادی', pinned: false, author: 'منشی رضایی', created_at: 1700009000, updated_at: 1700010000 },
|
|
] });
|
|
return Promise.resolve({ success: true, data: [] });
|
|
});
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('یادداشتها'));
|
|
expect(await screen.findByText('یادداشت پینشده')).toBeInTheDocument();
|
|
expect(screen.getByText('یادداشت عادی')).toBeInTheDocument();
|
|
expect(screen.getByText('دکتر احمدی')).toBeInTheDocument();
|
|
expect(screen.getByText('منشی رضایی')).toBeInTheDocument();
|
|
expect(screen.getByText('(ویرایششده)')).toBeInTheDocument(); // یادداشت دومی updated_at دارد
|
|
expect(screen.getByText('یادداشتهای قبلی (۲)')).toBeInTheDocument();
|
|
// pinned note is rendered before the normal one
|
|
const bodies = screen.getAllByText(/یادداشت (پینشده|عادی)/).map((el) => el.textContent);
|
|
expect(bodies[0]).toBe('یادداشت پینشده');
|
|
});
|
|
|
|
it('posts a new note from the compose box', async () => {
|
|
const post = api.post as ReturnType<typeof vi.fn>;
|
|
post.mockResolvedValue({ success: true, data: {} });
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('یادداشتها'));
|
|
const box = await screen.findByPlaceholderText('یادداشت خود را بنویسید...');
|
|
fireEvent.change(box, { target: { value: 'حساسیت دارویی' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /ذخیره یادداشت/ }));
|
|
await waitFor(() => expect(post).toHaveBeenCalledWith('/api/v1/patient/r1/note', { body: 'حساسیت دارویی' }));
|
|
});
|
|
|
|
it('toggles pin on a note via PATCH', async () => {
|
|
const patch = api.patch as ReturnType<typeof vi.fn>;
|
|
patch.mockResolvedValue({ success: true, data: {} });
|
|
get.mockImplementation((url: string) => {
|
|
if (url === '/api/v1/patient/r1') return Promise.resolve({ success: true, data: {
|
|
uuid: 'r1', user_name: 'ساغر صابری', record_number: 'P-1001', created_at: 1700000000, profile: null,
|
|
} });
|
|
if (url === '/api/v1/patient/r1/notes') return Promise.resolve({ success: true, data: [
|
|
{ uuid: 'n-2', body: 'یادداشت عادی', pinned: false, author: 'منشی رضایی', created_at: 1700009000, updated_at: null },
|
|
] });
|
|
return Promise.resolve({ success: true, data: [] });
|
|
});
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('یادداشتها'));
|
|
fireEvent.click(await screen.findByRole('button', { name: 'پین کردن' }));
|
|
await waitFor(() => expect(patch).toHaveBeenCalledWith('/api/v1/patient/note/n-2', { pinned: true }));
|
|
});
|
|
|
|
it('renders appointment turn cards + toolbar on the نوبتها tab', async () => {
|
|
get.mockImplementation((url: string) => {
|
|
if (url === '/api/v1/patient/r1') return Promise.resolve({ success: true, data: {
|
|
uuid: 'r1', user_name: 'ساغر صابری', record_number: 'P-1001', created_at: 1700000000, profile: null,
|
|
} });
|
|
if (url === '/api/v1/patient/r1/appointments') return Promise.resolve({ success: true, data: [
|
|
{ uuid: 'a1', starts_at: 1754000000, ends_at: 1754001800, status: 'confirmed', version: 1, doctor_name: 'دکتر راد', service_name: null },
|
|
] });
|
|
return Promise.resolve({ success: true, data: [] });
|
|
});
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('نوبتها'));
|
|
// card: generic title + doctor + live status label (confirmed → قطعی شده)
|
|
expect(await screen.findByText('دکتر راد')).toBeInTheDocument();
|
|
expect(screen.getByText('قطعی شده')).toBeInTheDocument();
|
|
expect(screen.getByText('تاریخ:')).toBeInTheDocument();
|
|
expect(screen.getByText('ساعت:')).toBeInTheDocument();
|
|
// toolbar buttons link to the existing appointment pages
|
|
expect(screen.getByRole('link', { name: /نوبت رزرو/ })).toHaveAttribute('href', '/admin/appointments/reserve');
|
|
expect(screen.getByRole('link', { name: /نوبت جدید/ })).toHaveAttribute('href', '/admin/appointments/new');
|
|
});
|
|
|
|
it('shows the empty state on the نوبتها tab when there are no appointments', async () => {
|
|
renderDetail();
|
|
await loaded();
|
|
fireEvent.click(screen.getByText('نوبتها'));
|
|
expect(await screen.findByText('نوبتی ثبت نشده است')).toBeInTheDocument();
|
|
});
|
|
});
|