UNPKG

10.2 kBPlain TextView Raw
1import {ChangeDetectionStrategy, ChangeDetectorRef, Component, Injectable, ViewContainerRef} from "@angular/core";
2import {TestBed} from "@angular/core/testing";
3import {Observable, of} from "rxjs";
4import {DefaultLangChangeEvent, LangChangeEvent, TranslateLoader, TranslateModule, TranslatePipe, TranslateService} from "../public-api";
5
6class FakeChangeDetectorRef extends ChangeDetectorRef {
7 markForCheck(): void {
8 }
9
10 detach(): void {
11 }
12
13 detectChanges(): void {
14 }
15
16 checkNoChanges(): void {
17 }
18
19 reattach(): void {
20 }
21}
22
23@Injectable()
24@Component({
25 selector: 'hmx-app',
26 changeDetection: ChangeDetectionStrategy.OnPush,
27 template: `{{'TEST' | translate}}`
28})
29class AppComponent {
30 viewContainerRef: ViewContainerRef;
31
32 constructor(viewContainerRef: ViewContainerRef) {
33 this.viewContainerRef = viewContainerRef;
34 }
35}
36
37let translations: any = {"TEST": "This is a test"};
38
39class FakeLoader implements TranslateLoader {
40 getTranslation(lang: string): Observable<any> {
41 return of(translations);
42 }
43}
44
45describe('TranslatePipe', () => {
46 let translate: TranslateService;
47 let translatePipe: TranslatePipe;
48 let ref: any;
49
50 beforeEach(() => {
51 TestBed.configureTestingModule({
52 imports: [
53 TranslateModule.forRoot({
54 loader: {provide: TranslateLoader, useClass: FakeLoader}
55 })
56 ],
57 declarations: [AppComponent]
58 });
59 translate = TestBed.inject(TranslateService);
60 ref = new FakeChangeDetectorRef();
61 translatePipe = new TranslatePipe(translate, ref);
62 });
63
64 afterEach(() => {
65 translations = {"TEST": "This is a test"};
66 ref = undefined;
67 });
68
69 it('is defined', () => {
70 expect(TranslatePipe).toBeDefined();
71 expect(translatePipe).toBeDefined();
72 expect(translatePipe instanceof TranslatePipe).toBeTruthy();
73 });
74
75 it('should translate a string', () => {
76 translate.setTranslation('en', {"TEST": "This is a test"});
77 translate.use('en');
78
79 expect(translatePipe.transform('TEST')).toEqual("This is a test");
80 });
81
82 it('should call markForChanges when it translates a string', () => {
83 translate.setTranslation('en', {"TEST": "This is a test"});
84 translate.use('en');
85 jest.spyOn(ref, 'markForCheck');
86
87 translatePipe.transform('TEST');
88 expect(ref.markForCheck).toHaveBeenCalled();
89 });
90
91 it('should translate a string with object parameters', () => {
92 translate.setTranslation('en', {"TEST": "This is a test {{param}}"});
93 translate.use('en');
94
95 expect(translatePipe.transform('TEST', {param: "with param"})).toEqual("This is a test with param");
96 });
97
98 it('should translate a string with object as string parameters', () => {
99 translate.setTranslation('en', {"TEST": "This is a test {{param}}"});
100 translate.use('en');
101
102 expect(translatePipe.transform('TEST', '{param: "with param"}')).toEqual("This is a test with param");
103 expect(translatePipe.transform('TEST', '{"param": "with param"}')).toEqual("This is a test with param");
104 expect(translatePipe.transform('TEST', "{param: 'with param'}")).toEqual("This is a test with param");
105 expect(translatePipe.transform('TEST', "{'param' : 'with param'}")).toEqual("This is a test with param");
106 });
107
108 it('should translate a string with object as multiple string parameters', () => {
109 translate.setTranslation('en', {"TEST": "This is a test {{param1}} {{param2}}"});
110 translate.use('en');
111
112 expect(translatePipe.transform('TEST', '{param1: "with param-1", param2: "and param-2"}'))
113 .toEqual("This is a test with param-1 and param-2");
114 expect(translatePipe.transform('TEST', '{"param1": "with param-1", "param2": "and param-2"}'))
115 .toEqual("This is a test with param-1 and param-2");
116 expect(translatePipe.transform('TEST', "{param1: 'with param-1', param2: 'and param-2'}"))
117 .toEqual("This is a test with param-1 and param-2");
118 expect(translatePipe.transform('TEST', "{'param1' : 'with param-1', 'param2': 'and param-2'}"))
119 .toEqual("This is a test with param-1 and param-2");
120 });
121
122 it('should translate a string with object as nested string parameters', () => {
123 translate.setTranslation('en', {"TEST": "This is a test {{param.one}} {{param.two}}"});
124 translate.use('en');
125
126 expect(translatePipe.transform('TEST', '{param: {one: "with param-1", two: "and param-2"}}'))
127 .toEqual("This is a test with param-1 and param-2");
128 expect(translatePipe.transform('TEST', '{"param": {"one": "with param-1", "two": "and param-2"}}'))
129 .toEqual("This is a test with param-1 and param-2");
130 expect(translatePipe.transform('TEST', "{param: {one: 'with param-1', two: 'and param-2'}}"))
131 .toEqual("This is a test with param-1 and param-2");
132 expect(translatePipe.transform('TEST', "{'param' : {'one': 'with param-1', 'two': 'and param-2'}}"))
133 .toEqual("This is a test with param-1 and param-2");
134 });
135
136 it('should update the value when the parameters change', () => {
137 translate.setTranslation('en', {"TEST": "This is a test {{param}}"});
138 translate.use('en');
139
140 jest.spyOn(translatePipe, 'updateValue');
141 jest.spyOn(ref, 'markForCheck');
142
143 expect(translatePipe.transform('TEST', {param: "with param"})).toEqual("This is a test with param");
144 // same value, shouldn't call 'updateValue' again
145 expect(translatePipe.transform('TEST', {param: "with param"})).toEqual("This is a test with param");
146 // different param, should call 'updateValue'
147 expect(translatePipe.transform('TEST', {param: "with param2"})).toEqual("This is a test with param2");
148 expect(translatePipe.updateValue).toHaveBeenCalledTimes(2);
149 expect(ref.markForCheck).toHaveBeenCalledTimes(2);
150 });
151
152 it("should throw if you don't give an object parameter", () => {
153 translate.setTranslation('en', {"TEST": "This is a test {{param}}"});
154 translate.use('en');
155 let param = 'param: "with param"';
156
157 expect(() => {
158 translatePipe.transform('TEST', param);
159 }).toThrowError(`Wrong parameter in TranslatePipe. Expected a valid Object, received: ${param}`);
160 });
161
162 it("should return given falsey or non length query", () => {
163 translate.setTranslation('en', {"TEST": "This is a test"});
164 translate.use('en');
165
166 expect(translatePipe.transform(null as any)).toBeNull();
167 expect(translatePipe.transform(undefined as any)).toBeUndefined();
168 expect(translatePipe.transform(1234 as any)).toBe(1234);
169 });
170
171 describe('should update translations on lang change', () => {
172 it('with fake loader', (done) => {
173 translate.setTranslation('en', {"TEST": "This is a test"});
174 translate.setTranslation('fr', {"TEST": "C'est un test"});
175 translate.use('en');
176
177 expect(translatePipe.transform('TEST')).toEqual("This is a test");
178
179 // this will be resolved at the next lang change
180 let subscription = translate.onLangChange.subscribe((res: LangChangeEvent) => {
181 expect(res.lang).toEqual('fr');
182 expect(translatePipe.transform('TEST')).toEqual("C'est un test");
183 subscription.unsubscribe();
184 done();
185 });
186
187 translate.use('fr');
188 });
189
190 it('with file loader', (done) => {
191 translate.use('en');
192 expect(translatePipe.transform('TEST')).toEqual("This is a test");
193
194 // this will be resolved at the next lang change
195 let subscription = translate.onLangChange.subscribe((res: LangChangeEvent) => {
196 // let it update the translations
197 setTimeout(() => {
198 expect(res.lang).toEqual('fr');
199 expect(translatePipe.transform('TEST')).toEqual("C'est un test");
200 subscription.unsubscribe();
201 done();
202 });
203 });
204
205 translations = {"TEST": "C'est un test"};
206 translate.use('fr');
207 });
208
209 it('should detect changes with OnPush', () => {
210 let fixture = (<any>TestBed).createComponent(AppComponent);
211 fixture.detectChanges();
212 expect(fixture.debugElement.nativeElement.innerHTML).toEqual("TEST");
213 translate.use('en');
214 fixture.detectChanges();
215 expect(fixture.debugElement.nativeElement.innerHTML).toEqual("This is a test");
216 });
217 });
218
219 describe('should update translations on default lang change', () => {
220 it('with fake loader', (done) => {
221 translate.setTranslation('en', {"TEST": "This is a test"});
222 translate.setTranslation('fr', {"TEST": "C'est un test"});
223 translate.setDefaultLang('en');
224
225 expect(translatePipe.transform('TEST')).toEqual("This is a test");
226
227 // this will be resolved at the next lang change
228 let subscription = translate.onDefaultLangChange.subscribe((res: DefaultLangChangeEvent) => {
229 expect(res.lang).toEqual('fr');
230 expect(translatePipe.transform('TEST')).toEqual("C'est un test");
231 subscription.unsubscribe();
232 done();
233 });
234
235 translate.setDefaultLang('fr');
236 });
237
238 it('with file loader', (done) => {
239 translate.setDefaultLang('en');
240 expect(translatePipe.transform('TEST')).toEqual("This is a test");
241
242 // this will be resolved at the next lang change
243 let subscription = translate.onDefaultLangChange.subscribe((res: DefaultLangChangeEvent) => {
244 // let it update the translations
245 setTimeout(() => {
246 expect(res.lang).toEqual('fr');
247 expect(translatePipe.transform('TEST')).toEqual("C'est un test");
248 subscription.unsubscribe();
249 done();
250 });
251 });
252
253 translations = {"TEST": "C'est un test"};
254 translate.setDefaultLang('fr');
255 });
256
257 it('should detect changes with OnPush', () => {
258 let fixture = (<any>TestBed).createComponent(AppComponent);
259 fixture.detectChanges();
260 expect(fixture.debugElement.nativeElement.innerHTML).toEqual("TEST");
261 translate.setDefaultLang('en');
262 fixture.detectChanges();
263 expect(fixture.debugElement.nativeElement.innerHTML).toEqual("This is a test");
264 });
265 });
266});