1
- import { Injector } from '@angular/core' ;
2
- import { LocalizeParser , ManualParserLoader } from '../src/localize-router.parser' ;
3
- import { getTestBed , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
4
- import { Routes } from '@angular/router' ;
5
- import { Observable } from 'rxjs/Observable' ;
6
- import { TranslateService } from '@ngx-translate/core' ;
7
- import { Location , CommonModule } from '@angular/common' ;
1
+ import { Injector } from '@angular/core' ;
2
+ import { LocalizeParser , ManualParserLoader } from '../src/localize-router.parser' ;
3
+ import { getTestBed , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
4
+ import { Routes } from '@angular/router' ;
5
+ import { Observable } from 'rxjs/Observable' ;
6
+ import { TranslateService } from '@ngx-translate/core' ;
7
+ import { Location , CommonModule } from '@angular/common' ;
8
8
9
9
class FakeTranslateService {
10
10
defLang : string ;
@@ -13,28 +13,45 @@ class FakeTranslateService {
13
13
browserLang : string = '' ;
14
14
15
15
content : any = {
16
- 'PREFIX.home' : 'home_TR' ,
17
- 'PREFIX.about' : 'about_TR'
16
+ 'PREFIX.home' : 'home_' ,
17
+ 'PREFIX.about' : 'about_' ,
18
+ 'PREFIX.contact' : 'contact_' ,
19
+ 'PREFIX.info' : 'info_'
18
20
} ;
19
21
20
- setDefaultLang ( lang : string ) { this . defLang = lang ; }
21
- getDefaultLang ( ) { return this . defLang ; }
22
+ setDefaultLang ( lang : string ) {
23
+ this . defLang = lang ;
24
+ }
25
+
26
+ getDefaultLang ( ) {
27
+ return this . defLang ;
28
+ }
29
+
22
30
use ( lang : string ) {
23
31
this . currentLang = lang ;
24
- return Observable . of ( this . content ) ;
32
+ return Observable . of ( Object . keys ( this . content ) . reduce ( ( prev : any , key ) => {
33
+ prev [ key ] = this . content [ key ] + this . currentLang ;
34
+ return prev ;
35
+ } , { } ) ) ;
25
36
}
26
- get ( input : string ) { return Observable . of ( this . content [ input ] || input ) ; }
27
37
28
- getBrowserLang ( ) { return this . browserLang ; }
38
+ get ( input : string ) {
39
+ return Observable . of ( this . content [ input ] ? this . content [ input ] + this . currentLang : input ) ;
40
+ }
41
+
42
+ getBrowserLang ( ) {
43
+ return this . browserLang ;
44
+ }
29
45
}
30
46
31
47
class FakeLocation {
32
- path ( ) :string {
33
- return "" ;
48
+ path ( ) : string {
49
+ return '' ;
34
50
}
35
51
}
36
52
37
- class DummyComponent { }
53
+ class DummyComponent {
54
+ }
38
55
39
56
describe ( 'LocalizeParser' , ( ) => {
40
57
let injector : Injector ;
@@ -50,16 +67,18 @@ describe('LocalizeParser', () => {
50
67
TestBed . configureTestingModule ( {
51
68
imports : [ CommonModule ] ,
52
69
providers : [
53
- { provide : TranslateService , useClass : FakeTranslateService } ,
54
- { provide : Location , useClass : FakeLocation }
70
+ { provide : TranslateService , useClass : FakeTranslateService } ,
71
+ { provide : Location , useClass : FakeLocation }
55
72
]
56
73
} ) ;
57
74
routes = [
58
75
{ path : '' , redirectTo : 'some/path' } ,
59
- { path : 'some/path' , children : [
76
+ {
77
+ path : 'some/path' , children : [
60
78
{ path : '' , redirectTo : 'nothing' } ,
61
79
{ path : 'else/:id' , redirectTo : 'nothing/else' }
62
- ] }
80
+ ]
81
+ }
63
82
] ;
64
83
locales = [ 'en' , 'de' , 'fr' ] ;
65
84
localStorage . removeItem ( 'LOCALIZE_LOCAL_STORAGE' ) ;
@@ -132,8 +151,8 @@ describe('LocalizeParser', () => {
132
151
routes = [ ] ;
133
152
loader . load ( routes ) ;
134
153
tick ( ) ;
135
- expect ( routes [ 0 ] ) . toEqual ( { path : '' , redirectTo : 'de' , pathMatch : 'full' } ) ;
136
- expect ( routes [ 1 ] ) . toEqual ( { path : 'de' , children : [ ] } ) ;
154
+ expect ( routes [ 0 ] ) . toEqual ( { path : '' , redirectTo : 'de' , pathMatch : 'full' } ) ;
155
+ expect ( routes [ 1 ] ) . toEqual ( { path : 'de' , children : [ ] } ) ;
137
156
expect ( loader . currentLang ) . toEqual ( 'de' ) ;
138
157
expect ( translate . currentLang ) . toEqual ( 'de' ) ;
139
158
} ) ) ;
@@ -147,8 +166,8 @@ describe('LocalizeParser', () => {
147
166
routes = [ ] ;
148
167
loader . load ( routes ) ;
149
168
tick ( ) ;
150
- expect ( routes [ 0 ] ) . toEqual ( { path : '' , redirectTo : 'fr' , pathMatch : 'full' } ) ;
151
- expect ( routes [ 1 ] ) . toEqual ( { path : 'fr' , children : [ ] } ) ;
169
+ expect ( routes [ 0 ] ) . toEqual ( { path : '' , redirectTo : 'fr' , pathMatch : 'full' } ) ;
170
+ expect ( routes [ 1 ] ) . toEqual ( { path : 'fr' , children : [ ] } ) ;
152
171
expect ( loader . currentLang ) . toEqual ( 'fr' , 'loader currentLang should equal' ) ;
153
172
expect ( translate . currentLang ) . toEqual ( 'fr' , 'translate currentLang should equal' ) ;
154
173
} ) ) ;
@@ -157,8 +176,7 @@ describe('LocalizeParser', () => {
157
176
loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
158
177
spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
159
178
160
- ( < any > translate ) [ 'browserLang' ] = 'sr' ;
161
- localStorage . removeItem ( 'LOCALIZE_DEFAULT_LANGUAGE' ) ;
179
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
162
180
163
181
routes = [ ] ;
164
182
loader . load ( routes ) ;
@@ -172,60 +190,145 @@ describe('LocalizeParser', () => {
172
190
loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
173
191
spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
174
192
175
- ( < any > translate ) [ 'browserLang' ] = 'sr' ;
193
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
176
194
177
- routes = [ { path : 'home' , component : DummyComponent } ] ;
195
+ routes = [ { path : 'home' , component : DummyComponent } , { path : '** ', component : DummyComponent } ] ;
178
196
loader . load ( routes ) ;
179
197
tick ( ) ;
180
- expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_TR' ) ;
198
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_en' ) ;
199
+ expect ( routes . length ) . toEqual ( 3 ) ;
200
+ } ) ) ;
201
+ it ( 'should translate path to new language' , fakeAsync ( ( ) => {
202
+ loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
203
+ spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
204
+
205
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
206
+
207
+ routes = [ { path : 'home' , component : DummyComponent } ] ;
208
+ loader . load ( routes ) ;
209
+ tick ( ) ;
210
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_en' ) ;
211
+ loader . translateRoutes ( 'de' ) . subscribe ( ( ) => {
212
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_de' ) ;
213
+ } ) ;
214
+ tick ( ) ;
181
215
} ) ) ;
182
216
183
217
it ( 'should not translate path if translation not found' , fakeAsync ( ( ) => {
184
218
loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
185
219
spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
186
- ( < any > translate ) [ 'browserLang' ] = 'sr' ;
187
220
188
- routes = [ { path : 'abc' , component : DummyComponent } ] ;
221
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
222
+
223
+ routes = [ { path : 'abc' , component : DummyComponent } ] ;
189
224
loader . load ( routes ) ;
190
225
tick ( ) ;
191
226
expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'abc' ) ;
192
227
} ) ) ;
228
+ it ( 'should not translate if prefix does not match' , fakeAsync ( ( ) => {
229
+ loader = new ManualParserLoader ( translate , location , locales ) ;
230
+ spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
231
+
232
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
233
+
234
+ routes = [ { path : 'home' , component : DummyComponent } ] ;
235
+ loader . load ( routes ) ;
236
+ tick ( ) ;
237
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home' ) ;
238
+ } ) ) ;
239
+ it ( 'should not translate if prefix does not match' , fakeAsync ( ( ) => {
240
+ loader = new ManualParserLoader ( translate , location , locales , null ) ;
241
+ spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
242
+
243
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
244
+
245
+ routes = [ { path : 'home' , component : DummyComponent } ] ;
246
+ loader . load ( routes ) ;
247
+ tick ( ) ;
248
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home' ) ;
249
+ } ) ) ;
193
250
194
251
it ( 'should translate redirectTo' , fakeAsync ( ( ) => {
195
252
loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
196
253
spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
197
- ( < any > translate ) [ 'browserLang' ] = 'sr' ;
198
254
199
- routes = [ { redirectTo : 'home' } ] ;
255
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
256
+
257
+ routes = [ { redirectTo : 'home' } ] ;
258
+ loader . load ( routes ) ;
259
+ tick ( ) ;
260
+ expect ( routes [ 1 ] . children [ 0 ] . redirectTo ) . toEqual ( 'home_en' ) ;
261
+ } ) ) ;
262
+ it ( 'should translate wildcard redirectTo' , fakeAsync ( ( ) => {
263
+ loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
264
+ spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
265
+
266
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
267
+
268
+ routes = [ { path : 'home' , component : DummyComponent } , { path : '**' , redirectTo : '/home' } ] ;
200
269
loader . load ( routes ) ;
201
270
tick ( ) ;
202
- expect ( routes [ 1 ] . children [ 0 ] . redirectTo ) . toEqual ( 'home_TR ' ) ;
271
+ expect ( routes [ 2 ] . redirectTo ) . toEqual ( '/en/home_en ' ) ;
203
272
} ) ) ;
204
273
205
274
it ( 'should translate complex path segments' , fakeAsync ( ( ) => {
206
275
loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
207
276
spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
208
- ( < any > translate ) [ 'browserLang' ] = 'sr' ;
209
277
210
- routes = [ { path : '/home/about' , component : DummyComponent } ] ;
278
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
279
+
280
+ routes = [ { path : '/home/about' , component : DummyComponent } ] ;
211
281
loader . load ( routes ) ;
212
282
tick ( ) ;
213
- expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( '/home_TR/about_TR ' ) ;
283
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( '/home_en/about_en ' ) ;
214
284
} ) ) ;
215
285
216
286
it ( 'should translate children' , fakeAsync ( ( ) => {
217
287
loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
218
288
spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
219
- ( < any > translate ) [ 'browserLang' ] = 'sr' ;
289
+
290
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
220
291
221
292
routes = [
222
- { path : 'home' , children : [
223
- { path : 'about' , component : DummyComponent }
224
- ] }
293
+ {
294
+ path : 'home' , children : [
295
+ { path : 'about' , component : DummyComponent }
296
+ ]
297
+ } ,
298
+ {
299
+ path : 'contact' , children : [
300
+ { path : 'info' , component : DummyComponent }
301
+ ]
302
+ }
225
303
] ;
226
304
loader . load ( routes ) ;
227
305
tick ( ) ;
228
- expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_TR' ) ;
229
- expect ( routes [ 1 ] . children [ 0 ] . children [ 0 ] . path ) . toEqual ( 'about_TR' ) ;
306
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_en' ) ;
307
+ expect ( routes [ 1 ] . children [ 0 ] . children [ 0 ] . path ) . toEqual ( 'about_en' ) ;
308
+ } ) ) ;
309
+ it ( 'should translate lazy loaded children' , fakeAsync ( ( ) => {
310
+ loader = new ManualParserLoader ( translate , location , locales , prefix ) ;
311
+ spyOn ( loader , 'translateRoutes' ) . and . callThrough ( ) ;
312
+
313
+ localStorage . setItem ( 'LOCALIZE_DEFAULT_LANGUAGE' , 'en' ) ;
314
+
315
+ routes = [
316
+ {
317
+ path : 'home' , children : [
318
+ { path : 'about' , component : DummyComponent }
319
+ ]
320
+ } ,
321
+ {
322
+ path : 'contact' , loadChildren : '#pathToSomeModule'
323
+ }
324
+ ] ;
325
+ ( < any > routes [ 1 ] ) [ '_loadedConfig' ] = { routes : [ { path : 'info' , component : DummyComponent } ] } ;
326
+
327
+ loader . load ( routes ) ;
328
+ tick ( ) ;
329
+ expect ( routes [ 1 ] . children [ 0 ] . path ) . toEqual ( 'home_en' ) ;
330
+ expect ( routes [ 1 ] . children [ 0 ] . children [ 0 ] . path ) . toEqual ( 'about_en' ) ;
331
+ expect ( routes [ 1 ] . children [ 1 ] . path ) . toEqual ( 'contact_en' ) ;
332
+ expect ( ( < any > routes [ 1 ] . children [ 1 ] ) . _loadedConfig . routes [ 0 ] . path ) . toEqual ( 'info_en' ) ;
230
333
} ) ) ;
231
334
} ) ;
0 commit comments