UNPKG

12.5 kBPlain TextView Raw
1import {ChangeDetectionStrategy, Component, ElementRef, Injectable, ViewChild, ViewContainerRef} from '@angular/core';
2import {ComponentFixture, TestBed} from '@angular/core/testing';
3import {TranslateModule, TranslateService} from '../public-api';
4
5@Injectable()
6@Component({
7 selector: 'hmx-app',
8 changeDetection: ChangeDetectionStrategy.OnPush,
9 template: `
10 <div #noKey translate>TEST</div>
11 <div #contentAsKey translate>TEST.VALUE</div>
12 <div #withKey [translate]="'TEST'">Some init content</div>
13 <div #noContent [translate]="'TEST'"></div>
14 <div #withOtherElements translate>TEST1 <span>Hey</span> TEST2</div>
15 <div #withParams [translate]="'TEST'" [translateParams]="value">Some init content</div>
16 <div #withParamsNoKey translate [translateParams]="value">TEST</div>
17 <div #leadingSpaceNoKeyNoParams translate> TEST</div>
18 <div #trailingSpaceNoKeyNoParams translate>TEST </div>
19 <div #withSpaceAndLineBreakNoKeyNoParams translate>
20 TEST
21 </div>
22 `
23})
24class AppComponent {
25 viewContainerRef: ViewContainerRef;
26 @ViewChild('noKey', {static: true}) noKey!: ElementRef;
27 @ViewChild('contentAsKey', {static: true}) contentAsKey!: ElementRef;
28 @ViewChild('withKey', {static: true}) withKey!: ElementRef;
29 @ViewChild('withOtherElements', {static: true}) withOtherElements!: ElementRef;
30 @ViewChild('withParams', {static: true}) withParams!: ElementRef;
31 @ViewChild('withParamsNoKey', {static: true}) withParamsNoKey!: ElementRef;
32 @ViewChild('noContent', {static: true}) noContent!: ElementRef;
33 @ViewChild('leadingSpaceNoKeyNoParams') leadingSpaceNoKeyNoParams!: ElementRef;
34 @ViewChild('trailingSpaceNoKeyNoParams') trailingSpaceNoKeyNoParams!: ElementRef;
35 @ViewChild('withSpaceAndLineBreakNoKeyNoParams') withSpaceAndLineBreakNoKeyNoParams!: ElementRef;
36 value = {value: 'ok'};
37
38 constructor(viewContainerRef: ViewContainerRef) {
39 this.viewContainerRef = viewContainerRef;
40 }
41}
42
43describe('TranslateDirective', () => {
44 let translate: TranslateService;
45 let fixture: ComponentFixture<AppComponent>;
46
47 beforeEach(() => {
48 TestBed.configureTestingModule({
49 imports: [
50 TranslateModule.forRoot()
51 ],
52 declarations: [AppComponent]
53 });
54 translate = TestBed.inject(TranslateService);
55
56 fixture = (<any>TestBed).createComponent(AppComponent);
57 fixture.detectChanges();
58 });
59
60 it('should translate a string using the container value', () => {
61 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
62
63 translate.setTranslation('en', {"TEST": "This is a test"});
64 translate.use('en');
65
66 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('This is a test');
67 });
68
69 it('should translate a string using the container value as a key', () => {
70 expect(fixture.componentInstance.contentAsKey.nativeElement.innerHTML).toEqual('TEST.VALUE');
71
72 translate.setTranslation('en', {"TEST": {"VALUE": "This is a test"}});
73 translate.use('en');
74
75 expect(fixture.componentInstance.contentAsKey.nativeElement.innerHTML).toEqual('This is a test');
76 });
77
78 it('should translate a string using the key value', () => {
79 // replace the content with the key
80 expect(fixture.componentInstance.withKey.nativeElement.innerHTML).toEqual('TEST');
81
82 translate.setTranslation('en', {"TEST": "This is a test"});
83 translate.use('en');
84
85 expect(fixture.componentInstance.withKey.nativeElement.innerHTML).toEqual('This is a test');
86 });
87
88 it('should translate first child strings with elements in the middle', () => {
89 // replace the content with the key
90 expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('TEST1 <span>Hey</span> TEST2');
91
92 translate.setTranslation('en', {"TEST1": "Awesome", "TEST2": "it works"});
93 translate.use('en');
94
95 expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('Awesome <span>Hey</span> it works');
96 });
97
98 it('should translate first child strings without recursion', () => {
99 // replace the content with the key
100 expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('TEST1 <span>Hey</span> TEST2');
101
102 translate.setTranslation('en', {"TEST1": "TEST2", "TEST2": "it works"});
103 translate.use('en');
104
105 expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('TEST2 <span>Hey</span> it works');
106 });
107
108 it('should translate a string with params and a key', () => {
109 // replace the content with the key
110 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
111
112 translate.setTranslation('en', {"TEST": "It is {{value}}"});
113 translate.use('en');
114
115 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is ok');
116 });
117
118 it('should translate a string with params and no key', () => {
119 // replace the content with the key
120 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('TEST');
121
122 translate.setTranslation('en', {"TEST": "It is {{value}}"});
123 translate.use('en');
124
125 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('It is ok');
126 });
127
128 it('should update the translation when params change', () => {
129 // replace the content with the key
130 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
131 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('TEST');
132
133 translate.setTranslation('en', {"TEST": "It is {{value}}"});
134 translate.use('en');
135
136 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is ok');
137 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('It is ok');
138 fixture.componentInstance.value = {value: 'changed'};
139 fixture.detectChanges();
140
141 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is changed');
142 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('It is changed');
143 });
144
145 it('should update the DOM when the lang changes and the translation key starts with space', () => {
146 expect(fixture.componentInstance.leadingSpaceNoKeyNoParams.nativeElement.innerHTML).toEqual(' TEST');
147
148 const en = "This is a test - with leading spaces in translation key";
149 const fr = "C'est un test - avec un espace de tête dans la clé de traduction";
150 const leadingSpaceFromKey = ' ';
151 translate.setTranslation('en', {"TEST": en});
152 translate.setTranslation('fr', {"TEST": fr});
153
154 translate.use('en');
155 expect(fixture.componentInstance.leadingSpaceNoKeyNoParams.nativeElement.innerHTML).toEqual(leadingSpaceFromKey + en);
156
157 translate.use('fr');
158 expect(fixture.componentInstance.leadingSpaceNoKeyNoParams.nativeElement.innerHTML).toEqual(leadingSpaceFromKey + fr);
159 });
160
161 it('should update the DOM when the lang changes and the translation key has line breaks and spaces', () => {
162 expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(' TEST ');
163
164 const en = "This is a test - with trailing spaces in translation key";
165 const fr = "C'est un test - avec un espace de fuite dans la clé de traduction";
166 const whiteSpaceFromKey = ' ';
167 translate.setTranslation('en', {"TEST": en});
168 translate.setTranslation('fr', {"TEST": fr});
169
170 translate.use('en');
171 expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(whiteSpaceFromKey + en + whiteSpaceFromKey);
172
173 translate.use('fr');
174 expect(fixture.componentInstance.withSpaceAndLineBreakNoKeyNoParams.nativeElement.innerHTML).toEqual(whiteSpaceFromKey + fr + whiteSpaceFromKey);
175 });
176
177 it('should update the DOM when the lang changes and the translation key ends with space', () => {
178 expect(fixture.componentInstance.trailingSpaceNoKeyNoParams.nativeElement.innerHTML).toEqual('TEST ');
179
180 const en = "This is a test - with spaces and line breaks in translation key";
181 const fr = "C'est un test - avec des espaces et sauts de lignes dans la clé de traduction";
182 const trailingSpaceFromKey = ' ';
183 translate.setTranslation('en', {"TEST": en});
184 translate.setTranslation('fr', {"TEST": fr});
185
186 translate.use('en');
187 expect(fixture.componentInstance.trailingSpaceNoKeyNoParams.nativeElement.innerHTML).toEqual(en + trailingSpaceFromKey);
188
189 translate.use('fr');
190 expect(fixture.componentInstance.trailingSpaceNoKeyNoParams.nativeElement.innerHTML).toEqual(fr + trailingSpaceFromKey);
191 });
192
193 it('should update the DOM when the lang changes', () => {
194 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
195 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
196 expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('TEST');
197
198 translate.setTranslation('en', {"TEST": "This is a test"});
199 translate.setTranslation('fr', {"TEST": "C'est un test"});
200
201 translate.use('en');
202 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('This is a test');
203 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('This is a test');
204 expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('This is a test');
205
206 translate.use('fr');
207 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual("C'est un test");
208 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual("C'est un test");
209 expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual("C'est un test");
210 });
211
212 it('should update the DOM when the lang changes and the translation ends with space', () => {
213 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
214 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
215 expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('TEST');
216
217 const en = " This is a test - with spaces ";
218 const fr = " C'est un test - avec espaces ";
219
220 translate.setTranslation('en', {"TEST": en});
221 translate.setTranslation('fr', {"TEST": fr});
222
223 translate.use('en');
224 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(`${en}`);
225 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual(en);
226 expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual(en);
227
228 translate.use('fr');
229 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(`${fr}`);
230 expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual(fr);
231 expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual(fr);
232 });
233
234 it('should update the DOM when the default lang changes', () => {
235 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
236
237 translate.setTranslation('en', {"TEST": "This is a test"});
238 translate.setTranslation('fr', {"TEST": "C'est un test"});
239 translate.setDefaultLang('en');
240 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('This is a test');
241
242 translate.setDefaultLang('fr');
243 expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual("C'est un test");
244 });
245
246 it('should unsubscribe from lang change subscription on destroy', () => {
247 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('TEST');
248
249 fixture.destroy();
250
251 translate.setTranslation('en', {"TEST": "This is a test"});
252 translate.use('en');
253
254 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('TEST');
255 });
256
257 it('should unsubscribe from default lang change subscription on destroy', () => {
258 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('TEST');
259
260 fixture.destroy();
261
262 translate.setTranslation('en', {"TEST": "This is a test"});
263 translate.setDefaultLang('en');
264
265 expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('TEST');
266 });
267});