@@ -4,63 +4,77 @@ import { LocaleService } from '..';
4
4
import { BuiltInLocales , BuiltInZhCN , ILocale } from '../localization' ;
5
5
6
6
describe ( 'The Locale Service' , ( ) => {
7
- const localeService = container . resolve ( LocaleService ) ;
8
-
9
7
const TestLocale = {
10
8
id : 'test' ,
11
9
source : new Map ( ) ,
12
10
name : 'test' ,
13
11
} ;
14
12
15
13
afterEach ( ( ) => {
14
+ localStorage . clear ( ) ;
15
+ } ) ;
16
+
17
+ test ( 'Instance the LocaleService by IOC' , ( ) => {
18
+ const localeService = container . resolve ( LocaleService ) ;
19
+ expect ( localeService ) . not . toBeUndefined ( ) ;
20
+ } ) ;
21
+
22
+ test ( 'Reset the LocaleService' , ( ) => {
23
+ const localeService = new LocaleService ( ) ;
24
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toBe ( BuiltInZhCN . id ) ;
16
25
localeService . reset ( ) ;
26
+ expect ( localeService . getCurrentLocale ( ) ) . toBeUndefined ( ) ;
17
27
} ) ;
18
28
19
29
test ( 'Get default Locale' , ( ) => {
30
+ const localeService = new LocaleService ( ) ;
20
31
const defaultLocale = localeService . getDefaultLocale ( ) ;
21
32
expect ( defaultLocale ) . toEqual ( BuiltInZhCN ) ;
22
33
} ) ;
23
34
24
35
test ( 'Get default Locales' , ( ) => {
36
+ const localeService = new LocaleService ( ) ;
25
37
const defaultLocale = localeService . getDefaultLocales ( ) ;
26
38
expect ( defaultLocale ) . toEqual ( BuiltInLocales ) ;
27
39
} ) ;
28
40
29
41
test ( 'The size of Built-in Locales should be 2' , ( ) => {
42
+ const localeService = new LocaleService ( ) ;
30
43
const locales = localeService . getLocales ( ) ;
31
44
expect ( locales . length ) . toBe ( 2 ) ;
32
45
} ) ;
33
46
34
47
test ( 'Initialize the locales' , ( ) => {
48
+ const localeService = new LocaleService ( ) ;
35
49
localeService . initialize ( [ TestLocale ] ) ;
36
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual (
50
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual (
37
51
localeService . getDefaultLocale ( ) . id
38
52
) ;
39
53
expect ( localeService . getLocales ( ) . length ) . toBe ( 3 ) ;
40
54
localeService . initialize ( [ ] , 'test' ) ;
41
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual ( BuiltInZhCN . id ) ;
55
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( BuiltInZhCN . id ) ;
42
56
// Clear the cached locale value
43
57
localStorage . clear ( ) ;
44
58
localeService . initialize ( [ ] , 'test' ) ;
45
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual ( 'test' ) ;
59
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( 'test' ) ;
46
60
localeService . initialize ( [ ] ) ;
47
61
// Get from the localStorage cache
48
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual ( 'test' ) ;
62
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( 'test' ) ;
49
63
} ) ;
50
64
51
65
test ( 'Get/Set current locale' , ( ) => {
52
- ( localeService as any ) . _current = null ;
53
- expect ( localeService . getCurrentLocale ( ) ) . toEqual (
54
- localeService . getDefaultLocale ( )
55
- ) ;
66
+ const localeService = new LocaleService ( ) ;
67
+ ( localeService as any ) . _current = undefined ;
68
+ expect ( localeService . getCurrentLocale ( ) ) . toBeUndefined ( ) ;
56
69
localeService . addLocales ( [ TestLocale ] ) ;
57
70
localeService . setCurrentLocale ( TestLocale . id ) ;
58
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual ( TestLocale . id ) ;
71
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( TestLocale . id ) ;
59
72
60
73
expect ( localeService . setCurrentLocale ( 'unknown' ) ) . toEqual ( false ) ;
61
74
} ) ;
62
75
63
76
test ( 'Add locales' , ( ) => {
77
+ const localeService = new LocaleService ( ) ;
64
78
expect ( localeService . getLocales ( ) . length ) . toBe ( 2 ) ;
65
79
localeService . addLocales ( [ TestLocale ] ) ;
66
80
expect ( localeService . getLocales ( ) . length ) . toBe ( 3 ) ;
@@ -72,6 +86,7 @@ describe('The Locale Service', () => {
72
86
} ) ;
73
87
74
88
test ( 'Add an locale inherit the en' , ( ) => {
89
+ const localeService = new LocaleService ( ) ;
75
90
expect ( TestLocale . source . size ) . toBe ( 0 ) ;
76
91
( TestLocale as ILocale ) . inherit = 'en' ;
77
92
localeService . addLocales ( [ TestLocale ] ) ;
@@ -85,6 +100,7 @@ describe('The Locale Service', () => {
85
100
} ) ;
86
101
87
102
test ( 'Get a specific locale' , ( ) => {
103
+ const localeService = new LocaleService ( ) ;
88
104
localeService . addLocales ( [ TestLocale ] ) ;
89
105
expect ( localeService . getLocale ( TestLocale . id ) ) . not . toBeNull ( ) ;
90
106
expect ( localeService . getLocale ( TestLocale . id ) ?. id ) . toEqual (
@@ -93,6 +109,7 @@ describe('The Locale Service', () => {
93
109
} ) ;
94
110
95
111
test ( 'Remove a locale' , ( ) => {
112
+ const localeService = new LocaleService ( ) ;
96
113
localeService . addLocales ( [ TestLocale ] ) ;
97
114
expect ( localeService . getLocale ( TestLocale . id ) ?. id ) . toEqual (
98
115
TestLocale . id
@@ -103,9 +120,9 @@ describe('The Locale Service', () => {
103
120
localeService . setCurrentLocale ( TestLocale . id ) ;
104
121
105
122
//Remove the current locale
106
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual ( TestLocale . id ) ;
123
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( TestLocale . id ) ;
107
124
localeService . removeLocale ( TestLocale . id ) ;
108
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual (
125
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual (
109
126
localeService . getDefaultLocale ( ) . id
110
127
) ;
111
128
@@ -114,14 +131,16 @@ describe('The Locale Service', () => {
114
131
} ) ;
115
132
116
133
test ( 'Listen to the current locale change event' , ( ) => {
134
+ const localeService = new LocaleService ( ) ;
117
135
const fn = jest . fn ( ) ;
118
136
localeService . onChange ( fn ) ;
119
137
localeService . setCurrentLocale ( 'en' ) ;
120
138
expect ( fn ) . toBeCalledTimes ( 1 ) ;
121
- expect ( localeService . getCurrentLocale ( ) . id ) . toEqual ( 'en' ) ;
139
+ expect ( localeService . getCurrentLocale ( ) ! . id ) . toEqual ( 'en' ) ;
122
140
} ) ;
123
141
124
142
test ( 'Localize the source key' , ( ) => {
143
+ const localeService = new LocaleService ( ) ;
125
144
let res = localeService . localize ( 'test' ) ;
126
145
expect ( res ) . toEqual ( '' ) ;
127
146
0 commit comments