165 lines
7.2 KiB
TypeScript
165 lines
7.2 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
EMAIL_REGEX,
|
|
SPECIAL_CHAR_REGEX,
|
|
cleanPhoneInput,
|
|
formatE164Phone,
|
|
formatPrefixList,
|
|
isValidEmail,
|
|
isValidPhoneNumber,
|
|
parsePhoneNumber,
|
|
updatePhoneDialCode,
|
|
validateEmail,
|
|
validatePassword,
|
|
validatePhoneNumber,
|
|
} from '../../../src/utils/validation';
|
|
|
|
describe('validation utils', () => {
|
|
describe('formatPrefixList', () => {
|
|
it('handles empty, single, two, and multiple prefixes', () => {
|
|
expect(formatPrefixList([])).toBe('');
|
|
expect(formatPrefixList(['3'])).toBe('3');
|
|
expect(formatPrefixList(['6', '7'])).toBe('6 or 7');
|
|
expect(formatPrefixList(['6', '7', '8', '9'])).toBe('6, 7, 8, or 9');
|
|
});
|
|
});
|
|
|
|
describe('cleanPhoneInput', () => {
|
|
it('strips non-numeric chars, respects allowPlus and maxLength', () => {
|
|
expect(cleanPhoneInput(' +91 987-654-3210 ', 12, true)).toBe('+919876543210');
|
|
expect(cleanPhoneInput('+91 9876543210', 10, false)).toBe('9198765432');
|
|
expect(cleanPhoneInput('abc12345def', 0, true)).toBe('12345');
|
|
});
|
|
});
|
|
|
|
describe('validatePhoneNumber & isValidPhoneNumber', () => {
|
|
it('returns null for empty/whitespace values', () => {
|
|
expect(validatePhoneNumber('')).toBeNull();
|
|
expect(validatePhoneNumber(' ')).toBeNull();
|
|
expect(validatePhoneNumber(null)).toBeNull();
|
|
expect(isValidPhoneNumber(undefined)).toBe(true);
|
|
});
|
|
|
|
it('rejects alphabetic characters, hyphens, and non-digit symbols', () => {
|
|
expect(validatePhoneNumber('98765abc10', '+91', 'India')).toContain('alphabetic');
|
|
expect(validatePhoneNumber('789--98--809', '+91', 'India')).toContain('hyphens');
|
|
expect(validatePhoneNumber('98765@3210', '+91', 'India')).toContain('only numeric digits');
|
|
expect(validatePhoneNumber('+ ', '+91', 'India')).toContain('numeric digits');
|
|
});
|
|
|
|
it('enforces exact and variable length rules', () => {
|
|
expect(validatePhoneNumber('98765', '+91', 'India')).toContain('exactly 10 digits');
|
|
expect(validatePhoneNumber('12345', '+49', 'Germany')).toContain('too short');
|
|
expect(validatePhoneNumber('1512345678901234', '+49', 'Germany')).toContain('cannot exceed');
|
|
});
|
|
|
|
it('enforces valid mobile prefixes and rejects landlines', () => {
|
|
expect(validatePhoneNumber('5123456789', '+91', 'India')).toContain('must start with');
|
|
// Valid Indian mobile
|
|
expect(validatePhoneNumber('9876543210', '+91', 'India')).toBeNull();
|
|
expect(isValidPhoneNumber('+919876543210', '+91', 'India')).toBe(true);
|
|
// Trunk 0 stripping for UK
|
|
expect(validatePhoneNumber('07123456789', '+44', 'United Kingdom')).toBeNull();
|
|
// Dial code prefix without '+' when longer than maxLength
|
|
expect(validatePhoneNumber('919876543210', '+91', 'India')).toBeNull();
|
|
// Fallback when country is not provided -> resolves by dialCode
|
|
expect(validatePhoneNumber('9876543210', '+91')).toBeNull();
|
|
// Fallback when neither country nor dialCode provided -> DEFAULT_COUNTRY
|
|
expect(validatePhoneNumber('9876543210')).toBeNull();
|
|
// Fixed line rejection (e.g. US/UK or invalid mobile)
|
|
expect(validatePhoneNumber('15123456789', '+49', 'Germany')).toBeNull();
|
|
expect(validatePhoneNumber('2025550173', '+1', 'United States')).toBeNull(); // US numbers are FIXED_LINE_OR_MOBILE
|
|
// Invalid UK landline 020...
|
|
const ukLandline = validatePhoneNumber('7123456789', '+44', 'United Kingdom');
|
|
expect(ukLandline).toBeNull();
|
|
// Invalid number with valid prefix but rejected by libphonenumber-js
|
|
expect(validatePhoneNumber('0000000000', '+91', 'India')).toContain('must start with');
|
|
expect(validatePhoneNumber('07700900123', '+44', 'United Kingdom')).toContain('Please enter a valid mobile number');
|
|
});
|
|
});
|
|
|
|
describe('parsePhoneNumber', () => {
|
|
it('parses empty, valid E.164, and fallback numbers', () => {
|
|
expect(parsePhoneNumber('', 'India')).toEqual({
|
|
country: 'India',
|
|
dialCode: '+91',
|
|
localDigits: '',
|
|
iso2: 'IN',
|
|
});
|
|
|
|
const parsedIn = parsePhoneNumber('+919876543210', 'India');
|
|
expect(parsedIn).toEqual({
|
|
country: 'India',
|
|
dialCode: '+91',
|
|
localDigits: '9876543210',
|
|
iso2: 'IN',
|
|
});
|
|
|
|
const parsedDe = parsePhoneNumber('+491512345678', 'India');
|
|
expect(parsedDe.country).toBe('Germany');
|
|
expect(parsedDe.dialCode).toBe('+49');
|
|
|
|
// Fallback when unparseable by libphonenumber-js
|
|
const fallbackSame = parsePhoneNumber('+91 123', 'India');
|
|
expect(fallbackSame.dialCode).toBe('+91');
|
|
|
|
const fallbackOther = parsePhoneNumber('+49', 'India');
|
|
expect(fallbackOther.dialCode).toBe('+49');
|
|
expect(fallbackOther.country).toBe('Germany');
|
|
|
|
const rawOnly = parsePhoneNumber('9876', 'India');
|
|
expect(rawOnly.localDigits).toBe('9876');
|
|
|
|
const unknownCode = parsePhoneNumber('+999123', 'India');
|
|
expect(unknownCode.localDigits).toBe('999123');
|
|
});
|
|
});
|
|
|
|
describe('formatE164Phone', () => {
|
|
it('formats valid and fallback E.164 strings', () => {
|
|
expect(formatE164Phone('+91', '')).toBe('');
|
|
expect(formatE164Phone('+91', 'abc')).toBe('');
|
|
expect(formatE164Phone('+91', '9876543210', 'India')).toBe('+919876543210');
|
|
expect(formatE164Phone('44', '07123456789', 'United Kingdom')).toBe('+447123456789');
|
|
// Fallback trunk 0 removal when invalid
|
|
expect(formatE164Phone('+91', '0123', 'India')).toBe('+91123');
|
|
// Italy keeps trunk 0 in fallback
|
|
expect(formatE164Phone('+39', '0123', 'Italy')).toBe('+390123');
|
|
});
|
|
});
|
|
|
|
describe('updatePhoneDialCode', () => {
|
|
it('updates dial code and clamps to new country maxLength', () => {
|
|
expect(updatePhoneDialCode('', 'India', 'Germany')).toBe('');
|
|
expect(updatePhoneDialCode('+91 9876543210', 'India', 'Singapore')).toBe('+65 98765432');
|
|
expect(updatePhoneDialCode('+91', 'India', 'Germany')).toBe('+49 ');
|
|
});
|
|
});
|
|
|
|
describe('validatePassword', () => {
|
|
it('enforces all password complexity rules', () => {
|
|
expect(SPECIAL_CHAR_REGEX.test('!')).toBe(true);
|
|
expect(validatePassword('')).toBe('Password is required.');
|
|
expect(validatePassword('Ab1!')).toContain('at least 8 characters');
|
|
expect(validatePassword('Abcd 1234!')).toContain('must not contain spaces');
|
|
expect(validatePassword('abcd1234!')).toContain('uppercase');
|
|
expect(validatePassword('Abcdefgh!')).toContain('number');
|
|
expect(validatePassword('Abcd1234😀!')).toContain('emojis');
|
|
expect(validatePassword('Abcd12345')).toContain('special character');
|
|
expect(validatePassword('ValidPass1!')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('validateEmail & isValidEmail', () => {
|
|
it('validates RFC-compliant emails', () => {
|
|
expect(EMAIL_REGEX.test('user@example.com')).toBe(true);
|
|
expect(validateEmail('')).toBe('Email address is required.');
|
|
expect(validateEmail(' ')).toBe('Email address is required.');
|
|
expect(validateEmail('invalid-email')).toBe('Please enter a valid email address.');
|
|
expect(validateEmail('user@example.com')).toBeNull();
|
|
expect(isValidEmail('user.name+1@sub.example.co.in')).toBe(true);
|
|
expect(isValidEmail('bad@')).toBe(false);
|
|
});
|
|
});
|
|
});
|