UNPKG

3.76 kBPlain TextView Raw
1import {TestBed} from "@angular/core/testing";
2import {Observable, of} from "rxjs";
3import {TranslateCompiler, TranslateFakeCompiler, TranslateLoader, TranslateModule, TranslateService} from "../public-api";
4
5let translations: any = {LOAD: 'This is a test'};
6
7class FakeLoader implements TranslateLoader {
8 getTranslation(lang: string): Observable<any> {
9 return of(translations);
10 }
11}
12
13describe('TranslateCompiler', () => {
14 let translate: TranslateService;
15
16 describe('with default TranslateFakeCompiler', () => {
17 beforeEach(() => {
18 TestBed.configureTestingModule({
19 imports: [
20 TranslateModule.forRoot({
21 loader: {provide: TranslateLoader, useClass: FakeLoader},
22 compiler: {provide: TranslateCompiler, useClass: TranslateFakeCompiler}
23 })
24 ],
25 });
26 translate = TestBed.inject(TranslateService);
27
28 translate.use('en');
29 });
30
31 it('should use the correct compiler', () => {
32 expect(translate).toBeDefined();
33 expect(translate.compiler).toBeDefined();
34 expect(translate.compiler instanceof TranslateFakeCompiler).toBeTruthy();
35 });
36
37 it('should use the compiler on loading translations', () => {
38 translate.get('LOAD').subscribe((res: string) => {
39 expect(res).toBe('This is a test');
40 });
41 });
42
43 it('should use the compiler on manually adding a translation object', () => {
44 translate.setTranslation('en', {'SET-TRANSLATION': 'A manually added translation'});
45 expect(translate.instant('SET-TRANSLATION')).toBe('A manually added translation');
46 });
47
48 it('should use the compiler on manually adding a single translation', () => {
49 translate.set('SET', 'Another manually added translation', 'en');
50 expect(translate.instant('SET')).toBe('Another manually added translation');
51 });
52 });
53
54 describe('with a custom compiler implementation', () => {
55 class CustomCompiler implements TranslateCompiler {
56 compile(value: string, lang: string): string {
57 return value + '|compiled';
58 }
59
60 compileTranslations(translation: any, lang: string): Object {
61 return Object.keys(translation).reduce((acc: any, key) => {
62 acc[key] = () => translation[key] + '|compiled';
63 return acc;
64 }, {});
65 }
66 }
67
68 beforeEach(() => {
69 TestBed.configureTestingModule({
70 imports: [
71 TranslateModule.forRoot({
72 loader: {provide: TranslateLoader, useClass: FakeLoader},
73 compiler: {provide: TranslateCompiler, useClass: CustomCompiler}
74 })
75 ],
76 });
77 translate = TestBed.inject(TranslateService);
78
79 translate.use('en');
80 });
81
82 it('should use the correct compiler', () => {
83 expect(translate).toBeDefined();
84 expect(translate.compiler).toBeDefined();
85 expect(translate.compiler instanceof CustomCompiler).toBeTruthy();
86 });
87
88 it('should use the compiler on loading translations', () => {
89 translate.get('LOAD').subscribe((res: string) => {
90 expect(res).toBe('This is a test|compiled');
91 });
92 });
93
94 it('should use the compiler on manually adding a translation object', () => {
95 translate.setTranslation('en', {'SET-TRANSLATION': 'A manually added translation'});
96 expect(translate.instant('SET-TRANSLATION')).toBe('A manually added translation|compiled');
97 });
98
99 it('should use the compiler on manually adding a single translation', () => {
100 translate.set('SET', 'Another manually added translation', 'en');
101 expect(translate.instant('SET')).toBe('Another manually added translation|compiled');
102 });
103 });
104});