222 lines
7.2 KiB
TypeScript
222 lines
7.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
validateField,
|
|
validateFields,
|
|
validateEmail,
|
|
validatePhone,
|
|
validatePassword,
|
|
validateUsername,
|
|
validatePlateNumber,
|
|
validateYear,
|
|
validateCurrency,
|
|
sanitizeString,
|
|
sanitizeNumber,
|
|
sanitizeInteger,
|
|
sanitizeFormData,
|
|
PATTERNS
|
|
} from '../validation-utils';
|
|
|
|
describe('Validation Utils', () => {
|
|
describe('validateField', () => {
|
|
it('should validate required fields', () => {
|
|
const result1 = validateField('', { required: true });
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toBe('هذا الحقل مطلوب');
|
|
|
|
const result2 = validateField('value', { required: true });
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate email fields', () => {
|
|
const result1 = validateField('invalid-email', { email: true });
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toBe('البريد الإلكتروني غير صحيح');
|
|
|
|
const result2 = validateField('test@example.com', { email: true });
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate phone fields', () => {
|
|
const result1 = validateField('invalid-phone', { phone: true });
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toBe('رقم الهاتف غير صحيح');
|
|
|
|
const result2 = validateField('+966501234567', { phone: true });
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate length constraints', () => {
|
|
const result1 = validateField('ab', { minLength: 3 });
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toBe('يجب أن يكون على الأقل 3 أحرف');
|
|
|
|
const result2 = validateField('a'.repeat(101), { maxLength: 100 });
|
|
expect(result2.isValid).toBe(false);
|
|
expect(result2.error).toBe('يجب أن يكون أقل من 100 حرف');
|
|
});
|
|
|
|
it('should validate numeric constraints', () => {
|
|
const result1 = validateField(5, { min: 10 });
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toBe('يجب أن يكون على الأقل 10');
|
|
|
|
const result2 = validateField(15, { max: 10 });
|
|
expect(result2.isValid).toBe(false);
|
|
expect(result2.error).toBe('يجب أن يكون أقل من 10');
|
|
});
|
|
|
|
it('should validate custom rules', () => {
|
|
const customRule = (value: any) => {
|
|
return value === 'forbidden' ? 'هذه القيمة غير مسموحة' : null;
|
|
};
|
|
|
|
const result1 = validateField('forbidden', { custom: customRule });
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toBe('هذه القيمة غير مسموحة');
|
|
|
|
const result2 = validateField('allowed', { custom: customRule });
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('validateFields', () => {
|
|
it('should validate multiple fields', () => {
|
|
const data = {
|
|
name: '',
|
|
email: 'invalid-email',
|
|
age: 5,
|
|
};
|
|
|
|
const rules = {
|
|
name: { required: true },
|
|
email: { email: true },
|
|
age: { min: 18 },
|
|
};
|
|
|
|
const result = validateFields(data, rules);
|
|
expect(result.isValid).toBe(false);
|
|
expect(result.errors.name).toBe('هذا الحقل مطلوب');
|
|
expect(result.errors.email).toBe('البريد الإلكتروني غير صحيح');
|
|
expect(result.errors.age).toBe('يجب أن يكون على الأقل 18');
|
|
});
|
|
});
|
|
|
|
describe('specific validation functions', () => {
|
|
it('should validate email', () => {
|
|
const result1 = validateEmail('');
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validateEmail('test@example.com');
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate phone', () => {
|
|
const result1 = validatePhone('invalid');
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validatePhone('+966501234567');
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate password', () => {
|
|
const result1 = validatePassword('123');
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validatePassword('password123');
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate username', () => {
|
|
const result1 = validateUsername('ab');
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validateUsername('user123');
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate plate number', () => {
|
|
const result1 = validatePlateNumber('');
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validatePlateNumber('ABC-1234');
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate year', () => {
|
|
const result1 = validateYear(1800);
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validateYear(2023);
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
|
|
it('should validate currency', () => {
|
|
const result1 = validateCurrency(-10);
|
|
expect(result1.isValid).toBe(false);
|
|
|
|
const result2 = validateCurrency(100.50);
|
|
expect(result2.isValid).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('sanitization functions', () => {
|
|
it('should sanitize strings', () => {
|
|
expect(sanitizeString(' hello world ')).toBe('hello world');
|
|
expect(sanitizeString('\t\n test \n\t')).toBe('test');
|
|
});
|
|
|
|
it('should sanitize numbers', () => {
|
|
expect(sanitizeNumber('123.45')).toBe(123.45);
|
|
expect(sanitizeNumber('invalid')).toBe(null);
|
|
expect(sanitizeNumber(456)).toBe(456);
|
|
});
|
|
|
|
it('should sanitize integers', () => {
|
|
expect(sanitizeInteger('123')).toBe(123);
|
|
expect(sanitizeInteger('123.45')).toBe(123);
|
|
expect(sanitizeInteger('invalid')).toBe(null);
|
|
});
|
|
|
|
it('should sanitize form data', () => {
|
|
const data = {
|
|
name: ' John Doe ',
|
|
age: 25,
|
|
description: '\t\n Some text \n\t',
|
|
};
|
|
|
|
const sanitized = sanitizeFormData(data);
|
|
expect(sanitized.name).toBe('John Doe');
|
|
expect(sanitized.age).toBe(25);
|
|
expect(sanitized.description).toBe('Some text');
|
|
});
|
|
});
|
|
|
|
describe('patterns', () => {
|
|
it('should match email pattern', () => {
|
|
expect(PATTERNS.email.test('test@example.com')).toBe(true);
|
|
expect(PATTERNS.email.test('invalid-email')).toBe(false);
|
|
});
|
|
|
|
it('should match phone pattern', () => {
|
|
expect(PATTERNS.phone.test('+966501234567')).toBe(true);
|
|
expect(PATTERNS.phone.test('0501234567')).toBe(true);
|
|
expect(PATTERNS.phone.test('invalid-phone')).toBe(false);
|
|
});
|
|
|
|
it('should match username pattern', () => {
|
|
expect(PATTERNS.username.test('user123')).toBe(true);
|
|
expect(PATTERNS.username.test('user_name')).toBe(true);
|
|
expect(PATTERNS.username.test('user-name')).toBe(false);
|
|
expect(PATTERNS.username.test('user name')).toBe(false);
|
|
});
|
|
|
|
it('should match numeric patterns', () => {
|
|
expect(PATTERNS.numeric.test('12345')).toBe(true);
|
|
expect(PATTERNS.numeric.test('123abc')).toBe(false);
|
|
|
|
expect(PATTERNS.decimal.test('123.45')).toBe(true);
|
|
expect(PATTERNS.decimal.test('123')).toBe(true);
|
|
expect(PATTERNS.decimal.test('123.45.67')).toBe(false);
|
|
});
|
|
});
|
|
}); |