|
1 | | -import { cloneValue, isObject, isDate } from './utils'; |
| 1 | +import { cloneValue, isObject, isDate, getCurrencyCode, getCurrencySymbol, getLocaleFirstDayOfWeek, formatDate, formatCurrency, formatPercent, formatNumber, getLocaleDateFormat, getLocaleDateTimeFormat } from './utils'; |
2 | 2 | import { SampleTestData } from '../test-utils/sample-test-data.spec'; |
3 | 3 |
|
4 | 4 | describe('Utils', () => { |
@@ -253,4 +253,133 @@ describe('Utils', () => { |
253 | 253 | expect(isDate(variable)).toBeFalsy(); |
254 | 254 | }); |
255 | 255 | }); |
| 256 | + |
| 257 | + describe('Localization', () => { |
| 258 | + describe('number formatting', () => { |
| 259 | + it('should format number correctly', () => { |
| 260 | + expect(formatNumber(1.25, 'en')).toEqual('1.25'); |
| 261 | + expect(formatNumber(125, 'en')).toEqual('125'); |
| 262 | + expect(formatNumber(1250, 'en')).toEqual('1,250'); |
| 263 | + expect(formatNumber(12500, 'en')).toEqual('12,500'); |
| 264 | + |
| 265 | + expect(formatNumber(1.25, 'bg')).toEqual('1,25'); |
| 266 | + expect(formatNumber(125, 'bg')).toEqual('125'); |
| 267 | + expect(formatNumber(1250, 'bg')).toEqual('1250'); |
| 268 | + expect(formatNumber(12500, 'bg')).toEqual('12 500'); |
| 269 | + |
| 270 | + expect(formatNumber(1.25, 'de')).toEqual('1,25'); |
| 271 | + expect(formatNumber(125, 'de')).toEqual('125'); |
| 272 | + expect(formatNumber(1250, 'de')).toEqual('1.250'); |
| 273 | + expect(formatNumber(12500, 'de')).toEqual('12.500'); |
| 274 | + }); |
| 275 | + |
| 276 | + it('should format percent correctly', () => { |
| 277 | + expect(formatPercent(1.25, 'en')).toEqual('125%'); |
| 278 | + expect(formatPercent(125, 'en')).toEqual('12,500%'); |
| 279 | + expect(formatPercent(1.25, 'bg')).toEqual('125%'); |
| 280 | + expect(formatPercent(125, 'bg')).toEqual('12 500%'); |
| 281 | + }); |
| 282 | + |
| 283 | + it('should format currency correctly', () => { |
| 284 | + expect(formatCurrency(12345, 'en', 'symbol', 'EUR')).toEqual('€12,345.00'); |
| 285 | + expect(formatCurrency(12345, 'en', 'symbol', 'EUR', '1.0-3')).toEqual('€12,345'); |
| 286 | + expect(formatCurrency('12345.33', 'en', 'symbol', 'EUR', '1.0-3')).toEqual('€12,345.33'); |
| 287 | + expect(formatCurrency(12345, 'en', 'symbol', 'EUR', '1.1-3')).toEqual('€12,345.0'); |
| 288 | + expect(formatCurrency('12345', 'en', 'symbol', 'EUR', '1.1-3')).toEqual('€12,345.0'); |
| 289 | + }); |
| 290 | + }) |
| 291 | + |
| 292 | + describe('date formatting', () => { |
| 293 | + it('should format string to dateTime', () => { |
| 294 | + expect(formatDate('2025-01-25T14:15:00', 'short', 'en-US')).toEqual('1/25/25, 2:15 PM'); |
| 295 | + expect(formatDate('2025-01-25T14:15:00', 'medium', 'en-US')).toEqual('Jan 25, 2025, 2:15:00 PM'); |
| 296 | + expect(formatDate('2025-01-25T14:15:00', 'long', 'en-US')).toEqual('January 25, 2025 at 2:15:00 PM GMT+2'); |
| 297 | + expect(formatDate('2025-01-25T14:15:00', 'full', 'en-US')).toEqual('Saturday, January 25, 2025 at 2:15:00 PM Eastern European Standard Time'); |
| 298 | + }); |
| 299 | + |
| 300 | + it('should format string to date', () => { |
| 301 | + expect(formatDate('2025-01-25T14:15:00', 'shortDate', 'en-US')).toEqual('1/25/25'); |
| 302 | + expect(formatDate('2025-01-25T14:15:00', 'mediumDate', 'en-US')).toEqual('Jan 25, 2025'); |
| 303 | + expect(formatDate('2025-01-25T14:15:00', 'longDate', 'en-US')).toEqual('January 25, 2025'); |
| 304 | + expect(formatDate('2025-01-25T14:15:00', 'fullDate', 'en-US')).toEqual('Saturday, January 25, 2025'); |
| 305 | + }); |
| 306 | + |
| 307 | + it('should format string to time', () => { |
| 308 | + expect(formatDate('2025-01-25T14:15:00', 'shortTime', 'en-US')).toEqual('2:15 PM'); |
| 309 | + expect(formatDate('2025-01-25T14:15:00', 'mediumTime', 'en-US')).toEqual('2:15:00 PM'); |
| 310 | + expect(formatDate('2025-01-25T14:15:00', 'longTime', 'en-US')).toEqual('2:15:00 PM GMT+2'); |
| 311 | + expect(formatDate('2025-01-25T14:15:00', 'fullTime', 'en-US')).toEqual('2:15:00 PM Eastern European Standard Time'); |
| 312 | + }); |
| 313 | + |
| 314 | + it('should format string to custom format', () => { |
| 315 | + expect(formatDate('2025-01-25T14:15:00', 'ex: hh:mm bbb GGG', 'en-US')).toEqual('ex: 02:15 in th. af. AD'); |
| 316 | + expect(formatDate('2025-01-25T14:15:00', 'ex: HH:mm bbb GGG', 'en-US')).toEqual('ex: 14:15 in th. af. AD'); |
| 317 | + }); |
| 318 | + |
| 319 | + it('should return correct date format per locale', () => { |
| 320 | + // Defaults to Angular's one because they are registered in tests |
| 321 | + expect(getLocaleDateFormat('en', 'short')).toEqual('M/d/yy'); |
| 322 | + expect(getLocaleDateFormat('en', 'medium')).toEqual('MMM d, y'); |
| 323 | + expect(getLocaleDateFormat('en', 'long')).toEqual('MMMM d, y'); |
| 324 | + expect(getLocaleDateFormat('en', 'full')).toEqual('EEEE, MMMM d, y'); |
| 325 | + |
| 326 | + expect(getLocaleDateFormat('de', 'short')).toEqual('dd.MM.yy'); |
| 327 | + expect(getLocaleDateFormat('de', 'medium')).toEqual('dd.MM.y'); |
| 328 | + expect(getLocaleDateFormat('de', 'long')).toEqual('d. MMMM y'); |
| 329 | + expect(getLocaleDateFormat('de', 'full')).toEqual('EEEE, d. MMMM y'); |
| 330 | + |
| 331 | + // There's no registered locale for IT in tests, so use new API |
| 332 | + expect(getLocaleDateFormat('it', 'short')).toEqual('dd/MM/yy'); |
| 333 | + expect(getLocaleDateFormat('it', 'medium')).toEqual('d MMM yyyy'); |
| 334 | + expect(getLocaleDateFormat('it', 'long')).toEqual(`d MMMM yyyy`); |
| 335 | + expect(getLocaleDateFormat('it', 'full')).toEqual(`EEEE d MMMM yyyy`); |
| 336 | + }); |
| 337 | + |
| 338 | + it('should return correct datetime format per locale', () => { |
| 339 | + // Defaults to Angular's one because they are registered in tests |
| 340 | + expect(getLocaleDateTimeFormat('en', 'short')).toEqual('{1}, {0}'); |
| 341 | + expect(getLocaleDateTimeFormat('en', 'medium')).toEqual('{1}, {0}'); |
| 342 | + expect(getLocaleDateTimeFormat('en', 'long')).toEqual(`{1} 'at' {0}`); |
| 343 | + expect(getLocaleDateTimeFormat('en', 'full')).toEqual(`{1} 'at' {0}`); |
| 344 | + |
| 345 | + expect(getLocaleDateTimeFormat('de', 'short')).toEqual('{1}, {0}'); |
| 346 | + expect(getLocaleDateTimeFormat('de', 'medium')).toEqual('{1}, {0}'); |
| 347 | + expect(getLocaleDateTimeFormat('de', 'long')).toEqual(`{1} 'um' {0}`); |
| 348 | + expect(getLocaleDateTimeFormat('de', 'full')).toEqual(`{1} 'um' {0}`); |
| 349 | + |
| 350 | + // There's no registered locale for IT in tests, so use new API |
| 351 | + expect(getLocaleDateTimeFormat('it', 'short')).toEqual('dd/MM/yy, HH:mm'); |
| 352 | + expect(getLocaleDateTimeFormat('it', 'medium')).toEqual('d MMM yyyy, HH:mm:ss'); |
| 353 | + expect(getLocaleDateTimeFormat('it', 'long')).toEqual(`d MMMM yyyy alle ore HH:mm:ss z`); |
| 354 | + expect(getLocaleDateTimeFormat('it', 'full')).toEqual(`EEEE d MMMM yyyy alle ore HH:mm:ss zzzz`); |
| 355 | + }); |
| 356 | + }); |
| 357 | + |
| 358 | + describe('other', () => { |
| 359 | + it('getCurrencyCode should return default USD as currency code for locale, if no Angular is defined', () => { |
| 360 | + expect(getCurrencyCode('en-US')).toEqual('USD'); |
| 361 | + |
| 362 | + // Registered in tests, that's why they are available |
| 363 | + expect(getCurrencyCode('bg')).toEqual('BGN'); |
| 364 | + expect(getCurrencyCode('de')).toEqual('EUR'); |
| 365 | + |
| 366 | + // This is not registered in tests yet |
| 367 | + expect(getCurrencyCode('it')).toEqual('USD'); |
| 368 | + }); |
| 369 | + |
| 370 | + it('getCurrencySymbol should return correct currency symbol', () => { |
| 371 | + expect(getCurrencySymbol('USD', 'en-US')).toEqual('$'); |
| 372 | + expect(getCurrencySymbol('BGN', 'bg')).toEqual('лв.'); |
| 373 | + expect(getCurrencySymbol('EUR', 'de')).toEqual('€'); |
| 374 | + expect(getCurrencySymbol('EUR', 'it')).toEqual('€'); |
| 375 | + }); |
| 376 | + |
| 377 | + it('getLocaleFirstDayOfWeek should return correct values per locale', () => { |
| 378 | + expect(getLocaleFirstDayOfWeek('en-US')).toEqual(0); // This is Angular's default |
| 379 | + expect(getLocaleFirstDayOfWeek('bg')).toEqual(1); |
| 380 | + expect(getLocaleFirstDayOfWeek('de')).toEqual(1); |
| 381 | + expect(getLocaleFirstDayOfWeek('it')).toEqual(1); |
| 382 | + }); |
| 383 | + }); |
| 384 | + }); |
256 | 385 | }); |
0 commit comments