-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore]: add test data from fakenumbers
- Loading branch information
Showing
5 changed files
with
3,010 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 78 additions & 44 deletions
122
packages/i18nify-js/src/modules/phoneNumber/__tests__/isValidPhoneNumber.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
); | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.