1
1
import 'reflect-metadata' ;
2
2
import { container } from 'tsyringe' ;
3
+ import { expectLoggerErrorToBeCalled } from '@test/utils' ;
4
+ import { ILocale } from '../localization' ;
3
5
import { LocaleService } from '..' ;
4
- import { BuiltInLocales , BuiltInDefault , ILocale } from '../localization' ;
5
6
6
7
describe ( 'The Locale Service' , ( ) => {
7
- const TestLocale = {
8
+ const TestLocale : ILocale = {
8
9
id : 'test' ,
9
10
source : new Map ( ) ,
10
11
name : 'test' ,
11
12
} ;
12
13
14
+ // LocaleService is support to add an object source but ILocale is banned
15
+ const TestEnLocale : ILocale = {
16
+ id : 'en' ,
17
+ name : 'English' ,
18
+ source : {
19
+ // @ts -ignore
20
+ 'molecule.welcome' : 'Welcome to Molecule' ,
21
+ 'test.id' : 'hello ${i}' ,
22
+ } ,
23
+ } ;
24
+
13
25
afterEach ( ( ) => {
14
26
localStorage . clear ( ) ;
15
27
} ) ;
@@ -19,82 +31,72 @@ describe('The Locale Service', () => {
19
31
expect ( localeService ) . not . toBeUndefined ( ) ;
20
32
} ) ;
21
33
22
- test ( 'Reset the LocaleService ' , ( ) => {
34
+ test ( 'Initialize the locales with testLocale ' , ( ) => {
23
35
const localeService = new LocaleService ( ) ;
24
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toBe ( BuiltInDefault . id ) ;
25
- localeService . reset ( ) ;
26
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toBe ( BuiltInDefault . id ) ;
27
- } ) ;
36
+ localeService . initialize ( [ TestLocale ] , TestLocale . id ) ;
28
37
29
- test ( 'Get default Locale' , ( ) => {
30
- const localeService = new LocaleService ( ) ;
31
- const defaultLocale = localeService . getDefaultLocale ( ) ;
32
- expect ( defaultLocale ) . toEqual ( BuiltInDefault ) ;
33
- } ) ;
38
+ expect ( localeService . getCurrentLocale ( ) ?. id ) . toEqual ( TestLocale . id ) ;
39
+ expect ( localeService . getLocales ( ) . length ) . toBe ( 1 ) ;
34
40
35
- test ( 'Get default Locales' , ( ) => {
36
- const localeService = new LocaleService ( ) ;
37
- const defaultLocale = localeService . getDefaultLocales ( ) ;
38
- expect ( defaultLocale ) . toEqual ( BuiltInLocales ) ;
41
+ localeService . reset ( ) ;
42
+ expectLoggerErrorToBeCalled ( ( ) => {
43
+ // @ts -ignore
44
+ localeService . initialize ( [ TestEnLocale , TestLocale ] ) ;
45
+ } ) ;
39
46
} ) ;
40
47
41
- test ( 'The size of Built-in Locales should be 3 ' , ( ) => {
48
+ test ( 'Reset the LocaleService ' , ( ) => {
42
49
const localeService = new LocaleService ( ) ;
43
- const locales = localeService . getLocales ( ) ;
44
- expect ( locales . length ) . toBe ( 3 ) ;
45
- } ) ;
50
+ expect ( localeService . getCurrentLocale ( ) ) . toBeUndefined ( ) ;
46
51
47
- test ( 'Initialize the locales' , ( ) => {
48
- const localeService = new LocaleService ( ) ;
49
- localeService . initialize ( [ TestLocale ] ) ;
50
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual (
51
- localeService . getDefaultLocale ( ) . id
52
- ) ;
53
- expect ( localeService . getLocales ( ) . length ) . toBe ( 4 ) ;
54
- localeService . initialize ( [ ] , 'test' ) ;
55
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( BuiltInDefault . id ) ;
56
- // Clear the cached locale value
57
- localStorage . clear ( ) ;
58
- localeService . initialize ( [ ] , 'test' ) ;
59
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( 'test' ) ;
60
- localeService . initialize ( [ ] ) ;
61
- // Get from the localStorage cache
62
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( 'test' ) ;
52
+ localeService . initialize ( [ TestLocale ] , TestLocale . id ) ;
53
+ expect ( localeService . getCurrentLocale ( ) ) . toEqual ( TestLocale ) ;
54
+
55
+ localeService . reset ( ) ;
56
+ expect ( localeService . getCurrentLocale ( ) ) . toBeUndefined ( ) ;
63
57
} ) ;
64
58
65
59
test ( 'Get/Set current locale' , ( ) => {
66
60
const localeService = new LocaleService ( ) ;
67
- ( localeService as any ) . _current = undefined ;
68
- expect ( localeService . getCurrentLocale ( ) ) . toBe ( BuiltInDefault ) ;
69
- localeService . addLocales ( [ TestLocale ] ) ;
70
- localeService . setCurrentLocale ( TestLocale . id ) ;
71
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( TestLocale . id ) ;
61
+ expect ( localeService . getCurrentLocale ( ) ) . toBeUndefined ( ) ;
62
+
63
+ localeService . initialize ( [ TestLocale , TestEnLocale ] , TestLocale . id ) ;
64
+
65
+ expect ( localeService . getCurrentLocale ( ) ? .id ) . toEqual ( TestLocale . id ) ;
72
66
67
+ localeService . setCurrentLocale ( TestEnLocale . id ) ;
68
+ expect ( localeService . getCurrentLocale ( ) ?. id ) . toEqual ( TestEnLocale . id ) ;
69
+ // set an unknow locale will fail
73
70
expect ( localeService . setCurrentLocale ( 'unknown' ) ) . toEqual ( false ) ;
74
71
} ) ;
75
72
76
73
test ( 'Add locales' , ( ) => {
77
74
const localeService = new LocaleService ( ) ;
78
- expect ( localeService . getLocales ( ) . length ) . toBe ( 3 ) ;
75
+ expect ( localeService . getLocales ( ) . length ) . toBe ( 0 ) ;
76
+
79
77
localeService . addLocales ( [ TestLocale ] ) ;
80
- expect ( localeService . getLocales ( ) . length ) . toBe ( 4 ) ;
78
+ expect ( localeService . getLocales ( ) . length ) . toBe ( 1 ) ;
79
+
81
80
localeService . addLocales ( [ ] ) ;
82
- expect ( localeService . getLocales ( ) . length ) . toBe ( 4 ) ;
81
+ expect ( localeService . getLocales ( ) . length ) . toBe ( 1 ) ;
82
+
83
83
// Add an existed locale
84
84
localeService . addLocales ( [ TestLocale ] ) ;
85
- expect ( localeService . getLocales ( ) . length ) . toBe ( 4 ) ;
85
+ expect ( localeService . getLocales ( ) . length ) . toBe ( 1 ) ;
86
86
} ) ;
87
87
88
88
test ( 'Add an locale inherit the en' , ( ) => {
89
89
const localeService = new LocaleService ( ) ;
90
+ localeService . initialize ( [ TestEnLocale ] , TestEnLocale . id ) ;
91
+
90
92
expect ( TestLocale . source . size ) . toBe ( 0 ) ;
91
- ( TestLocale as ILocale ) . inherit = 'en' ;
93
+ TestLocale . inherit = 'en' ;
92
94
localeService . addLocales ( [ TestLocale ] ) ;
93
95
expect ( localeService . getLocale ( TestLocale . id ) ?. source . size ) . not . toBe ( 0 ) ;
94
96
95
97
// Inherit an not exist locale
96
98
localeService . removeLocale ( TestLocale . id ) ;
97
- ( TestLocale as ILocale ) . inherit = 'unknown' ;
99
+ TestLocale . inherit = 'unknown' ;
98
100
localeService . addLocales ( [ TestLocale ] ) ;
99
101
expect ( localeService . getLocale ( TestLocale . id ) ?. source . size ) . toBe ( 0 ) ;
100
102
} ) ;
@@ -110,38 +112,47 @@ describe('The Locale Service', () => {
110
112
111
113
test ( 'Remove a locale' , ( ) => {
112
114
const localeService = new LocaleService ( ) ;
113
- localeService . addLocales ( [ TestLocale ] ) ;
115
+ localeService . initialize ( [ TestLocale , TestEnLocale ] , TestLocale . id ) ;
114
116
expect ( localeService . getLocale ( TestLocale . id ) ?. id ) . toEqual (
115
117
TestLocale . id
116
118
) ;
117
- localeService . removeLocale ( TestLocale . id ) ;
119
+
120
+ const removedLocale = localeService . removeLocale ( TestLocale . id ) ;
118
121
expect ( localeService . getLocale ( TestLocale . id ) ) . toBeUndefined ( ) ;
122
+ expect ( removedLocale ) . toEqual ( TestLocale ) ;
123
+
119
124
localeService . addLocales ( [ TestLocale ] ) ;
120
125
localeService . setCurrentLocale ( TestLocale . id ) ;
121
126
122
127
//Remove the current locale
123
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( TestLocale . id ) ;
128
+ expect ( localeService . getCurrentLocale ( ) ? .id ) . toEqual ( TestLocale . id ) ;
124
129
localeService . removeLocale ( TestLocale . id ) ;
125
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual (
126
- localeService . getDefaultLocale ( ) . id
127
- ) ;
130
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( TestEnLocale . id ) ;
128
131
129
132
// Remove an undefined
130
133
expect ( localeService . removeLocale ( TestLocale . id ) ) ;
134
+
135
+ expect ( localeService . getLocales ( ) . length ) . toBe ( 1 ) ;
136
+ // The last one couldn't be removed
137
+ expect ( localeService . removeLocale ( TestEnLocale . id ) ) . toBeFalsy ( ) ;
131
138
} ) ;
132
139
133
140
test ( 'Listen to the current locale change event' , ( ) => {
134
- const target = 'zh-CN' ;
135
141
const localeService = new LocaleService ( ) ;
136
142
const fn = jest . fn ( ) ;
137
143
localeService . onChange ( fn ) ;
138
- localeService . setCurrentLocale ( target ) ;
144
+
145
+ localeService . initialize ( [ TestLocale , TestEnLocale ] , TestLocale . id ) ;
146
+ localeService . setCurrentLocale ( TestEnLocale . id ) ;
147
+
139
148
expect ( fn ) . toBeCalledTimes ( 1 ) ;
140
- expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( target ) ;
149
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( TestEnLocale . id ) ;
141
150
} ) ;
142
151
143
152
test ( 'Localize the source key' , ( ) => {
144
153
const localeService = new LocaleService ( ) ;
154
+
155
+ localeService . initialize ( [ TestLocale , TestEnLocale ] , TestEnLocale . id ) ;
145
156
let res = localeService . localize ( 'test' ) ;
146
157
expect ( res ) . toEqual ( '' ) ;
147
158
@@ -154,19 +165,10 @@ describe('The Locale Service', () => {
154
165
res = localeService . localize ( 'molecule.welcome' , 'default' ) ;
155
166
expect ( res ) . toEqual ( 'Welcome to Molecule' ) ;
156
167
157
- const map = new Map ( ) ;
158
- map . set ( 'test.id' , 'hello ${i}' ) ;
159
- const mockData = {
160
- id : 'mock' ,
161
- name : 'mock' ,
162
- source : map ,
163
- } ;
164
- localeService . addLocales ( [ mockData ] ) ;
165
- localeService . setCurrentLocale ( mockData . id ) ;
166
168
res = localeService . localize ( 'test.id' , '' , 'world' ) ;
167
169
expect ( res ) . toEqual ( 'hello world' ) ;
168
170
169
- ( localeService as any ) . _current = null ;
171
+ localeService . setCurrentLocale ( TestLocale . id ) ;
170
172
res = localeService . localize ( 'molecule.welcome' , 'default' ) ;
171
173
expect ( res ) . toEqual ( 'default' ) ;
172
174
} ) ;
0 commit comments