angular-cn/packages/common/test/i18n/locale_data_api_spec.ts
Pete Bacon Darwin 7e8eec57f0 refactor(common): move the low level locale registering to core (#33523)
To limit the exposure of the private `LOCALE_DATA` from outside
`@angular/core` this commit exposes private functions in the core
to hide the internal structures better.

* The `registerLocaleData()` implementation has moved from
`@angular/common` to `@angular/core`. A stub that delegates to
core has been left in common for backward compatibility.
* A new `ɵunregisterLocaleData()` function has been provided,
which is particularly useful in tests to clear out registered locales
to prevent subsequent tests from being affected.
* A private export of `ɵregisterLocaleData()` has been removed
from `@angular/common`. This was not being used and is accessible
via `@angular/core` anyway.

PR Close #33523
2019-11-05 17:26:59 +00:00

61 lines
2.5 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ɵregisterLocaleData, ɵunregisterLocaleData} from '@angular/core';
import localeEn from '@angular/common/locales/en';
import localeFr from '@angular/common/locales/fr';
import localeZh from '@angular/common/locales/zh';
import localeEnAU from '@angular/common/locales/en-AU';
import {getCurrencySymbol, getLocaleDateFormat, FormatWidth, getNumberOfCurrencyDigits} from '../../src/i18n/locale_data_api';
{
describe('locale data api', () => {
beforeAll(() => {
ɵregisterLocaleData(localeEn);
ɵregisterLocaleData(localeFr);
ɵregisterLocaleData(localeZh);
ɵregisterLocaleData(localeEnAU);
});
afterAll(() => { ɵunregisterLocaleData(); });
describe('getting currency symbol', () => {
it('should return the correct symbol', () => {
expect(getCurrencySymbol('USD', 'wide')).toEqual('$');
expect(getCurrencySymbol('USD', 'narrow')).toEqual('$');
expect(getCurrencySymbol('AUD', 'wide')).toEqual('A$');
expect(getCurrencySymbol('AUD', 'narrow')).toEqual('$');
expect(getCurrencySymbol('CRC', 'wide')).toEqual('CRC');
expect(getCurrencySymbol('CRC', 'narrow')).toEqual('₡');
expect(getCurrencySymbol('unexisting_ISO_code', 'wide')).toEqual('unexisting_ISO_code');
expect(getCurrencySymbol('unexisting_ISO_code', 'narrow')).toEqual('unexisting_ISO_code');
expect(getCurrencySymbol('USD', 'wide', 'en-AU')).toEqual('USD');
expect(getCurrencySymbol('USD', 'narrow', 'en-AU')).toEqual('$');
expect(getCurrencySymbol('AUD', 'wide', 'en-AU')).toEqual('$');
expect(getCurrencySymbol('AUD', 'narrow', 'en-AU')).toEqual('$');
expect(getCurrencySymbol('USD', 'wide', 'fr')).toEqual('$US');
});
});
describe('getNbOfCurrencyDigits', () => {
it('should return the correct value', () => {
expect(getNumberOfCurrencyDigits('USD')).toEqual(2);
expect(getNumberOfCurrencyDigits('IDR')).toEqual(0);
expect(getNumberOfCurrencyDigits('BHD')).toEqual(3);
expect(getNumberOfCurrencyDigits('unexisting_ISO_code')).toEqual(2);
});
});
describe('getLastDefinedValue', () => {
it('should find the last defined date format when format not defined',
() => { expect(getLocaleDateFormat('zh', FormatWidth.Long)).toEqual('y年M月d日'); });
});
});
}