simujs
Version:
A simple JavaScript utility for identifying Tanzanian mobile network operators based on phone numbers, using prefix data regulated by TCRA.
64 lines (50 loc) • 2.16 kB
text/typescript
import simu, { format, Operators } from "../simu";
describe('format', () => {
it('should return national number for valid TZ number with +255', () => {
expect(format('+255754123456')).toBe(754123456);
});
it('should return national number for valid TZ number with 0', () => {
expect(format('0754123456')).toBe(754123456);
});
it('should return national number for valid TZ number without prefix', () => {
expect(format('754123456')).toBe(754123456);
});
it('should return undefined for invalid number', () => {
expect(format('123')).toBeUndefined();
});
it('should return undefined for non-TZ number', () => {
expect(format('+254712345678')).toBeUndefined();
});
it('should return undefined for empty input', () => {
expect(format('')).toBeUndefined();
});
it('should return undefined for null input', () => {
expect(format(null as any)).toBeUndefined();
});
it('should return undefined for malformed phone string', () => {
expect(format('not-a-phone-number')).toBeUndefined();
});
});
describe("Network Operator Identification", () => {
it("should identify the correct network operator for a valid phone number", () => {
const phoneNumber = '0755667788';
const network = simu(phoneNumber);
expect(network?.name).toBe(Operators.Vodacom);
});
it("should return undefined for an invalid phone number", () => {
const invalidPhoneNumber = '075566778';
const network = simu(invalidPhoneNumber);
expect(network).toBeUndefined();
});
it("should correctly identify network operators for a variety of phone numbers", () => {
const phoneNumbers = [
{ phoneNumber: '0785667788', operator: Operators.Airtel },
{ phoneNumber: '0655667788', operator: Operators.Yas },
{ phoneNumber: '0615667788', operator: Operators.Halotel },
];
phoneNumbers.forEach(({ phoneNumber, operator }) => {
const network = simu(phoneNumber);
expect(network?.name).toBe(operator);
});
});
});