From 5926bf452dddfe8b7fe18c4ba680b0e61d2e3d12 Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Thu, 25 Apr 2024 15:17:29 +0200 Subject: [PATCH] refactor(common): increment test coverage (#55532) Ahead of #55283 and with what we learned from it, let's improve our test coverage on the i18n subsystem. PR Close #55532 --- packages/common/test/i18n/format_date_spec.ts | 35 ++++++++++++++++--- .../common/test/i18n/format_number_spec.ts | 22 ++++++++++++ .../common/test/i18n/locale_data_api_spec.ts | 2 +- packages/common/test/pipes/date_pipe_spec.ts | 12 +++++++ .../common/test/pipes/number_pipe_spec.ts | 11 ++++++ 5 files changed, 76 insertions(+), 6 deletions(-) diff --git a/packages/common/test/i18n/format_date_spec.ts b/packages/common/test/i18n/format_date_spec.ts index c8a3f81be6a..0ff6dd0b891 100644 --- a/packages/common/test/i18n/format_date_spec.ts +++ b/packages/common/test/i18n/format_date_spec.ts @@ -73,7 +73,7 @@ describe('Format date', () => { const isoStringWithoutTimeOrDate = '2015-01'; const isoStringWithoutTimeOrDateOrMonth = '2015'; const defaultFormat = 'mediumDate'; - let date: Date; + const date = new Date(2015, 5, 15, 9, 3, 1, 550); // Check the transformation of a date into a pattern function expectDateFormatAs(date: Date | string, pattern: any, output: string): void { @@ -95,10 +95,6 @@ describe('Format date', () => { afterAll(() => ɵunregisterLocaleData()); - beforeEach(() => { - date = new Date(2015, 5, 15, 9, 3, 1, 550); - }); - it('should format each component correctly', () => { const dateFixtures: any = { G: 'AD', @@ -280,6 +276,12 @@ describe('Format date', () => { }); }); + it('should support non-normalized locales', () => { + expect(formatDate(date, 'short', 'de-DE')).toEqual('15.06.15, 09:03'); + expect(formatDate(date, 'short', 'de_DE')).toEqual('15.06.15, 09:03'); + expect(formatDate(date, 'short', 'de-de')).toEqual('15.06.15, 09:03'); + }); + it('should format with timezones', () => { const dateFixtures: any = { z: /GMT(\+|-)\d/, @@ -490,6 +492,29 @@ describe('Format date', () => { expect(formatDate(date, 'fullDate', 'fi')).toMatch('maanantai 15. kesäkuuta 2015'); }); + it('should wrap negative years', () => { + const date = new Date(new Date('2024-01-13').setFullYear(-1)); // Year -1 + expect(formatDate(date, 'yyyy', ɵDEFAULT_LOCALE_ID)).toEqual('0002'); + }); + + it('should support years < 1000', () => { + expect(formatDate(new Date('0054-02-18'), 'yyy', ɵDEFAULT_LOCALE_ID)).toEqual('054'); + expect(formatDate(new Date('0054-02-18'), 'yyyy', ɵDEFAULT_LOCALE_ID)).toEqual('0054'); + expect(formatDate(new Date('0803-02-18'), 'yyyy', ɵDEFAULT_LOCALE_ID)).toEqual('0803'); + }); + + it('should support timezones', () => { + const isoDate = '2024-02-17T12:00:00Z'; + + const date1 = formatDate(isoDate, 'long', 'en', 'America/New_York'); + const date2 = formatDate(isoDate, 'long', 'en', 'EST'); + expect(date1).toBe('February 17, 2024 at 12:00:00 PM GMT+0'); + expect(date2).toBe('February 17, 2024 at 7:00:00 AM GMT-5'); + + const date3 = formatDate(isoDate, 'long', 'en', '+0500'); + expect(date3).toBe('February 17, 2024 at 5:00:00 PM GMT+5'); + }); + it('should return thursday date of the same week', () => { // Dec. 31st is a Sunday, last day of the last week of 2023 expect(getThursdayThisIsoWeek(new Date('2023-12-31'))).toEqual(new Date('2023-12-28')); diff --git a/packages/common/test/i18n/format_number_spec.ts b/packages/common/test/i18n/format_number_spec.ts index 9023c25b1ae..9946e6a6480 100644 --- a/packages/common/test/i18n/format_number_spec.ts +++ b/packages/common/test/i18n/format_number_spec.ts @@ -34,6 +34,8 @@ describe('Format number', () => { expect(formatNumber(1.1234, ɵDEFAULT_LOCALE_ID)).toEqual('1.123'); expect(formatNumber(1.123456, ɵDEFAULT_LOCALE_ID, '.2')).toEqual('1.123'); expect(formatNumber(1.123456, ɵDEFAULT_LOCALE_ID, '.4')).toEqual('1.1235'); + + expect(formatNumber(1e100, ɵDEFAULT_LOCALE_ID)).toEqual('1E+100'); }); it('should throw if minFractionDigits is explicitly higher than maxFractionDigits', () => { @@ -47,6 +49,12 @@ describe('Format number', () => { it('should return the correct format for es-US', () => { expect(formatNumber(9999999.99, 'es-US', '1.2-2')).toEqual('9,999,999.99'); }); + + it('should support non-normalized locales', () => { + expect(formatNumber(12345, 'en-US')).toEqual('12,345'); + expect(formatNumber(12345, 'en_US')).toEqual('12,345'); + expect(formatNumber(12345, 'en_us')).toEqual('12,345'); + }); }); }); @@ -56,6 +64,8 @@ describe('Format number', () => { expect(formatPercent(1.23, ɵDEFAULT_LOCALE_ID)).toEqual('123%'); expect(formatPercent(1.2, ɵDEFAULT_LOCALE_ID, '.2')).toEqual('120.00%'); expect(formatPercent(1.2, ɵDEFAULT_LOCALE_ID, '4.2')).toEqual('0,120.00%'); + expect(formatPercent(0, ɵDEFAULT_LOCALE_ID)).toEqual('0%'); + expect(formatPercent(-0, ɵDEFAULT_LOCALE_ID)).toEqual('0%'); expect(formatPercent(1.2, 'fr', '4.2')).toEqual('0\u202f120,00 %'); expect(formatPercent(1.2, 'ar', '4.2')).toEqual('0,120.00‎%‎'); // see issue #20136 @@ -75,6 +85,12 @@ describe('Format number', () => { expect(formatPercent(1.5e2, ɵDEFAULT_LOCALE_ID)).toEqual('15,000%'); expect(formatPercent(1e100, ɵDEFAULT_LOCALE_ID)).toEqual('1E+102%'); }); + + it('should support non-normalized locales', () => { + expect(formatPercent(1.23, 'en-US')).toEqual('123%'); + expect(formatPercent(1.23, 'en_US')).toEqual('123%'); + expect(formatPercent(1.23, 'en_us')).toEqual('123%'); + }); }); }); @@ -128,6 +144,12 @@ describe('Format number', () => { expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'BHD', 'BHD')).toEqual('BHD5.123'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'BHD', 'BHD', '.1-2')).toEqual('BHD5.12'); }); + + it('should support non-normalized locales', () => { + expect(formatCurrency(12345, 'en-US', 'USD')).toEqual('USD12,345.00'); + expect(formatCurrency(12345, 'en_US', 'USD')).toEqual('USD12,345.00'); + expect(formatCurrency(12345, 'en_us', 'USD')).toEqual('USD12,345.00'); + }); }); }); }); diff --git a/packages/common/test/i18n/locale_data_api_spec.ts b/packages/common/test/i18n/locale_data_api_spec.ts index e44a0f409a1..2362b8b5e30 100644 --- a/packages/common/test/i18n/locale_data_api_spec.ts +++ b/packages/common/test/i18n/locale_data_api_spec.ts @@ -23,7 +23,7 @@ import { getLocaleMonthNames, getNumberOfCurrencyDigits, TranslationWidth, -} from '../../src/i18n/locale_data_api'; +} from '@angular/common'; describe('locale data api', () => { beforeAll(() => { diff --git a/packages/common/test/pipes/date_pipe_spec.ts b/packages/common/test/pipes/date_pipe_spec.ts index 43dab5c8001..8f2a5eb8739 100644 --- a/packages/common/test/pipes/date_pipe_spec.ts +++ b/packages/common/test/pipes/date_pipe_spec.ts @@ -145,10 +145,22 @@ describe('DatePipe', () => { ); }); + it('should take timezone into account with timezone offset', () => { + expect(pipe.transform('2017-01-11T00:00:00', 'mediumDate', '-1200')).toEqual('Jan 10, 2017'); + }); + + it('should support an empty string for the timezone', () => { + expect(pipe.transform('2017-01-11T00:00:00', 'mediumDate', '')).toEqual('Jan 11, 2017'); + }); + it('should take timezone into account', () => { expect(pipe.transform('2017-01-11T00:00:00', 'mediumDate', '-1200')).toEqual('Jan 10, 2017'); }); + it('should take timezone into account with timezone offset', () => { + expect(pipe.transform('2017-01-11T00:00:00', 'mediumDate', '-1200')).toEqual('Jan 10, 2017'); + }); + it('should take the default timezone into account when no timezone is passed in', () => { pipe = new DatePipe('en-US', '-1200'); expect(pipe.transform('2017-01-11T00:00:00', 'mediumDate')).toEqual('Jan 10, 2017'); diff --git a/packages/common/test/pipes/number_pipe_spec.ts b/packages/common/test/pipes/number_pipe_spec.ts index fb172123c3c..1495cb2d95c 100644 --- a/packages/common/test/pipes/number_pipe_spec.ts +++ b/packages/common/test/pipes/number_pipe_spec.ts @@ -175,6 +175,11 @@ describe('Number pipes', () => { expect(pipe.transform(123456789, 'EUR', 'symbol', '', 'de-at')).toEqual('€ 123.456.789,00'); expect(pipe.transform(5.1234, 'EUR', '', '', 'de-at')).toEqual('5,12'); expect(pipe.transform(5.1234, 'DKK', '', '', 'da')).toEqual('5,12'); + + // CLP doesn't have a subdivision, so it should not display decimals unless explicitly + // told so + expect(pipe.transform(5.1234, 'CLP', '')).toEqual('5'); + expect(pipe.transform(5.1234, 'CLP', '', '2.0-3')).toEqual('05.123'); }); it('should use the injected default currency code if none is provided', () => { @@ -189,6 +194,12 @@ describe('Number pipes', () => { ); // currency code is USD, the pipe will format based on USD but will display "Custom name" expect(pipe.transform(5.1234, 'USD', 'Custom name')).toEqual('Custom name5.12'); + + // currency code is unknown, default formatting options will be used and will display + // "Custom name" + expect(pipe.transform(5.1234, 'unexisting_ISO_code', 'Custom name')).toEqual( + 'Custom name5.12', + ); }); it('should return null for NaN', () => {