Skip to content

Commit 004b4d0

Browse files
Brian J.imbrianj
authored andcommitted
Update tests to get back to 100% coverage. Remove impossible logic
1 parent 33d78b9 commit 004b4d0

File tree

6 files changed

+100
-11
lines changed

6 files changed

+100
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/base.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ describe('Phone number pretty formatting', () => {
433433
regionCode: '47',
434434
format: findPhoneFormat({ regionCode: '47', e164: '+471740876543' }),
435435
},
436+
egyptStringFormat: {
437+
e164: '+201012345678',
438+
regionCode: '20',
439+
format: findPhoneFormat({ regionCode: '20', e164: '+201012345678' }),
440+
},
436441
};
437442

438443
expect(formatPhoneNumber(testNumbers.nullCase)).toBe(null);
@@ -443,5 +448,68 @@ describe('Phone number pretty formatting', () => {
443448
expect(formatPhoneNumber(testNumbers.norwayUnexpected)).toBe(
444449
'+47174087654',
445450
);
451+
// Egypt has a string format (not array), this tests the else if (formatRaw)
452+
expect(formatPhoneNumber(testNumbers.egyptStringFormat)).toBe(
453+
'+20 101 234 5678',
454+
);
455+
});
456+
});
457+
458+
describe('E.164 formatting edge cases', () => {
459+
describe('formatPhoneNumberForE164 with missing required fields', () => {
460+
it('should format US numbers with separate area code correctly', () => {
461+
expect(
462+
formatPhoneNumberForE164({
463+
regionCode: '1',
464+
areaCode: '310',
465+
localNumber: '3496200',
466+
}),
467+
).toBe('+13103496200');
468+
});
469+
470+
it('should format international numbers without area code correctly', () => {
471+
expect(
472+
formatPhoneNumberForE164({
473+
regionCode: '44',
474+
areaCode: null,
475+
localNumber: '2079460958',
476+
}),
477+
).toBe('+442079460958');
478+
});
479+
});
480+
481+
describe('International number parsing with unknown region codes', () => {
482+
it('should handle international numbers that do not match any known region code in the loop', () => {
483+
// Test a number with + prefix and valid length but an unrecognized region code
484+
// The parser should try all region code lengths (1, 2, 3 digits) but find no match in PHONE_FORMATS
485+
const phoneParts = getPhoneParts('+00012345678');
486+
// Should not have regionCode since +000, +00, or +0 are not valid region codes
487+
expect(phoneParts.regionCode).toBeNull();
488+
expect(phoneParts.localNumber).toBeNull();
489+
});
490+
491+
it('should handle numbers that skip the international parsing else-if block entirely', () => {
492+
// Test numbers with + prefix that exceed the maximum length for international number parsing
493+
// The parser checks strippedPhoneNumber.length <= 14, so 16 chars (15 digits + plus sign) is too long
494+
// This ensures the else-if branch is not entered, leaving regionCode and localNumber as null
495+
const longNumber = getPhoneParts('+123456789012345'); // 15 digits, 16 chars total
496+
expect(longNumber.regionCode).toBeNull();
497+
expect(longNumber.localNumber).toBeNull();
498+
});
499+
});
500+
501+
describe('findNumbersInString with multiple phone numbers', () => {
502+
it('should extract multiple valid phone numbers from a single string', () => {
503+
const text = 'Call me at +1 (310) 349-6200 or +44 20 7946 0958.';
504+
const results = findNumbersInString(text);
505+
expect(results.length).toBe(2);
506+
expect(results[0].e164).toBe('+13103496200');
507+
expect(results[1].e164).toBe('+442079460958');
508+
});
509+
510+
it('should return an empty array when no valid phone numbers are present', () => {
511+
const text = 'No phone numbers here!';
512+
expect(findNumbersInString(text)).toEqual([]);
513+
});
446514
});
447515
});

src/__tests__/geo.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,26 @@ describe('Extracts all useful phone info from a long string of text', () => {
586586
expect(numbers).toEqual(longStringOutput);
587587
});
588588
});
589+
590+
describe('Area code region information mapping', () => {
591+
it('should include region details when area code has region information', () => {
592+
const result = findTimeFromAreaCode('206', new Date('2024-07-15T08:00:00'));
593+
594+
expect(result.region).toBeDefined();
595+
expect(result.region.name).toBe('United States');
596+
expect(result.region.code).toBe('US');
597+
expect(result.state).toBeDefined();
598+
expect(result.state.name).toBe('Washington');
599+
expect(result.state.code).toBe('WA');
600+
});
601+
602+
it('should include region flag for area codes with region property', () => {
603+
// Test with Puerto Rico which has a region property with flag
604+
const result = findTimeFromAreaCode('787', new Date('2024-07-15T08:00:00'));
605+
606+
expect(result.region).toBeDefined();
607+
expect(result.region.flag).toBeDefined();
608+
expect(result.region.name).toBe('Puerto Rico');
609+
expect(result.region.code).toBe('PR');
610+
});
611+
});

src/base.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface PhoneParts {
55
formattedNumber: string | null;
66
href: string | null;
77
localNumber: string | null;
8-
rawNumber: string | undefined; // Can be undefined for getPhoneParts()
8+
rawNumber: string;
99
regionCode: string | null;
1010
}
1111

src/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export const findPhoneFormat = ({ regionCode, e164 }) => {
371371
}
372372
}
373373
// Some region (such as the US) will have a consistent format, so we expect a string.
374-
else if (formatRaw) {
374+
else {
375375
format = formatRaw;
376376
}
377377
}

src/geo.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,11 @@ export function findTimeFromAreaCode(areaCode, date = new Date()) {
155155
code: AREA_CODES[areaCode].code,
156156
};
157157

158-
if (AREA_CODES[areaCode].region) {
159-
returnTime.region = {
160-
name: AREA_CODES[areaCode].region.name,
161-
code: AREA_CODES[areaCode].region.code,
162-
flag: AREA_CODES[areaCode].region.flag,
163-
};
164-
}
158+
returnTime.region = {
159+
name: AREA_CODES[areaCode].region.name,
160+
code: AREA_CODES[areaCode].region.code,
161+
flag: AREA_CODES[areaCode].region.flag,
162+
};
165163
}
166164

167165
if (!stateName || !STATE_TIMEZONES[stateName]) {

0 commit comments

Comments
 (0)