Skip to content

Commit 6e6f4d0

Browse files
committed
(chore) Unit tests
1 parent 0bebd80 commit 6e6f4d0

File tree

2 files changed

+155
-50
lines changed

2 files changed

+155
-50
lines changed

tests/localize-router.parser.spec.ts

Lines changed: 147 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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';
88

99
class FakeTranslateService {
1010
defLang: string;
@@ -13,28 +13,45 @@ class FakeTranslateService {
1313
browserLang: string = '';
1414

1515
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_'
1820
};
1921

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+
2230
use(lang: string) {
2331
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+
}, {}));
2536
}
26-
get(input: string) { return Observable.of(this.content[input] || input); }
2737

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+
}
2945
}
3046

3147
class FakeLocation {
32-
path():string {
33-
return "";
48+
path(): string {
49+
return '';
3450
}
3551
}
3652

37-
class DummyComponent {}
53+
class DummyComponent {
54+
}
3855

3956
describe('LocalizeParser', () => {
4057
let injector: Injector;
@@ -50,16 +67,18 @@ describe('LocalizeParser', () => {
5067
TestBed.configureTestingModule({
5168
imports: [CommonModule],
5269
providers: [
53-
{provide: TranslateService, useClass: FakeTranslateService},
54-
{provide: Location, useClass: FakeLocation}
70+
{ provide: TranslateService, useClass: FakeTranslateService },
71+
{ provide: Location, useClass: FakeLocation }
5572
]
5673
});
5774
routes = [
5875
{ path: '', redirectTo: 'some/path' },
59-
{ path: 'some/path', children: [
76+
{
77+
path: 'some/path', children: [
6078
{ path: '', redirectTo: 'nothing' },
6179
{ path: 'else/:id', redirectTo: 'nothing/else' }
62-
]}
80+
]
81+
}
6382
];
6483
locales = ['en', 'de', 'fr'];
6584
localStorage.removeItem('LOCALIZE_LOCAL_STORAGE');
@@ -132,8 +151,8 @@ describe('LocalizeParser', () => {
132151
routes = [];
133152
loader.load(routes);
134153
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: [] });
137156
expect(loader.currentLang).toEqual('de');
138157
expect(translate.currentLang).toEqual('de');
139158
}));
@@ -147,8 +166,8 @@ describe('LocalizeParser', () => {
147166
routes = [];
148167
loader.load(routes);
149168
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: [] });
152171
expect(loader.currentLang).toEqual('fr', 'loader currentLang should equal');
153172
expect(translate.currentLang).toEqual('fr', 'translate currentLang should equal');
154173
}));
@@ -157,8 +176,7 @@ describe('LocalizeParser', () => {
157176
loader = new ManualParserLoader(translate, location, locales, prefix);
158177
spyOn(loader, 'translateRoutes').and.callThrough();
159178

160-
(<any>translate)['browserLang'] = 'sr';
161-
localStorage.removeItem('LOCALIZE_DEFAULT_LANGUAGE');
179+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
162180

163181
routes = [];
164182
loader.load(routes);
@@ -172,60 +190,145 @@ describe('LocalizeParser', () => {
172190
loader = new ManualParserLoader(translate, location, locales, prefix);
173191
spyOn(loader, 'translateRoutes').and.callThrough();
174192

175-
(<any>translate)['browserLang'] = 'sr';
193+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
176194

177-
routes = [{path: 'home', component: DummyComponent }];
195+
routes = [{ path: 'home', component: DummyComponent }, { path: '**', component: DummyComponent }];
178196
loader.load(routes);
179197
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();
181215
}));
182216

183217
it('should not translate path if translation not found', fakeAsync(() => {
184218
loader = new ManualParserLoader(translate, location, locales, prefix);
185219
spyOn(loader, 'translateRoutes').and.callThrough();
186-
(<any>translate)['browserLang'] = 'sr';
187220

188-
routes = [{path: 'abc', component: DummyComponent }];
221+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
222+
223+
routes = [{ path: 'abc', component: DummyComponent }];
189224
loader.load(routes);
190225
tick();
191226
expect(routes[1].children[0].path).toEqual('abc');
192227
}));
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+
}));
193250

194251
it('should translate redirectTo', fakeAsync(() => {
195252
loader = new ManualParserLoader(translate, location, locales, prefix);
196253
spyOn(loader, 'translateRoutes').and.callThrough();
197-
(<any>translate)['browserLang'] = 'sr';
198254

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' }];
200269
loader.load(routes);
201270
tick();
202-
expect(routes[1].children[0].redirectTo).toEqual('home_TR');
271+
expect(routes[2].redirectTo).toEqual('/en/home_en');
203272
}));
204273

205274
it('should translate complex path segments', fakeAsync(() => {
206275
loader = new ManualParserLoader(translate, location, locales, prefix);
207276
spyOn(loader, 'translateRoutes').and.callThrough();
208-
(<any>translate)['browserLang'] = 'sr';
209277

210-
routes = [{path: '/home/about', component: DummyComponent }];
278+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
279+
280+
routes = [{ path: '/home/about', component: DummyComponent }];
211281
loader.load(routes);
212282
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');
214284
}));
215285

216286
it('should translate children', fakeAsync(() => {
217287
loader = new ManualParserLoader(translate, location, locales, prefix);
218288
spyOn(loader, 'translateRoutes').and.callThrough();
219-
(<any>translate)['browserLang'] = 'sr';
289+
290+
localStorage.setItem('LOCALIZE_DEFAULT_LANGUAGE', 'en');
220291

221292
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+
}
225303
];
226304
loader.load(routes);
227305
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');
230333
}));
231334
});

tests/util.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,24 @@ describe('util', () => {
5555
expect(equals({text: 123, same: 1}, {text: 123, same: 1})).toBe(true);
5656
});
5757
it('should ignore if inherited fields dont match', () => {
58-
class Klass1 {
58+
class Class1 {
5959
same: boolean;
6060

61+
first() { }
62+
6163
constructor() { }
6264
}
63-
Klass1.prototype['first'] = 'first';
64-
class Klass2 {
65+
class Class2 {
6566
same: boolean;
6667

68+
second() { }
69+
6770
constructor() { }
6871
}
69-
Klass2.prototype['second'] = 'second';
7072

71-
let instance1: any = new Klass1();
73+
let instance1: any = new Class1();
7274
instance1.same = true;
73-
let instance2: any = new Klass2();
75+
let instance2: any = new Class2();
7476
instance2.same = true;
7577

7678
expect(equals(instance1, instance2)).toBe(true);

0 commit comments

Comments
 (0)