1
1
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2
- import { ReplaySubject } from 'rxjs' ;
3
2
import { tx } from '@transifex/native' ;
3
+ import { ReplaySubject } from 'rxjs' ;
4
4
5
- import { TComponent } from '../src/lib/T.component' ;
6
- import { SafeHtmlPipe , TranslationService , TXInstanceComponent } from '../src/public-api' ;
7
-
5
+ import { TComponent } from '../lib/T.component' ;
6
+ import {
7
+ SafeHtmlPipe ,
8
+ TranslationService ,
9
+ TXInstanceComponent ,
10
+ } from '../public-api' ;
8
11
9
12
describe ( 'TComponent' , ( ) => {
10
13
let localeChangedSubject : ReplaySubject < string > ;
@@ -30,16 +33,19 @@ describe('TComponent', () => {
30
33
await TestBed . configureTestingModule ( {
31
34
declarations : [ TComponent , SafeHtmlPipe , TXInstanceComponent ] ,
32
35
providers : [ TXInstanceComponent ] ,
33
- } )
34
- . compileComponents ( ) ;
36
+ } ) . compileComponents ( ) ;
35
37
36
38
localeChangedSubject = new ReplaySubject < string > ( 0 ) ;
37
39
38
40
service = TestBed . inject ( TranslationService ) ;
39
41
instance = TestBed . inject ( TXInstanceComponent ) ;
40
42
41
43
spyOn ( service , 'getCurrentLocale' ) . and . returnValue ( 'en' ) ;
42
- localeChangedSpy = spyOnProperty ( service , 'localeChanged' , 'get' ) . and . returnValue ( localeChangedSubject ) ;
44
+ localeChangedSpy = spyOnProperty (
45
+ service ,
46
+ 'localeChanged' ,
47
+ 'get'
48
+ ) . and . returnValue ( localeChangedSubject ) ;
43
49
spyOn ( service , 'setCurrentLocale' ) . and . callFake ( async ( locale ) => {
44
50
localeChangedSubject . next ( locale ) ;
45
51
} ) ;
@@ -54,8 +60,11 @@ describe('TComponent', () => {
54
60
it ( 'should create the component' , async ( ) => {
55
61
// setup
56
62
spyOn ( component , 'translate' ) ;
57
- localeChangedSpy = spyOnProperty ( component , 'localeChanged' , 'get' )
58
- . and . returnValue ( localeChangedSubject ) ;
63
+ localeChangedSpy = spyOnProperty (
64
+ component ,
65
+ 'localeChanged' ,
66
+ 'get'
67
+ ) . and . returnValue ( localeChangedSubject ) ;
59
68
60
69
// act
61
70
component . ngOnInit ( ) ;
@@ -82,8 +91,11 @@ describe('TComponent', () => {
82
91
fixture . detectChanges ( ) ;
83
92
84
93
// assert
85
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' ,
86
- { ...translationParams } , '' ) ;
94
+ expect ( service . translate ) . toHaveBeenCalledWith (
95
+ 'not-translated' ,
96
+ { ...translationParams } ,
97
+ ''
98
+ ) ;
87
99
expect ( component . translatedStr ) . toEqual ( 'translated' ) ;
88
100
} ) ;
89
101
@@ -98,8 +110,11 @@ describe('TComponent', () => {
98
110
fixture . detectChanges ( ) ;
99
111
100
112
// assert
101
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' ,
102
- { ...translationParams } , '' ) ;
113
+ expect ( service . translate ) . toHaveBeenCalledWith (
114
+ 'not-translated' ,
115
+ { ...translationParams } ,
116
+ ''
117
+ ) ;
103
118
expect ( component . translatedStr ) . toEqual ( 'translated' ) ;
104
119
} ) ;
105
120
@@ -114,8 +129,11 @@ describe('TComponent', () => {
114
129
fixture . detectChanges ( ) ;
115
130
116
131
// assert
117
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' ,
118
- { ...translationParams , _key : 'key-not-translated' } , '' ) ;
132
+ expect ( service . translate ) . toHaveBeenCalledWith (
133
+ 'not-translated' ,
134
+ { ...translationParams , _key : 'key-not-translated' } ,
135
+ ''
136
+ ) ;
119
137
expect ( component . translatedStr ) . toEqual ( 'translated' ) ;
120
138
} ) ;
121
139
@@ -130,8 +148,9 @@ describe('TComponent', () => {
130
148
131
149
// assert
132
150
const compiled = fixture . debugElement . nativeElement ;
133
- expect ( ( compiled as HTMLDivElement ) . innerHTML )
134
- . toContain ( '<a>translated</a>' ) ;
151
+ expect ( ( compiled as HTMLDivElement ) . innerHTML ) . toContain (
152
+ '<a>translated</a>'
153
+ ) ;
135
154
} ) ;
136
155
137
156
it ( 'should translate and sanitize the string' , ( ) => {
@@ -146,8 +165,9 @@ describe('TComponent', () => {
146
165
147
166
// assert
148
167
const compiled = fixture . debugElement . nativeElement ;
149
- expect ( ( compiled as HTMLDivElement ) . innerHTML )
150
- . toContain ( '<span><a>translated</a></span>' ) ;
168
+ expect ( ( compiled as HTMLDivElement ) . innerHTML ) . toContain (
169
+ '<span><a>translated</a></span>'
170
+ ) ;
151
171
} ) ;
152
172
153
173
it ( 'should detect input parameters change and translate' , ( ) => {
@@ -158,14 +178,15 @@ describe('TComponent', () => {
158
178
// act
159
179
service . translate ( 'test' , { ...translationParams } ) ;
160
180
component . str = 'other-value' ;
161
- component . ngOnChanges ( )
181
+ component . ngOnChanges ( ) ;
162
182
fixture . detectChanges ( ) ;
163
183
164
184
// assert
165
185
expect ( service . translate ) . toHaveBeenCalled ( ) ;
166
186
const compiled = fixture . debugElement . nativeElement ;
167
- expect ( ( compiled as HTMLDivElement ) . innerHTML )
168
- . toContain ( '<a>translated</a>' ) ;
187
+ expect ( ( compiled as HTMLDivElement ) . innerHTML ) . toContain (
188
+ '<a>translated</a>'
189
+ ) ;
169
190
} ) ;
170
191
171
192
it ( 'should detect localeChange and translate' , async ( ) => {
@@ -183,8 +204,11 @@ describe('TComponent', () => {
183
204
fixture . detectChanges ( ) ;
184
205
185
206
// assert
186
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' ,
187
- { ...translationParams , _key : 'key-not-translated' } , '' ) ;
207
+ expect ( service . translate ) . toHaveBeenCalledWith (
208
+ 'not-translated' ,
209
+ { ...translationParams , _key : 'key-not-translated' } ,
210
+ ''
211
+ ) ;
188
212
expect ( component . translatedStr ) . toEqual ( 'translated-again' ) ;
189
213
} ) ;
190
214
@@ -203,11 +227,15 @@ describe('TComponent', () => {
203
227
fixture . detectChanges ( ) ;
204
228
205
229
// assert
206
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' , {
207
- ...translationParams ,
208
- _key : 'key-not-translated' ,
209
- _context : 'late' ,
210
- } , '' ) ;
230
+ expect ( service . translate ) . toHaveBeenCalledWith (
231
+ 'not-translated' ,
232
+ {
233
+ ...translationParams ,
234
+ _key : 'key-not-translated' ,
235
+ _context : 'late' ,
236
+ } ,
237
+ ''
238
+ ) ;
211
239
expect ( component . translatedStr ) . toEqual ( 'translated' ) ;
212
240
} ) ;
213
241
@@ -223,8 +251,11 @@ describe('TComponent', () => {
223
251
fixture . detectChanges ( ) ;
224
252
225
253
// assert
226
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' ,
227
- { ...translationParams } , 'instance-alias' ) ;
254
+ expect ( service . translate ) . toHaveBeenCalledWith (
255
+ 'not-translated' ,
256
+ { ...translationParams } ,
257
+ 'instance-alias'
258
+ ) ;
228
259
expect ( component . translatedStr ) . toEqual ( 'translated' ) ;
229
260
} ) ;
230
261
@@ -245,24 +276,26 @@ describe('TComponent', () => {
245
276
expect ( service . translate ) . toHaveBeenCalled ( ) ;
246
277
} ) ;
247
278
248
- it ( 'should detect translationsFetched using alternative instance' ,
249
- async ( ) => {
250
- // setup
251
- instance . token = 'instance-token' ;
252
- instance . alias = 'instance-alias' ;
253
- spyOn ( service , 'translate' ) . and . returnValue ( 'translated' ) ;
254
-
255
- // act
256
- component . str = 'not-translated' ;
257
- component . ngOnInit ( ) ;
258
- fixture . detectChanges ( ) ;
259
-
260
- // change
261
- await service . fetchTranslations ( 'tag1' ) ;
262
- fixture . detectChanges ( ) ;
263
-
264
- // assert
265
- expect ( service . translate ) . toHaveBeenCalledWith ( 'not-translated' ,
266
- { ...translationParams } , 'instance-alias' ) ;
267
- } ) ;
279
+ it ( 'should detect translationsFetched using alternative instance' , async ( ) => {
280
+ // setup
281
+ instance . token = 'instance-token' ;
282
+ instance . alias = 'instance-alias' ;
283
+ spyOn ( service , 'translate' ) . and . returnValue ( 'translated' ) ;
284
+
285
+ // act
286
+ component . str = 'not-translated' ;
287
+ component . ngOnInit ( ) ;
288
+ fixture . detectChanges ( ) ;
289
+
290
+ // change
291
+ await service . fetchTranslations ( 'tag1' ) ;
292
+ fixture . detectChanges ( ) ;
293
+
294
+ // assert
295
+ expect ( service . translate ) . toHaveBeenCalledWith (
296
+ 'not-translated' ,
297
+ { ...translationParams } ,
298
+ 'instance-alias'
299
+ ) ;
300
+ } ) ;
268
301
} ) ;
0 commit comments