powr-sdk-api
Version:
Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.
40 lines (39 loc) • 1.46 kB
JavaScript
;
const {
coercePhoneForStorage,
normalizePhone,
getPhoneLookupVariants
} = require('./phone');
describe('coercePhoneForStorage', () => {
it('normalizes a valid 10-digit Indian number', () => {
expect(coercePhoneForStorage('7903658469')).toBe('+917903658469');
});
it('salvages digits when trailing junk is present', () => {
expect(coercePhoneForStorage('7903658469 welcome')).toBe('+917903658469');
});
it('returns null for non-phone text', () => {
expect(coercePhoneForStorage('welcome')).toBeNull();
expect(coercePhoneForStorage('')).toBeNull();
});
it('never returns raw corrupted input', () => {
const result = coercePhoneForStorage('7903658469 welcome');
expect(result).not.toContain('welcome');
expect(result).toMatch(/^\+\d+$/);
});
});
describe('getPhoneLookupVariants with corrupted input', () => {
it('includes E.164 variants for phone with trailing junk', () => {
const variants = getPhoneLookupVariants('7903658469 welcome');
expect(variants).toContain('+917903658469');
expect(variants).toContain('7903658469');
});
});
describe('normalizePhone', () => {
it('returns empty string for non-phone text', () => {
expect(normalizePhone('welcome')).toBe('');
expect(normalizePhone('')).toBe('');
});
it('parses phone even when trailing junk is present (libphonenumber)', () => {
expect(normalizePhone('7903658469 welcome')).toBe('+917903658469');
});
});