Skip to content

Commit

Permalink
[chore]: add test data from fakenumbers
Browse files Browse the repository at this point in the history
  • Loading branch information
RgnDunes committed Jun 19, 2024
1 parent 51e6baf commit 93b1ab5
Show file tree
Hide file tree
Showing 5 changed files with 3,010 additions and 45 deletions.
3 changes: 2 additions & 1 deletion packages/i18nify-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"tsconfig-paths": "^4.2.0"
},
"dependencies": {
"@internationalized/date": "^3.5.2"
"@internationalized/date": "^3.5.2",
"libphonenumber-js": "^1.11.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,91 @@
import {
CountryCode,
isValidPhoneNumber as googleIsValidPhoneNumber,
} from 'libphonenumber-js';

import PHONE_NUMBERS_JSON from './mocks/phoneNumbers.json';
import { isValidPhoneNumber } from '../index';
import { CountryCodeType } from '../../types';
import { PhoneNumbersMockData } from '../types';

describe('isValidPhoneNumber', () => {
const validTestDataSet = [
{ countryCode: 'IN', phoneNumber: '+917394926646' },
{ countryCode: 'MY', phoneNumber: '+60123456789' },
];

const invalidTestDataSet = [
{ countryCode: 'IN', phoneNumber: '1234' },
{ countryCode: 'MY', phoneNumber: '60123' },
];

validTestDataSet.forEach((dataset) => {
it(`should validate a valid phone number for ${dataset.countryCode}`, () => {
const isValid = isValidPhoneNumber(
dataset.phoneNumber,
dataset.countryCode as any,
);
expect(isValid).toBe(true);
describe('test using inhouse validator', () => {
const validTestDataSet = [
{ countryCode: 'IN', phoneNumber: '+917394926646' },
{ countryCode: 'MY', phoneNumber: '+60123456789' },
];

const invalidTestDataSet = [
{ countryCode: 'IN', phoneNumber: '1234' },
{ countryCode: 'MY', phoneNumber: '60123' },
];

validTestDataSet.forEach((dataset) => {
it(`should validate a valid phone number for ${dataset.countryCode}`, () => {
const isValid = isValidPhoneNumber(
dataset.phoneNumber,
dataset.countryCode as any,
);
expect(isValid).toBe(true);
});
});
});

invalidTestDataSet.forEach((dataset) => {
it(`should reject an invalid phone number for ${dataset.countryCode}`, () => {
const isValid = isValidPhoneNumber(
dataset.phoneNumber,
dataset.countryCode as any,
);
invalidTestDataSet.forEach((dataset) => {
it(`should reject an invalid phone number for ${dataset.countryCode}`, () => {
const isValid = isValidPhoneNumber(
dataset.phoneNumber,
dataset.countryCode as any,
);
expect(isValid).toBe(false);
});
});

it('should handle a invalid country code and detect it from phone number to validate it', () => {
const phoneNumber = '1234567890';
const countryCode = 'XYZ';
const isValid = isValidPhoneNumber(phoneNumber, countryCode as any);
expect(isValid).toBe(false);
});
});

it('should handle a invalid country code and detect it from phone number to validate it', () => {
const phoneNumber = '1234567890';
const countryCode = 'XYZ';
const isValid = isValidPhoneNumber(phoneNumber, countryCode as any);
expect(isValid).toBe(false);
});
it('should handle a missing phoneNumber', () => {
const phoneNumber = '';
const countryCode = 'MY';
const isValid = isValidPhoneNumber(phoneNumber, countryCode as any);
expect(isValid).toBe(false);
});

it('should handle a missing phoneNumber', () => {
const phoneNumber = '';
const countryCode = 'MY';
const isValid = isValidPhoneNumber(phoneNumber, countryCode as any);
expect(isValid).toBe(false);
it('should return false if the countryCode is not supported', () => {
const unsupportedCountryCode = 'XXX';
const phoneNumber = '+1234567890';
const result = isValidPhoneNumber(
phoneNumber,
unsupportedCountryCode as any,
);
expect(result).toBe(false);
});
});

it('should return false if the countryCode is not supported', () => {
const unsupportedCountryCode = 'XXX';
const phoneNumber = '+1234567890';
const result = isValidPhoneNumber(
phoneNumber,
unsupportedCountryCode as any,
);
expect(result).toBe(false);
describe('test via libphonenumber-js validator', () => {
const phoneNumbersData: PhoneNumbersMockData =
PHONE_NUMBERS_JSON as PhoneNumbersMockData;
Object.keys(phoneNumbersData).forEach((countryCode) => {
it(`should match output with libphonenumber-js for ${countryCode}`, () => {
phoneNumbersData[countryCode].forEach(
(data: { PhoneNumber: string }) => {
expect(
isValidPhoneNumber(
data.PhoneNumber,
countryCode as CountryCodeType,
),
).toBe(
googleIsValidPhoneNumber(
data.PhoneNumber,
countryCode as CountryCode,
),
);
},
);
});
});
});
});
Loading

0 comments on commit 93b1ab5

Please sign in to comment.