1 | import {TestBed} from "@angular/core/testing";
|
2 | import {Observable, of} from "rxjs";
|
3 | import {TranslateLoader, TranslateModule, TranslateService} from "../public-api";
|
4 |
|
5 | let translations: any = {"TEST": "This is a test"};
|
6 |
|
7 | class FakeLoader implements TranslateLoader {
|
8 | getTranslation(lang: string): Observable<any> {
|
9 | return of(translations);
|
10 | }
|
11 | }
|
12 |
|
13 | describe('TranslateLoader', () => {
|
14 | let translate: TranslateService;
|
15 |
|
16 | it('should be able to provide TranslateStaticLoader', () => {
|
17 | TestBed.configureTestingModule({
|
18 | imports: [
|
19 | TranslateModule.forRoot({
|
20 | loader: {provide: TranslateLoader, useClass: FakeLoader}
|
21 | })
|
22 | ],
|
23 | });
|
24 | translate = TestBed.inject(TranslateService);
|
25 |
|
26 | expect(translate).toBeDefined();
|
27 | expect(translate.currentLoader).toBeDefined();
|
28 | expect(translate.currentLoader instanceof FakeLoader).toBeTruthy();
|
29 |
|
30 |
|
31 | translate.use('en');
|
32 |
|
33 |
|
34 | translate.get('TEST').subscribe((res: string) => {
|
35 | expect(res).toEqual('This is a test');
|
36 | });
|
37 | });
|
38 |
|
39 | it('should be able to provide any TranslateLoader', () => {
|
40 | class CustomLoader implements TranslateLoader {
|
41 | getTranslation(lang: string): Observable<any> {
|
42 | return of({"TEST": "This is also a test"});
|
43 | }
|
44 | }
|
45 |
|
46 | TestBed.configureTestingModule({
|
47 | imports: [
|
48 | TranslateModule.forRoot({
|
49 | loader: {provide: TranslateLoader, useClass: CustomLoader}
|
50 | })
|
51 | ]
|
52 | });
|
53 | translate = TestBed.inject(TranslateService);
|
54 |
|
55 | expect(translate).toBeDefined();
|
56 | expect(translate.currentLoader).toBeDefined();
|
57 | expect(translate.currentLoader instanceof CustomLoader).toBeTruthy();
|
58 |
|
59 |
|
60 | translate.use('en');
|
61 |
|
62 |
|
63 | translate.get('TEST').subscribe((res: string) => {
|
64 | expect(res).toEqual('This is also a test');
|
65 | });
|
66 | });
|
67 |
|
68 | });
|