Skip to content

Commit

Permalink
refactor(common): increment test coverage (#55532)
Browse files Browse the repository at this point in the history
Ahead of #55283 and with what we learned from it,  let's improve our test coverage on the i18n subsystem.

PR Close #55532
  • Loading branch information
JeanMeche authored and AndrewKushnir committed Apr 25, 2024
1 parent c3d077c commit 75db462
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
35 changes: 30 additions & 5 deletions packages/common/test/i18n/format_date_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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',
Expand Down Expand Up @@ -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/,
Expand Down Expand Up @@ -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'));
Expand Down
22 changes: 22 additions & 0 deletions packages/common/test/i18n/format_number_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});
});

Expand All @@ -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
Expand All @@ -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%');
});
});
});

Expand Down Expand Up @@ -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');
});
});
});
});
2 changes: 1 addition & 1 deletion packages/common/test/i18n/locale_data_api_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getLocaleMonthNames,
getNumberOfCurrencyDigits,
TranslationWidth,
} from '../../src/i18n/locale_data_api';
} from '@angular/common';

describe('locale data api', () => {
beforeAll(() => {
Expand Down
12 changes: 12 additions & 0 deletions packages/common/test/pipes/date_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions packages/common/test/pipes/number_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit 75db462

Please sign in to comment.