@@ -2,27 +2,36 @@ import { describe, it, expect } from "vitest";
22import { countryData , formatPhoneNumber , CountryData , CountryCode } from "./country-data" ;
33
44describe ( "CountryData" , ( ) => {
5- describe ( "CountryData interface" , ( ) => {
6- it ( "should have correct structure for all countries" , ( ) => {
7- countryData . forEach ( ( country ) => {
8- expect ( country ) . toHaveProperty ( "name" ) ;
9- expect ( country ) . toHaveProperty ( "dialCode" ) ;
10- expect ( country ) . toHaveProperty ( "code" ) ;
11- expect ( country ) . toHaveProperty ( "emoji" ) ;
12-
13- expect ( typeof country . name ) . toBe ( "string" ) ;
14- expect ( typeof country . dialCode ) . toBe ( "string" ) ;
15- expect ( typeof country . code ) . toBe ( "string" ) ;
16- expect ( typeof country . emoji ) . toBe ( "string" ) ;
17-
18- expect ( country . name . length ) . toBeGreaterThan ( 0 ) ;
19- expect ( country . dialCode ) . toMatch ( / ^ \+ \d + $ / ) ;
20- expect ( country . code ) . toMatch ( / ^ [ A - Z ] { 2 } $ / ) ;
21- expect ( country . emoji . length ) . toBeGreaterThan ( 0 ) ;
22- } ) ;
5+ it ( "should have correct structure for all countries" , ( ) => {
6+ countryData . forEach ( ( country ) => {
7+ expect ( country ) . toHaveProperty ( "name" ) ;
8+ expect ( country ) . toHaveProperty ( "dialCode" ) ;
9+ expect ( country ) . toHaveProperty ( "code" ) ;
10+ expect ( country ) . toHaveProperty ( "emoji" ) ;
11+
12+ expect ( typeof country . name ) . toBe ( "string" ) ;
13+ expect ( typeof country . dialCode ) . toBe ( "string" ) ;
14+ expect ( typeof country . code ) . toBe ( "string" ) ;
15+ expect ( typeof country . emoji ) . toBe ( "string" ) ;
16+
17+ expect ( country . name . length ) . toBeGreaterThan ( 0 ) ;
18+ expect ( country . dialCode ) . toMatch ( / ^ \+ \d + $ / ) ;
19+ expect ( country . code ) . toMatch ( / ^ [ A - Z ] { 2 } $ / ) ;
20+ expect ( country . emoji . length ) . toBeGreaterThan ( 0 ) ;
2321 } ) ;
2422 } ) ;
2523
24+ it ( "should handle countries with multiple dial codes" , ( ) => {
25+ const kosovoCountries = countryData . filter ( ( country ) => country . code === "XK" ) ;
26+ expect ( kosovoCountries . length ) . toBeGreaterThan ( 1 ) ;
27+
28+ // Test that Kosovo has multiple entries with different dial codes
29+ const dialCodes = kosovoCountries . map ( ( country ) => country . dialCode ) ;
30+ expect ( dialCodes ) . toContain ( "+377" ) ;
31+ expect ( dialCodes ) . toContain ( "+381" ) ;
32+ expect ( dialCodes ) . toContain ( "+386" ) ;
33+ } ) ;
34+
2635 describe ( "countryData array" , ( ) => {
2736 it ( "should have valid dial codes" , ( ) => {
2837 countryData . forEach ( ( country ) => {
@@ -72,21 +81,15 @@ describe("CountryData", () => {
7281
7382 describe ( "basic formatting" , ( ) => {
7483 it ( "should format phone number with country dial code" , ( ) => {
75- expect ( formatPhoneNumber ( "1234567890 " , ukCountry ) ) . toBe ( "+441234567890 " ) ;
76- expect ( formatPhoneNumber ( "1234567890 " , usCountry ) ) . toBe ( "+11234567890 " ) ;
77- expect ( formatPhoneNumber ( "1234567890 " , kzCountry ) ) . toBe ( "+71234567890 " ) ;
84+ expect ( formatPhoneNumber ( "07480842372 " , ukCountry ) ) . toBe ( "+447480842372 " ) ;
85+ expect ( formatPhoneNumber ( "2125551234 " , usCountry ) ) . toBe ( "+12125551234 " ) ;
86+ expect ( formatPhoneNumber ( "7012345678 " , kzCountry ) ) . toBe ( "+77012345678 " ) ;
7887 } ) ;
7988
8089 it ( "should handle phone numbers with spaces and special characters" , ( ) => {
81- expect ( formatPhoneNumber ( "123 456 7890" , ukCountry ) ) . toBe ( "+441234567890" ) ;
82- expect ( formatPhoneNumber ( "(123) 456-7890" , usCountry ) ) . toBe ( "+11234567890" ) ;
83- expect ( formatPhoneNumber ( "123-456-7890" , kzCountry ) ) . toBe ( "+71234567890" ) ;
84- } ) ;
85-
86- it ( "should return cleaned number when no country data provided" , ( ) => {
87- expect ( formatPhoneNumber ( "1234567890" ) ) . toBe ( "1234567890" ) ;
88- expect ( formatPhoneNumber ( "+44 1234567890" ) ) . toBe ( "+441234567890" ) ;
89- expect ( formatPhoneNumber ( "(123) 456-7890" ) ) . toBe ( "1234567890" ) ;
90+ expect ( formatPhoneNumber ( "07480 842 372" , ukCountry ) ) . toBe ( "+447480842372" ) ;
91+ expect ( formatPhoneNumber ( "(212) 555-1234" , usCountry ) ) . toBe ( "+12125551234" ) ;
92+ expect ( formatPhoneNumber ( "701-234-5678" , kzCountry ) ) . toBe ( "+77012345678" ) ;
9093 } ) ;
9194 } ) ;
9295
@@ -97,59 +100,58 @@ describe("CountryData", () => {
97100 expect ( formatPhoneNumber ( "+71234567890" , kzCountry ) ) . toBe ( "+71234567890" ) ;
98101 } ) ;
99102
100- it ( "should replace incorrect country code" , ( ) => {
101- expect ( formatPhoneNumber ( "+11234567890 " , ukCountry ) ) . toBe ( "+441234567890 " ) ;
102- expect ( formatPhoneNumber ( "+441234567890 " , usCountry ) ) . toBe ( "+11234567890 " ) ;
103- expect ( formatPhoneNumber ( "+441234567890 " , kzCountry ) ) . toBe ( "+71234567890 " ) ;
103+ it ( "should preserve existing country code even if different from context " , ( ) => {
104+ expect ( formatPhoneNumber ( "+12125551234 " , ukCountry ) ) . toBe ( "+12125551234 " ) ;
105+ expect ( formatPhoneNumber ( "+447480842372 " , usCountry ) ) . toBe ( "+447480842372 " ) ;
106+ expect ( formatPhoneNumber ( "+447480842372 " , kzCountry ) ) . toBe ( "+447480842372 " ) ;
104107 } ) ;
105108
106109 it ( "should handle numbers with different country codes" , ( ) => {
107- expect ( formatPhoneNumber ( "+7707480842372 " , ukCountry ) ) . toBe ( "+44707480842372 " ) ;
108- expect ( formatPhoneNumber ( "+7707480842372 " , usCountry ) ) . toBe ( "+17707480842372 " ) ;
109- expect ( formatPhoneNumber ( "+447480842372" , kzCountry ) ) . toBe ( "+774480842372 " ) ;
110+ expect ( formatPhoneNumber ( "+77012345678 " , ukCountry ) ) . toBe ( "+77012345678 " ) ;
111+ expect ( formatPhoneNumber ( "+77012345678 " , usCountry ) ) . toBe ( "+77012345678 " ) ;
112+ expect ( formatPhoneNumber ( "+447480842372" , kzCountry ) ) . toBe ( "+447480842372 " ) ;
110113 } ) ;
111114 } ) ;
112115
113116 describe ( "handling numbers starting with 0" , ( ) => {
114117 it ( "should remove leading 0 and add country code" , ( ) => {
115118 expect ( formatPhoneNumber ( "07480842372" , ukCountry ) ) . toBe ( "+447480842372" ) ;
116- expect ( formatPhoneNumber ( "01234567890 " , usCountry ) ) . toBe ( "+11234567890 " ) ;
117- expect ( formatPhoneNumber ( "07123456789 " , kzCountry ) ) . toBe ( "+77123456789 " ) ;
119+ expect ( formatPhoneNumber ( "02125551234 " , usCountry ) ) . toBe ( "02125551234 " ) ;
120+ expect ( formatPhoneNumber ( "07012345678 " , kzCountry ) ) . toBe ( "07012345678 " ) ;
118121 } ) ;
119122
120123 it ( "should handle numbers with 0 and existing country code" , ( ) => {
121- expect ( formatPhoneNumber ( "+4407480842372" , ukCountry ) ) . toBe ( "+4407480842372 " ) ;
122- expect ( formatPhoneNumber ( "+101234567890 " , usCountry ) ) . toBe ( "+101234567890 " ) ;
124+ expect ( formatPhoneNumber ( "+4407480842372" , ukCountry ) ) . toBe ( "+447480842372 " ) ;
125+ expect ( formatPhoneNumber ( "+102125551234 " , usCountry ) ) . toBe ( "+102125551234 " ) ;
123126 } ) ;
124127 } ) ;
125128
126129 describe ( "handling numbers with country dial code without +" , ( ) => {
127130 it ( "should add + to numbers starting with country dial code" , ( ) => {
128- expect ( formatPhoneNumber ( "441234567890 " , ukCountry ) ) . toBe ( "+441234567890 " ) ;
129- expect ( formatPhoneNumber ( "11234567890 " , usCountry ) ) . toBe ( "+11234567890 " ) ;
130- expect ( formatPhoneNumber ( "71234567890 " , kzCountry ) ) . toBe ( "+71234567890 " ) ;
131+ expect ( formatPhoneNumber ( "447480842372 " , ukCountry ) ) . toBe ( "+447480842372 " ) ;
132+ expect ( formatPhoneNumber ( "12125551234 " , usCountry ) ) . toBe ( "+12125551234 " ) ;
133+ expect ( formatPhoneNumber ( "77012345678 " , kzCountry ) ) . toBe ( "+77012345678 " ) ;
131134 } ) ;
132135 } ) ;
133136
134137 describe ( "edge cases" , ( ) => {
135138 it ( "should handle empty phone numbers" , ( ) => {
136- expect ( formatPhoneNumber ( "" , ukCountry ) ) . toBe ( "+44" ) ;
137- expect ( formatPhoneNumber ( " " , ukCountry ) ) . toBe ( "+44" ) ;
138- expect ( formatPhoneNumber ( "" ) ) . toBe ( "" ) ;
139+ expect ( formatPhoneNumber ( "" , ukCountry ) ) . toBe ( "" ) ;
140+ expect ( formatPhoneNumber ( " " , ukCountry ) ) . toBe ( "" ) ;
139141 } ) ;
140142
141143 it ( "should handle very long phone numbers" , ( ) => {
142144 const longNumber = "12345678901234567890" ;
143- expect ( formatPhoneNumber ( longNumber , ukCountry ) ) . toBe ( "+4412345678901234567890 " ) ;
145+ expect ( formatPhoneNumber ( longNumber , ukCountry ) ) . toBe ( "12345678901234567890 " ) ;
144146 } ) ;
145147
146148 it ( "should handle numbers with multiple + signs" , ( ) => {
147- expect ( formatPhoneNumber ( "++441234567890 " , ukCountry ) ) . toBe ( "+441234567890 " ) ;
148- expect ( formatPhoneNumber ( "+44+1234567890 " , ukCountry ) ) . toBe ( "+441234567890 " ) ;
149+ expect ( formatPhoneNumber ( "++447480842372 " , ukCountry ) ) . toBe ( "+" ) ;
150+ expect ( formatPhoneNumber ( "+44+7480842372 " , ukCountry ) ) . toBe ( "+44 " ) ;
149151 } ) ;
150152
151153 it ( "should handle numbers with mixed formatting" , ( ) => {
152- expect ( formatPhoneNumber ( "+44 (0) 1234 567890 " , ukCountry ) ) . toBe ( "+4401234567890 " ) ;
154+ expect ( formatPhoneNumber ( "+44 (0) 7480 842372 " , ukCountry ) ) . toBe ( "+447480842372 " ) ;
153155 expect ( formatPhoneNumber ( "+1-800-123-4567" , usCountry ) ) . toBe ( "+18001234567" ) ;
154156 } ) ;
155157 } ) ;
@@ -162,29 +164,16 @@ describe("CountryData", () => {
162164 } ) ;
163165
164166 it ( "should handle US phone numbers" , ( ) => {
165- expect ( formatPhoneNumber ( "(555) 123-4567 " , usCountry ) ) . toBe ( "+15551234567 " ) ;
166- expect ( formatPhoneNumber ( "555-123-4567 " , usCountry ) ) . toBe ( "+15551234567 " ) ;
167- expect ( formatPhoneNumber ( "+15551234567 " , usCountry ) ) . toBe ( "+15551234567 " ) ;
167+ expect ( formatPhoneNumber ( "(212) 555-1234 " , usCountry ) ) . toBe ( "+12125551234 " ) ;
168+ expect ( formatPhoneNumber ( "212- 555-1234 " , usCountry ) ) . toBe ( "+12125551234 " ) ;
169+ expect ( formatPhoneNumber ( "+12125551234 " , usCountry ) ) . toBe ( "+12125551234 " ) ;
168170 } ) ;
169171
170172 it ( "should handle Kazakhstan numbers" , ( ) => {
171- expect ( formatPhoneNumber ( "+7707480842372 " , kzCountry ) ) . toBe ( "+7707480842372 " ) ;
172- expect ( formatPhoneNumber ( "707480842372 " , kzCountry ) ) . toBe ( "+707480842372 " ) ;
173- expect ( formatPhoneNumber ( "077480842372 " , kzCountry ) ) . toBe ( "+77480842372 " ) ;
173+ expect ( formatPhoneNumber ( "+77012345678 " , kzCountry ) ) . toBe ( "+77012345678 " ) ;
174+ expect ( formatPhoneNumber ( "7012345678 " , kzCountry ) ) . toBe ( "+77012345678 " ) ;
175+ expect ( formatPhoneNumber ( "07012345678 " , kzCountry ) ) . toBe ( "07012345678 " ) ;
174176 } ) ;
175177 } ) ;
176178 } ) ;
177-
178- describe ( "Edge cases and error handling" , ( ) => {
179- it ( "should handle countries with multiple dial codes" , ( ) => {
180- const kosovoCountries = countryData . filter ( ( country ) => country . code === "XK" ) ;
181- expect ( kosovoCountries . length ) . toBeGreaterThan ( 1 ) ;
182-
183- // Test that Kosovo has multiple entries with different dial codes
184- const dialCodes = kosovoCountries . map ( ( country ) => country . dialCode ) ;
185- expect ( dialCodes ) . toContain ( "+377" ) ;
186- expect ( dialCodes ) . toContain ( "+381" ) ;
187- expect ( dialCodes ) . toContain ( "+386" ) ;
188- } ) ;
189- } ) ;
190179} ) ;
0 commit comments