@@ -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} ) ;
0 commit comments