2016-08-12 19:52:55 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-08-12 19:52:55 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-12-14 05:39:41 -05:00
|
|
|
import {DEFAULT_CURRENCY_CODE, LOCALE_ID} from '@angular/core';
|
2019-10-23 17:49:02 -04:00
|
|
|
import {ivyEnabled} from '@angular/private/testing';
|
|
|
|
|
|
|
|
import {getLocaleId} from '../src/render3';
|
|
|
|
import {global} from '../src/util/global';
|
|
|
|
import {TestBed} from '../testing';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {describe, expect, inject, it} from '../testing/src/testing_internal';
|
2016-08-12 19:52:55 -04:00
|
|
|
|
2017-12-15 19:28:41 -05:00
|
|
|
{
|
2016-08-12 19:52:55 -04:00
|
|
|
describe('Application module', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should set the default locale to "en-US"', inject([LOCALE_ID], (defaultLocale: string) => {
|
|
|
|
expect(defaultLocale).toEqual('en-US');
|
|
|
|
}));
|
2019-10-23 17:49:02 -04:00
|
|
|
|
2019-12-14 05:39:41 -05:00
|
|
|
it('should set the default currency code to "USD"',
|
|
|
|
inject([DEFAULT_CURRENCY_CODE], (defaultCurrencyCode: string) => {
|
|
|
|
expect(defaultCurrencyCode).toEqual('USD');
|
|
|
|
}));
|
|
|
|
|
2019-10-23 17:49:02 -04:00
|
|
|
if (ivyEnabled) {
|
|
|
|
it('should set the ivy locale with the configured LOCALE_ID', () => {
|
|
|
|
TestBed.configureTestingModule({providers: [{provide: LOCALE_ID, useValue: 'fr'}]});
|
|
|
|
const before = getLocaleId();
|
|
|
|
const locale = TestBed.inject(LOCALE_ID);
|
|
|
|
const after = getLocaleId();
|
|
|
|
expect(before).toEqual('en-us');
|
|
|
|
expect(locale).toEqual('fr');
|
|
|
|
expect(after).toEqual('fr');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('$localize.locale', () => {
|
|
|
|
beforeEach(() => initLocale('de'));
|
|
|
|
afterEach(() => restoreLocale());
|
|
|
|
|
|
|
|
it('should set the ivy locale to `$localize.locale` value if it is defined', () => {
|
|
|
|
// Injecting `LOCALE_ID` should also initialize the ivy locale
|
|
|
|
const locale = TestBed.inject(LOCALE_ID);
|
|
|
|
expect(locale).toEqual('de');
|
|
|
|
expect(getLocaleId()).toEqual('de');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set the ivy locale to an application provided LOCALE_ID even if `$localize.locale` is defined',
|
|
|
|
() => {
|
|
|
|
TestBed.configureTestingModule({providers: [{provide: LOCALE_ID, useValue: 'fr'}]});
|
|
|
|
const locale = TestBed.inject(LOCALE_ID);
|
|
|
|
expect(locale).toEqual('fr');
|
|
|
|
expect(getLocaleId()).toEqual('fr');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-08-12 19:52:55 -04:00
|
|
|
});
|
2017-01-05 12:24:37 -05:00
|
|
|
}
|
2019-10-23 17:49:02 -04:00
|
|
|
|
|
|
|
let hasGlobalLocalize: boolean;
|
|
|
|
let hasGlobalLocale: boolean;
|
|
|
|
let originalLocale: string;
|
|
|
|
|
|
|
|
function initLocale(locale: string) {
|
|
|
|
hasGlobalLocalize = Object.getOwnPropertyNames(global).includes('$localize');
|
|
|
|
if (!hasGlobalLocalize) {
|
|
|
|
global.$localize = {};
|
|
|
|
}
|
|
|
|
hasGlobalLocale = Object.getOwnPropertyNames(global.$localize).includes('locale');
|
|
|
|
originalLocale = global.$localize.locale;
|
|
|
|
global.$localize.locale = locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreLocale() {
|
|
|
|
if (hasGlobalLocale) {
|
|
|
|
global.$localize.locale = originalLocale;
|
|
|
|
} else {
|
|
|
|
delete global.$localize.locale;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasGlobalLocalize) {
|
|
|
|
delete global.$localize;
|
|
|
|
}
|
|
|
|
}
|