car_mms/app/lib/__tests__/customer-validation.test.ts
2025-09-11 14:22:27 +03:00

109 lines
3.4 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { validateCustomer } from '../validation';
describe('Customer Validation', () => {
describe('validateCustomer', () => {
it('should validate required name field', () => {
const result = validateCustomer({ name: '' });
expect(result.isValid).toBe(false);
expect(result.errors.name).toBe('اسم العميل مطلوب');
});
it('should validate name length', () => {
const longName = 'أ'.repeat(101);
const result = validateCustomer({ name: longName });
expect(result.isValid).toBe(false);
expect(result.errors.name).toBe('الاسم يجب أن يكون أقل من 100 حرف');
});
it('should validate phone format', () => {
const result = validateCustomer({
name: 'أحمد محمد',
phone: 'invalid-phone'
});
expect(result.isValid).toBe(false);
expect(result.errors.phone).toBe('رقم الهاتف غير صحيح');
});
it('should accept valid phone formats', () => {
const validPhones = [
'0501234567',
'+966501234567',
'050 123 4567',
'050-123-4567',
'(050) 123-4567',
];
validPhones.forEach(phone => {
const result = validateCustomer({
name: 'أحمد محمد',
phone
});
expect(result.isValid).toBe(true);
expect(result.errors.phone).toBeUndefined();
});
});
it('should validate email format', () => {
const result = validateCustomer({
name: 'أحمد محمد',
email: 'invalid-email'
});
expect(result.isValid).toBe(false);
expect(result.errors.email).toBe('البريد الإلكتروني غير صحيح');
});
it('should accept valid email format', () => {
const result = validateCustomer({
name: 'أحمد محمد',
email: 'ahmed@example.com'
});
expect(result.isValid).toBe(true);
expect(result.errors.email).toBeUndefined();
});
it('should allow empty optional fields', () => {
const result = validateCustomer({
name: 'أحمد محمد',
phone: '',
email: '',
address: ''
});
expect(result.isValid).toBe(true);
expect(Object.keys(result.errors)).toHaveLength(0);
});
it('should validate complete customer data', () => {
const result = validateCustomer({
name: 'أحمد محمد',
phone: '0501234567',
email: 'ahmed@example.com',
address: 'الرياض، المملكة العربية السعودية'
});
expect(result.isValid).toBe(true);
expect(Object.keys(result.errors)).toHaveLength(0);
});
it('should handle undefined fields gracefully', () => {
const result = validateCustomer({
name: 'أحمد محمد',
phone: undefined,
email: undefined,
address: undefined
});
expect(result.isValid).toBe(true);
expect(Object.keys(result.errors)).toHaveLength(0);
});
it('should trim whitespace from name', () => {
const result = validateCustomer({ name: ' أحمد محمد ' });
expect(result.isValid).toBe(true);
});
it('should reject name with only whitespace', () => {
const result = validateCustomer({ name: ' ' });
expect(result.isValid).toBe(false);
expect(result.errors.name).toBe('اسم العميل مطلوب');
});
});
});