UNPKG

7.5 kBPlain TextView Raw
1import {TestBed} from "@angular/core/testing";
2import {Observable, of} from "rxjs";
3import {MissingTranslationHandler, MissingTranslationHandlerParams, TranslateLoader, TranslateModule, TranslateService} from "../public-api";
4
5let translations: any = {"TEST": "This is a test"};
6let fakeTranslation: any = {"NOT_USED": "not used"};
7
8class FakeLoader implements TranslateLoader {
9 getTranslation(lang: string): Observable<any> {
10 if (lang === 'fake') {
11 return of(fakeTranslation);
12 }
13
14 return of(translations);
15 }
16}
17
18describe('MissingTranslationHandler', () => {
19 let translate: TranslateService;
20 let missingTranslationHandler: MissingTranslationHandler;
21
22 class Missing implements MissingTranslationHandler {
23 handle(params: MissingTranslationHandlerParams) {
24 return "handled";
25 }
26 }
27
28 class MissingObs implements MissingTranslationHandler {
29 handle(params: MissingTranslationHandlerParams): Observable<any> {
30 return of(`handled: ${params.key}`);
31 }
32 }
33
34 let prepare = ((handlerClass: Function, defaultLang: boolean = true) => {
35 TestBed.configureTestingModule({
36 imports: [
37 TranslateModule.forRoot({
38 loader: {provide: TranslateLoader, useClass: FakeLoader},
39 useDefaultLang: defaultLang
40 })
41 ],
42 providers: [
43 {provide: MissingTranslationHandler, useClass: handlerClass}
44 ]
45 });
46 translate = TestBed.inject(TranslateService);
47 missingTranslationHandler = TestBed.inject(MissingTranslationHandler);
48 });
49
50 afterEach(() => {
51 translations = {"TEST": "This is a test"};
52 });
53
54 it('should use the MissingTranslationHandler when the key does not exist', () => {
55 prepare(Missing);
56 translate.use('en');
57 jest.spyOn(missingTranslationHandler, 'handle');
58
59 translate.get('nonExistingKey').subscribe((res: string) => {
60 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({key: 'nonExistingKey'}));
61 //test that the instance of the last called argument is string
62 expect(res).toEqual('handled');
63 });
64 });
65
66 it('should propagate interpolation params when the key does not exist', () => {
67 prepare(Missing);
68 translate.use('en');
69 jest.spyOn(missingTranslationHandler, 'handle');
70 let interpolateParams = {some: 'params'};
71
72 translate.get('nonExistingKey', interpolateParams).subscribe((res: string) => {
73 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({interpolateParams: interpolateParams}));
74 //test that the instance of the last called argument is string
75 expect(res).toEqual('handled');
76 });
77 });
78
79 it('should propagate TranslationService params when the key does not exist', () => {
80 prepare(Missing);
81 translate.use('en');
82 jest.spyOn(missingTranslationHandler, 'handle');
83 let interpolateParams = {some: 'params'};
84
85 translate.get('nonExistingKey', interpolateParams).subscribe((res: string) => {
86 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({translateService: translate}));
87 //test that the instance of the last called argument is string
88 expect(res).toEqual('handled');
89 });
90 });
91
92 it('should return the key when using MissingTranslationHandler & the handler returns nothing', () => {
93 class MissingUndef implements MissingTranslationHandler {
94 handle(params: MissingTranslationHandlerParams) {
95 }
96 }
97
98 prepare(MissingUndef);
99 translate.use('en');
100 jest.spyOn(missingTranslationHandler, 'handle');
101
102 translate.get('nonExistingKey').subscribe((res: string) => {
103 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({key: 'nonExistingKey'}));
104 expect(res).toEqual('nonExistingKey');
105 });
106 });
107
108 it('should not call the MissingTranslationHandler when the key exists', () => {
109 prepare(Missing);
110 translate.use('en');
111 jest.spyOn(missingTranslationHandler, 'handle');
112
113 translate.get('TEST').subscribe(() => {
114 expect(missingTranslationHandler.handle).not.toHaveBeenCalled();
115 });
116 });
117
118 it('should use the MissingTranslationHandler when the key does not exist & we use instant translation', () => {
119 prepare(Missing);
120 translate.use('en');
121 jest.spyOn(missingTranslationHandler, 'handle');
122
123 expect(translate.instant('nonExistingKey')).toEqual('handled');
124 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({key: 'nonExistingKey'}));
125 });
126
127 it('should wait for the MissingTranslationHandler when it returns an observable & we use get', () => {
128 prepare(MissingObs);
129 translate.use('en');
130 jest.spyOn(missingTranslationHandler, 'handle');
131
132 translate.get('nonExistingKey').subscribe((res: string) => {
133 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({key: 'nonExistingKey'}));
134 expect(res).toEqual('handled: nonExistingKey');
135 });
136 });
137
138 it('should wait for the MissingTranslationHandler when it returns an observable & we use get with an array', () => {
139 let tr = {
140 nonExistingKey1: 'handled: nonExistingKey1',
141 nonExistingKey2: 'handled: nonExistingKey2',
142 nonExistingKey3: 'handled: nonExistingKey3'
143 };
144
145 prepare(MissingObs);
146 translate.use('en');
147 jest.spyOn(missingTranslationHandler, 'handle');
148
149 translate.get(Object.keys(tr)).subscribe((res: string) => {
150 expect(missingTranslationHandler.handle).toHaveBeenCalledTimes(3);
151 expect(res).toEqual(tr as any);
152 });
153 });
154
155 it('should not wait for the MissingTranslationHandler when it returns an observable & we use instant', () => {
156 prepare(MissingObs);
157 translate.use('en');
158 jest.spyOn(missingTranslationHandler, 'handle');
159
160 expect(translate.instant('nonExistingKey')).toEqual('nonExistingKey');
161 });
162
163 it('should not wait for the MissingTranslationHandler when it returns an observable & we use instant with an array', () => {
164 let tr = {
165 nonExistingKey1: 'handled: nonExistingKey1',
166 nonExistingKey2: 'handled: nonExistingKey2',
167 nonExistingKey3: 'handled: nonExistingKey3'
168 };
169
170 prepare(MissingObs);
171 translate.use('en');
172 jest.spyOn(missingTranslationHandler, 'handle');
173
174 expect(translate.instant(Object.keys(tr))).toEqual({
175 nonExistingKey1: 'nonExistingKey1',
176 nonExistingKey2: 'nonExistingKey2',
177 nonExistingKey3: 'nonExistingKey3'
178 } as any);
179 });
180
181 it('should not return default translation, but missing handler', () => {
182 prepare(Missing, false);
183 translate.use('en');
184 translate.use('fake');
185
186 jest.spyOn(missingTranslationHandler, 'handle');
187 translate.get('TEST').subscribe((res: string) => {
188 expect(missingTranslationHandler.handle).toHaveBeenCalledWith(expect.objectContaining({key: 'TEST'}));
189 //test that the instance of the last called argument is string
190 expect(res).toEqual('handled');
191 });
192 });
193
194 it('should return default translation', () => {
195 prepare(Missing, true);
196 translate.use('en');
197 translate.use('fake');
198
199 jest.spyOn(missingTranslationHandler, 'handle');
200 translate.get('TEST').subscribe((res: string) => {
201 expect(res).toEqual('This is a test');
202 });
203 });
204});
205
\No newline at end of file