/
locale_data_api_spec.ts
85 lines (72 loc) 路 3.42 KB
/
locale_data_api_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @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 {findLocaleData, registerLocaleData, unregisterAllLocaleData} from '../../src/i18n/locale_data_api';
import {global} from '../../src/util/global';
{
describe('locale data api', () => {
const localeCaESVALENCIA: any[] = ['ca-ES-VALENCIA'];
const localeDe: any[] = ['de'];
const localeDeCH: any[] = ['de-CH'];
const localeEn: any[] = ['en'];
const localeFr: any[] = ['fr'];
const localeFrCA: any[] = ['fr-CA'];
const localeZh: any[] = ['zh'];
const localeEnAU: any[] = ['en-AU'];
const fakeGlobalFr: any[] = ['fr'];
beforeAll(() => {
// Sumulate manually registering some locale data
registerLocaleData(localeCaESVALENCIA);
registerLocaleData(localeEn);
registerLocaleData(localeFr);
registerLocaleData(localeFrCA);
registerLocaleData(localeFr, 'fake-id');
registerLocaleData(localeFrCA, 'fake_Id2');
registerLocaleData(localeZh);
registerLocaleData(localeEnAU);
// Simulate some locale data existing on the global already
global.ng = global.ng || {};
global.ng.common = global.ng.common || {locales: {}};
global.ng.common.locales = global.ng.common.locales || {};
global.ng.common.locales['fr'] = fakeGlobalFr;
global.ng.common.locales['de'] = localeDe;
global.ng.common.locales['de-ch'] = localeDeCH;
});
afterAll(() => {
unregisterAllLocaleData();
global.ng.common.locales = undefined;
});
describe('findLocaleData', () => {
it('should throw if the LOCALE_DATA for the chosen locale or its parent locale is not available',
() => {
expect(() => findLocaleData('pt-AO'))
.toThrowError(/Missing locale data for the locale "pt-AO"/);
});
it('should return english data if the locale is en-US',
() => { expect(findLocaleData('en-US')).toEqual(localeEn); });
it('should return the exact LOCALE_DATA if it is available',
() => { expect(findLocaleData('fr-CA')).toEqual(localeFrCA); });
it('should return the parent LOCALE_DATA if it exists and exact locale is not available',
() => { expect(findLocaleData('fr-BE')).toEqual(localeFr); });
it(`should find the LOCALE_DATA even if the locale id is badly formatted`, () => {
expect(findLocaleData('ca-ES-VALENCIA')).toEqual(localeCaESVALENCIA);
expect(findLocaleData('CA_es_Valencia')).toEqual(localeCaESVALENCIA);
});
it(`should find the LOCALE_DATA if the locale id was registered`, () => {
expect(findLocaleData('fake-id')).toEqual(localeFr);
expect(findLocaleData('fake_iD')).toEqual(localeFr);
expect(findLocaleData('fake-id2')).toEqual(localeFrCA);
});
it('should find the exact LOCALE_DATA if the locale is on the global object',
() => { expect(findLocaleData('de-CH')).toEqual(localeDeCH); });
it('should find the parent LOCALE_DATA if the exact locale is not available and the parent locale is on the global object',
() => { expect(findLocaleData('de-BE')).toEqual(localeDe); });
it('should find the registered LOCALE_DATA even if the same locale is on the global object',
() => { expect(findLocaleData('fr')).not.toBe(fakeGlobalFr); });
});
});
}