@@ -4,6 +4,7 @@ import {MockBackend, MockConnection} from "@angular/http/testing";
4
4
import {
5
5
TranslateService ,
6
6
MissingTranslationHandler ,
7
+ MissingTranslationHandlerParams ,
7
8
TranslateLoader ,
8
9
TranslateStaticLoader ,
9
10
LangChangeEvent ,
@@ -310,23 +311,23 @@ describe('MissingTranslationHandler', () => {
310
311
let missingTranslationHandler : MissingTranslationHandler ;
311
312
312
313
class Missing implements MissingTranslationHandler {
313
- handle ( key : string ) {
314
+ handle ( params : MissingTranslationHandlerParams ) {
314
315
return "handled" ;
315
316
}
316
317
}
317
318
318
319
class MissingObs implements MissingTranslationHandler {
319
- handle ( key : string ) : Observable < any > {
320
- return Observable . of ( `handled: ${ key } ` ) ;
320
+ handle ( params : MissingTranslationHandlerParams ) : Observable < any > {
321
+ return Observable . of ( `handled: ${ params . key } ` ) ;
321
322
}
322
323
}
323
324
324
325
let prepare = ( ( handlerClass : Function ) => {
325
326
TestBed . configureTestingModule ( {
326
327
imports : [ HttpModule , TranslateModule . forRoot ( ) ] ,
327
328
providers : [
328
- { provide : MissingTranslationHandler , useClass : handlerClass } ,
329
- { provide : XHRBackend , useClass : MockBackend }
329
+ { provide : MissingTranslationHandler , useClass : handlerClass } ,
330
+ { provide : XHRBackend , useClass : MockBackend }
330
331
]
331
332
} ) ;
332
333
injector = getTestBed ( ) ;
@@ -351,7 +352,40 @@ describe('MissingTranslationHandler', () => {
351
352
spyOn ( missingTranslationHandler , 'handle' ) . and . callThrough ( ) ;
352
353
353
354
translate . get ( 'nonExistingKey' ) . subscribe ( ( res : string ) => {
354
- expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( 'nonExistingKey' ) ;
355
+ expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { key : 'nonExistingKey' } ) ) ;
356
+ //test that the instance of the last called argument is string
357
+ expect ( res ) . toEqual ( 'handled' ) ;
358
+ } ) ;
359
+
360
+ // mock response after the xhr request, otherwise it will be undefined
361
+ mockBackendResponse ( connection , '{"TEST": "This is a test"}' ) ;
362
+ } ) ;
363
+
364
+ it ( 'should propagate interpolation params when the key does not exist' , ( ) => {
365
+ prepare ( Missing ) ;
366
+ translate . use ( 'en' ) ;
367
+ spyOn ( missingTranslationHandler , 'handle' ) . and . callThrough ( ) ;
368
+ let interpolateParams = { some : 'params' } ;
369
+
370
+ translate . get ( 'nonExistingKey' , interpolateParams ) . subscribe ( ( res : string ) => {
371
+ expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { interpolateParams : interpolateParams } ) ) ;
372
+ //test that the instance of the last called argument is string
373
+ expect ( res ) . toEqual ( 'handled' ) ;
374
+ } ) ;
375
+
376
+ // mock response after the xhr request, otherwise it will be undefined
377
+ mockBackendResponse ( connection , '{"TEST": "This is a test"}' ) ;
378
+ } ) ;
379
+
380
+ it ( 'should propagate TranslationService params when the key does not exist' , ( ) => {
381
+ prepare ( Missing ) ;
382
+ translate . use ( 'en' ) ;
383
+ spyOn ( missingTranslationHandler , 'handle' ) . and . callThrough ( ) ;
384
+ let interpolateParams = { some : 'params' } ;
385
+
386
+ translate . get ( 'nonExistingKey' , interpolateParams ) . subscribe ( ( res : string ) => {
387
+ expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { translateService : translate } ) ) ;
388
+ //test that the instance of the last called argument is string
355
389
expect ( res ) . toEqual ( 'handled' ) ;
356
390
} ) ;
357
391
@@ -361,7 +395,7 @@ describe('MissingTranslationHandler', () => {
361
395
362
396
it ( 'should return the key when using MissingTranslationHandler & the handler returns nothing' , ( ) => {
363
397
class MissingUndef implements MissingTranslationHandler {
364
- handle ( key : string ) {
398
+ handle ( params : MissingTranslationHandlerParams ) {
365
399
}
366
400
}
367
401
@@ -370,7 +404,7 @@ describe('MissingTranslationHandler', () => {
370
404
spyOn ( missingTranslationHandler , 'handle' ) . and . callThrough ( ) ;
371
405
372
406
translate . get ( 'nonExistingKey' ) . subscribe ( ( res : string ) => {
373
- expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( 'nonExistingKey' ) ;
407
+ expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { key : 'nonExistingKey' } ) ) ;
374
408
expect ( res ) . toEqual ( 'nonExistingKey' ) ;
375
409
} ) ;
376
410
@@ -397,7 +431,7 @@ describe('MissingTranslationHandler', () => {
397
431
spyOn ( missingTranslationHandler , 'handle' ) . and . callThrough ( ) ;
398
432
399
433
expect ( translate . instant ( 'nonExistingKey' ) ) . toEqual ( 'handled' ) ;
400
- expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( 'nonExistingKey' ) ;
434
+ expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { key : 'nonExistingKey' } ) ) ;
401
435
} ) ;
402
436
403
437
it ( 'should wait for the MissingTranslationHandler when it returns an observable & we use get' , ( ) => {
@@ -406,7 +440,7 @@ describe('MissingTranslationHandler', () => {
406
440
spyOn ( missingTranslationHandler , 'handle' ) . and . callThrough ( ) ;
407
441
408
442
translate . get ( 'nonExistingKey' ) . subscribe ( ( res : string ) => {
409
- expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( 'nonExistingKey' ) ;
443
+ expect ( missingTranslationHandler . handle ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { key : 'nonExistingKey' } ) ) ;
410
444
expect ( res ) . toEqual ( 'handled: nonExistingKey' ) ;
411
445
} ) ;
412
446
0 commit comments