Files
clinicpro/assets/admin/lib/patientForm.test.ts
T
hamed b423a0ae4d refactor: normalize date handling to Tehran timezone
- Updated date handling in BlogSeoFields and ScheduleSection to use Tehran timezone utilities for consistency.
- Introduced `toTehranClockTime`, `tehranWallClockToUnix`, and `todayIso` functions for accurate date representation.
- Modified various components to utilize these new utilities, ensuring that date strings are correctly formatted and timestamps are accurately converted.
- Enhanced API documentation to clarify the handling of date fields, emphasizing the importance of server-local midnight.
- Added tests to verify that date overrides and holidays maintain the correct day without shifting due to timezone discrepancies.
2026-07-27 19:37:44 +03:30

83 lines
3.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
tsToDateStr,
dateStrToTs,
profileToFormValues,
formValuesToPayload,
} from '@/lib/patientForm';
import type { PatientProfile } from '@/types';
import type { PatientFormValues } from '@/components/PatientRecordInfoForm';
describe('date helpers', () => {
// مبنا نیمه‌شبِ تهران است (نه UTC)، چون بک‌اند هم تاریخ‌های روزمحور را با
// strtotime در تایم‌زون سرور (Asia/Tehran) ذخیره می‌کند.
it('timestamp ↔ YYYY-MM-DD رفت‌وبرگشت روی نیمه‌شبِ تهران', () => {
const ts = dateStrToTs('2025-01-07');
expect(ts).toBe(Math.floor(Date.UTC(2025, 0, 6, 20, 30, 0) / 1000));
expect(tsToDateStr(ts)).toBe('2025-01-07');
});
it('timestampهای ساخته‌شده توسط سرور یک روز عقب نمی‌روند', () => {
expect(tsToDateStr(Math.floor(Date.UTC(2026, 6, 28, 20, 30, 0) / 1000))).toBe('2026-07-29');
});
it('خالی/نامعتبر → مقدار تهی', () => {
expect(tsToDateStr(null)).toBe('');
expect(tsToDateStr(0)).toBe('');
expect(dateStrToTs('')).toBeNull();
});
});
describe('profileToFormValues', () => {
it('فیلدهای جدید و تاریخ را درست مپ می‌کند', () => {
const p: PatientProfile = {
name: 'ساغر', fathers_name: 'رضا', national_code: '0012345678',
date_of_birth: dateStrToTs('2000-05-10')!, field_of_study: 'نرم‌افزار',
province_id: 8, city_id: 42, postal_code: '8913746351',
referral_source: 'اینستاگرام', description: 'توضیح', mobile: '09131234567',
};
const v = profileToFormValues(p);
expect(v.name).toBe('ساغر');
expect(v.fathers_name).toBe('رضا');
expect(v.birth_date).toBe('2000-05-10');
expect(v.province_id).toBe(8);
expect(v.city_id).toBe(42);
expect(v.referral_source).toBe('اینستاگرام');
});
it('پروفایل تهی → مقادیر پیش‌فرض امن', () => {
const v = profileToFormValues(null);
expect(v.name).toBe('');
expect(v.birth_date).toBe('');
expect(v.province_id).toBeNull();
});
});
describe('formValuesToPayload', () => {
const base: PatientFormValues = {
name: 'ساغر', mobile: '', national_code: '0012345678', gender: 'female',
birth_date: '2000-05-10', fathers_name: 'رضا', marital_status: 'single',
basic_insurance_id: '3', supplementary_insurance_id: '5', field_of_study: 'نرم‌افزار', education: 'کارشناسی',
job: 'مهندس', province_id: 8, city_id: '42', home_phone: '', address: 'یزد',
postal_code: '8913746351', referral_source: 'اینستاگرام', description: '',
};
it('تاریخ به timestamp و idها به عدد تبدیل می‌شوند', () => {
const out = formValuesToPayload(base);
expect(out.date_of_birth).toBe(dateStrToTs('2000-05-10'));
expect(out.basic_insurance_id).toBe(3);
expect(out.supplementary_insurance_id).toBe(5);
expect(out.province_id).toBe(8);
expect(out.city_id).toBe(42);
});
it('موبایل خالی ارسال نمی‌شود؛ موبایل پرشده ارسال می‌شود', () => {
expect('mobile' in formValuesToPayload(base)).toBe(false);
const withMobile = formValuesToPayload({ ...base, mobile: '09131234567' });
expect(withMobile.mobile).toBe('09131234567');
});
it('id خالی → null', () => {
const out = formValuesToPayload({ ...base, basic_insurance_id: null, province_id: '' });
expect(out.basic_insurance_id).toBeNull();
expect(out.province_id).toBeNull();
});
});