test-numbers-generator
Version:
Generate and validate European test phone numbers (mobile and landline) in safe, non-existent ranges.
223 lines (191 loc) • 9.07 kB
text/typescript
import {
generateTestMobileNumber,
generateTestLandlineNumber
} from '../index';
import { generateTestKvKNumber } from '../kvkGenerator';
import { generateTestPostcode } from '../postcodeGenerator';
import { generateTestBSN, isValidBSN } from '../bsnGenerators';
import { generateTestDutchIBAN, isValidDutchIBAN } from '../ibanGenerator';
import { SupportedCountry } from '../mobileNumberGenerators';
import { isValidDutchPostcode } from '../postcodeGenerator';
import { isTestMobileNumber, isTestLandlineNumber, isTestPhoneNumber } from '../validators';
describe('Mobile Number Generators', () => {
// Test basic functionality for all countries
for (const country of Object.keys(generateTestMobileNumber) as SupportedCountry[]) {
it(`should generate valid mobile number for ${country}`, () => {
const num = generateTestMobileNumber[country]();
expect(typeof num).toBe('string');
expect(num.length).toBeGreaterThan(5);
// Test that generated number passes validator
expect(isTestMobileNumber[country](num)).toBe(true);
});
}
// Specific format tests for corrected implementations
it('should generate Netherlands mobile number with correct format', () => {
const num = generateTestMobileNumber.Netherlands();
expect(num).toMatch(/^0031 6 \d{8}$/);
expect(num.length).toBe(15);
});
it('should generate Germany mobile number with correct format', () => {
const num = generateTestMobileNumber.Germany();
expect(num).toMatch(/^0049 01[567] \d{7}$/);
expect(num.length).toBe(16);
});
it('should generate Belgium mobile number with correct format', () => {
const num = generateTestMobileNumber.Belgium();
expect(num).toMatch(/^0032 4[789] \d{7}$/); // Updated to 47/48/49 without national 0
expect(num.length).toBe(15);
});
it('should generate France mobile number with correct format', () => {
const num = generateTestMobileNumber.France();
expect(num).toMatch(/^0033 [67] \d{8}$/);
expect(num.length).toBe(15);
});
it('should generate UK mobile number with correct format', () => {
const num = generateTestMobileNumber.UnitedKingdom();
expect(num).toMatch(/^0044 7 \d{9}$/);
expect(num.length).toBe(16);
});
it('should generate Spain mobile number with correct format', () => {
const num = generateTestMobileNumber.Spain();
expect(num).toMatch(/^0034 [67]\d{8}$/);
expect(num.length).toBe(14);
});
it('should generate Italy mobile number with correct format', () => {
const num = generateTestMobileNumber.Italy();
expect(num).toMatch(/^0039 3\d{2} \d{7}$/);
expect(num.length).toBe(16);
});
it('should generate Austria mobile number with correct format', () => {
const num = generateTestMobileNumber.Austria();
expect(num).toMatch(/^0043 (664|676|699) \d{7}$/); // Updated without national 0
expect(num.length).toBe(16);
});
it('should generate Switzerland mobile number with correct format', () => {
const num = generateTestMobileNumber.Switzerland();
expect(num).toMatch(/^0041 7[56789] \d{6}$/); // Updated to include 75 and without national 0
expect(num.length).toBe(14);
});
it('should generate Sweden mobile number with correct format', () => {
const num = generateTestMobileNumber.Sweden();
expect(num).toMatch(/^0046 7 \d{8}$/);
expect(num.length).toBe(15);
});
it('should generate Norway mobile number with correct format', () => {
const num = generateTestMobileNumber.Norway();
expect(num).toMatch(/^0047 [49]\d{7}$/);
expect(num.length).toBe(13);
});
it('should generate Denmark mobile number with correct format', () => {
const num = generateTestMobileNumber.Denmark();
expect(num).toMatch(/^0045 (20|21|22|23|24|25|26|27|28|29|30|31)\d{6}$/); // Updated to 20-31 range
expect(num.length).toBe(13);
});
it('should generate Finland mobile number with correct format', () => {
const num = generateTestMobileNumber.Finland();
expect(num).toMatch(/^00358 (4[0-9]|50|457|59) \d{6}$/); // Updated to include 457 and 59
expect(num.length).toBe(15); // Corrected from 16 to 15
});
it('should generate Portugal mobile number with correct format', () => {
const num = generateTestMobileNumber.Portugal();
expect(num).toMatch(/^00351 9[1236] \d{7}$/);
expect(num.length).toBe(16);
});
it('should generate Ireland mobile number with correct format', () => {
const num = generateTestMobileNumber.Ireland();
expect(num).toMatch(/^00353 8[35679] \d{6}$/); // Updated to include 83 and without national 0
expect(num.length).toBe(15);
});
it('should generate Turkey mobile number with correct format', () => {
const num = generateTestMobileNumber.Turkey();
expect(num).toMatch(/^0090 53[1-9] \d{6}$/); // Updated to 5xx format without national 0
expect(num.length).toBe(15);
});
it('should generate Morocco mobile number with correct format', () => {
const num = generateTestMobileNumber.Morocco();
expect(num).toMatch(/^00212 [67] \d{8}$/); // Updated without national 0
expect(num.length).toBe(16); // Corrected from 15 to 16
});
});
describe('Mobile Number Validators', () => {
// Test that validators correctly validate generated numbers
for (const country of Object.keys(generateTestMobileNumber) as SupportedCountry[]) {
it(`should validate generated ${country} mobile numbers`, () => {
for (let i = 0; i < 5; i++) {
const num = generateTestMobileNumber[country]();
expect(isTestMobileNumber[country](num)).toBe(true);
}
});
}
// Test invalid numbers are rejected
it('should reject invalid mobile numbers', () => {
expect(isTestMobileNumber.Germany('0049 018 1234567')).toBe(false); // Wrong prefix (018 not valid - only 015/016/017)
expect(isTestMobileNumber.France('0033 8 12345678')).toBe(false); // Wrong prefix (8 instead of 6/7)
expect(isTestMobileNumber.Netherlands('0031 7 12345678')).toBe(false); // Wrong mobile prefix
expect(isTestMobileNumber.Germany('0049 015 123456')).toBe(false); // Too short
expect(isTestMobileNumber.Belgium('0032 046 1234567')).toBe(false); // Wrong prefix (046 with national 0)
expect(isTestMobileNumber.Spain('0034 5123456789')).toBe(false); // Wrong prefix (5 instead of 6/7)
expect(isTestMobileNumber.UnitedKingdom('0044 8 123456789')).toBe(false); // Wrong prefix (8 instead of 7)
expect(isTestMobileNumber.Denmark('0045 191234567')).toBe(false); // Wrong prefix (19 not in 20-31 range)
expect(isTestMobileNumber.Switzerland('0041 074 123456')).toBe(false); // Wrong prefix (074 not in 75-79 range)
expect(isTestMobileNumber.Austria('0043 0664 1234567')).toBe(false); // Wrong format (includes national 0)
expect(isTestMobileNumber.Turkey('0090 0532 123456')).toBe(false); // Wrong format (includes national 0)
expect(isTestMobileNumber.Ireland('00353 084 123456')).toBe(false); // Wrong prefix (084 not valid)
});
});
describe('Landline Number Generators', () => {
for (const country of Object.keys(generateTestLandlineNumber) as SupportedCountry[]) {
it(`should generate valid landline number for ${country}`, () => {
const num = generateTestLandlineNumber[country]();
expect(typeof num).toBe('string');
expect(num.length).toBeGreaterThan(5);
expect(isTestLandlineNumber[country](num)).toBe(true);
});
}
});
describe('General Phone Number Validator', () => {
it('should identify test mobile numbers', () => {
const mobileNum = generateTestMobileNumber.Netherlands();
expect(isTestPhoneNumber(mobileNum)).toBe(true);
});
it('should identify test landline numbers', () => {
const landlineNum = generateTestLandlineNumber.Netherlands();
expect(isTestPhoneNumber(landlineNum)).toBe(true);
});
it('should reject non-test numbers', () => {
expect(isTestPhoneNumber('+31 6 12345678')).toBe(false);
expect(isTestPhoneNumber('020 1234567')).toBe(false);
});
});
describe('BSN Generator & Validator', () => {
it('should generate valid Dutch BSN', () => {
const bsn = generateTestBSN();
expect(isValidBSN(bsn)).toBe(true);
});
it('should reject invalid BSN', () => {
expect(isValidBSN('123456789')).toBe(false);
});
});
describe('IBAN Generator & Validator', () => {
it('should generate valid Dutch IBAN', () => {
const iban = generateTestDutchIBAN();
expect(isValidDutchIBAN(iban)).toBe(true);
});
it('should reject invalid IBAN', () => {
expect(isValidDutchIBAN('NL00TEST0123456789')).toBe(false);
});
});
describe('KvK Generator', () => {
it('should generate valid KvK number', () => {
const kvk = generateTestKvKNumber();
expect(typeof kvk).toBe('string');
expect(kvk.length).toBe(8);
expect(/^\d{8}$/.test(kvk)).toBe(true);
});
});
describe('Postcode Generator & Validator', () => {
it('should generate valid Dutch postcode', () => {
const postcode = generateTestPostcode.Netherlands();
expect(isValidDutchPostcode(postcode)).toBe(true);
});
});