@@ -62,21 +62,39 @@ describe('PoI18nService:', () => {
62
62
} ) ;
63
63
} ) ;
64
64
65
- it ( 'should get specific literals passing parameters' , done => {
65
+ it ( 'should get specific literals passing parameters' , ( ) => {
66
66
service . getLiterals ( { literals : [ 'text' ] } ) . subscribe ( ( literals : any ) => {
67
- expect ( literals [ 'text' ] ) . toBeTruthy ( ) ;
68
-
69
- done ( ) ;
67
+ expect ( literals [ 'text' ] ) . toBe ( 'texto' ) ;
70
68
} ) ;
71
69
} ) ;
72
70
73
- it ( 'should get specific literals from unexist language' , done => {
74
- // Procura em ingles, se não acho busca em pt-br
71
+ it ( 'should get pt-br literals from unexist language' , ( ) => {
75
72
service . getLiterals ( { literals : [ 'text' ] , language : 'en-us' } ) . subscribe ( ( literals : any ) => {
76
73
expect ( literals [ 'text' ] ) . toBeTruthy ( ) ;
74
+ } ) ;
75
+ } ) ;
77
76
78
- done ( ) ;
77
+ it ( 'should call getLiteralsFromContextService when servicesContext[context] exists' , ( ) => {
78
+ const observerMock = {
79
+ next : jasmine . createSpy ( 'next' ) ,
80
+ error : jasmine . createSpy ( 'error' ) ,
81
+ complete : jasmine . createSpy ( 'complete' )
82
+ } ;
83
+
84
+ service [ 'servicesContext' ] = { meuContexto : { } } ;
85
+
86
+ spyOn ( service , < any > 'getLiteralsFromContextService' ) . and . callFake ( ( ) => {
87
+ observerMock . complete ( ) ;
79
88
} ) ;
89
+
90
+ service . getLiterals ( { context : 'meuContexto' , language : 'pt' } ) . subscribe ( observerMock ) ;
91
+
92
+ expect ( service [ 'getLiteralsFromContextService' ] ) . toHaveBeenCalledWith (
93
+ 'pt' ,
94
+ 'meuContexto' ,
95
+ [ ] ,
96
+ jasmine . any ( Object )
97
+ ) ;
80
98
} ) ;
81
99
82
100
it ( 'should get literals with specific context' , ( ) => {
@@ -321,74 +339,24 @@ describe('PoI18nService:', () => {
321
339
322
340
service = TestBed . inject ( PoI18nService ) ;
323
341
httpMock = TestBed . inject ( HttpTestingController ) ;
324
- } ) ;
325
342
326
- it ( 'should get all literals from service' , done => {
327
- spyOn ( service , 'getLanguage' ) . and . returnValue ( 'pt' ) ;
328
-
329
- service . getLiterals ( ) . subscribe ( ( literals : any ) => {
330
- expect ( literals [ 'developer' ] ) . toBeTruthy ( ) ;
331
- expect ( literals [ 'task' ] ) . toBeTruthy ( ) ;
332
-
333
- done ( ) ;
343
+ spyOn ( localStorage , 'getItem' ) . and . callFake ( ( key : string ) => {
344
+ const mockStorage = {
345
+ 'en-general-label1' : 'Label 1' ,
346
+ 'en-general-label2' : 'Label 2'
347
+ } ;
348
+ return mockStorage [ key ] || null ;
334
349
} ) ;
335
-
336
- httpMock . expectOne ( ( req : HttpRequest < any > ) => req . method === 'GET' ) . flush ( mockResponse ) ;
337
- } ) ;
338
-
339
- it ( 'should return empty object when not found specific literals from service' , done => {
340
- spyOn ( service , 'getLanguage' ) . and . returnValue ( 'pt' ) ;
341
-
342
- service . getLiterals ( { literals : [ 'teste' ] } ) . subscribe ( ( literals : any ) => {
343
- expect ( Object . keys ( literals ) . length ) . toBe ( 0 ) ;
344
-
345
- done ( ) ;
346
- } ) ;
347
-
348
- httpMock . expectOne ( ( req : HttpRequest < any > ) => req . method === 'GET' ) . flush ( { } ) ;
349
- } ) ;
350
-
351
- it ( 'should get specific literals from localStorage' , done => {
352
- const developerTranslation = 'desenvolvedor' ;
353
- const taskTranslation = 'tarefa' ;
354
-
355
- const language = 'en' ;
356
-
357
- spyOn ( service , 'getLanguage' ) . and . returnValue ( language ) ;
358
-
359
- localStorage . setItem ( `${ language } -general-developer` , developerTranslation ) ;
360
- localStorage . setItem ( `${ language } -general-task` , taskTranslation ) ;
361
-
362
- service . getLiterals ( { literals : [ 'developer' , 'task' ] } ) . subscribe ( ( literals : any ) => {
363
- expect ( literals [ 'developer' ] ) . toEqual ( developerTranslation ) ;
364
- expect ( literals [ 'task' ] ) . toEqual ( taskTranslation ) ;
365
-
366
- done ( ) ;
367
- } ) ;
368
-
369
- localStorage . clear ( ) ;
370
350
} ) ;
371
351
372
- it ( 'should get literals from localStorage, selecting context, literals and language' , done => {
373
- const carTranslation = 'carro' ;
374
- const testTranslation = 'teste' ;
375
-
376
- localStorage . setItem ( 'pt-br-general-car' , carTranslation ) ;
377
- localStorage . setItem ( 'pt-br-another-test' , testTranslation ) ;
378
-
379
- service
380
- . getLiterals ( { context : 'general' , literals : [ 'car' , 'test' ] , language : 'pt-br' } )
381
- . subscribe ( ( literals : any ) => {
382
- expect ( literals [ 'car' ] ) . toEqual ( carTranslation ) ;
383
- expect ( literals [ 'test' ] ) . toBeUndefined ( ) ;
384
-
385
- done ( ) ;
352
+ describe ( 'Methods: ' , ( ) => {
353
+ describe ( 'getHttpService' , ( ) => {
354
+ it ( 'should return a http servic' , ( ) => {
355
+ const httpService = service [ 'getHttpService' ] ( '/' , 'pt' , [ 'text' ] ) ;
356
+ expect ( httpService ) . toBeTruthy ( ) ;
386
357
} ) ;
358
+ } ) ;
387
359
388
- localStorage . clear ( ) ;
389
- } ) ;
390
-
391
- describe ( 'Methods: ' , ( ) => {
392
360
describe ( 'getLiteralsFromContextService' , ( ) => {
393
361
it ( `should call 'observer.next' with translations if translations keys length is greater than 0
394
362
and call 'getLiteralsLocalStorageAndCache'` , ( ) => {
@@ -420,6 +388,74 @@ describe('PoI18nService:', () => {
420
388
expect ( spyMergeObject ) . toHaveBeenCalled ( ) ;
421
389
expect ( spyGetLiteralsLocalStorageAndCache ) . toHaveBeenCalled ( ) ;
422
390
} ) ;
391
+
392
+ it ( 'should assign languageAlternative to languageSearch when languageAlternative is provided' , ( ) => {
393
+ const language = 'en' ;
394
+ const languageAlternative = 'es' ;
395
+ const context = 'general' ;
396
+ const literals = [ 'label1' , 'label2' ] ;
397
+ const observer = { next : jasmine . createSpy ( 'next' ) } ;
398
+ const translations = { } ;
399
+
400
+ spyOn ( service as any , 'mergeObject' ) . and . callThrough ( ) ;
401
+ spyOn ( service , 'searchInVarI18n' as keyof PoI18nService ) . and . returnValue ( '' ) ;
402
+ spyOn ( service , 'countObject' as keyof PoI18nService ) . and . returnValue ( '0' ) ;
403
+ spyOn ( service , 'getLiteralsLocalStorageAndCache' as keyof PoI18nService ) ;
404
+
405
+ service [ 'getLiteralsFromContextService' ] (
406
+ language ,
407
+ context ,
408
+ literals ,
409
+ observer ,
410
+ translations ,
411
+ languageAlternative
412
+ ) ;
413
+
414
+ expect ( service [ 'getLiteralsLocalStorageAndCache' ] ) . toHaveBeenCalledWith (
415
+ languageAlternative ,
416
+ context ,
417
+ literals ,
418
+ observer ,
419
+ translations ,
420
+ languageAlternative
421
+ ) ;
422
+ } ) ;
423
+ } ) ;
424
+
425
+ describe ( 'searchInLocalStorage' , ( ) => {
426
+ it ( 'should return translations when literals exist in localStorage' , ( ) => {
427
+ const language = 'en' ;
428
+ const context = 'general' ;
429
+ const literals = [ 'label1' , 'label2' ] ;
430
+
431
+ const result = service [ 'searchInLocalStorage' ] ( language , context , literals ) ;
432
+
433
+ expect ( result ) . toEqual ( {
434
+ label1 : 'Label 1' ,
435
+ label2 : 'Label 2'
436
+ } ) ;
437
+ } ) ;
438
+
439
+ it ( 'should return an empty object when literals are not found in localStorage' , ( ) => {
440
+ const language = 'en' ;
441
+ const context = 'general' ;
442
+ const literals = [ 'label3' , 'label4' ] ; // Literais não presentes no mockStorage
443
+
444
+ const result = service [ 'searchInLocalStorage' ] ( language , context , literals ) ;
445
+
446
+ expect ( result ) . toEqual ( { } ) ;
447
+ } ) ;
448
+
449
+ it ( 'should return an empty object when literals array is empty' , ( ) => {
450
+ const language = 'en' ;
451
+ const context = 'general' ;
452
+ const literals : Array < string > = [ ] ;
453
+
454
+ const result = service [ 'searchInLocalStorage' ] ( language , context , literals ) ;
455
+
456
+ expect ( result ) . toEqual ( { } ) ;
457
+ expect ( localStorage . getItem ) . not . toHaveBeenCalled ( ) ;
458
+ } ) ;
423
459
} ) ;
424
460
425
461
describe ( 'getLiteralsLocalStorageAndCache' , ( ) => {
@@ -554,6 +590,21 @@ describe('PoI18nService:', () => {
554
590
expect ( mergedObject . people ) . toBe ( expectedPeopleTranslation ) ;
555
591
expect ( Object . keys ( mergedObject ) . length ) . toBe ( 2 ) ;
556
592
} ) ;
593
+
594
+ it ( 'updateLocalStorage: should store values in localStorage when useCache is true' , ( ) => {
595
+ service [ 'useCache' ] = true ;
596
+
597
+ spyOn ( localStorage , 'setItem' ) . and . callFake ( ( ) => { } ) ;
598
+ const language = 'en' ;
599
+ const context = 'general' ;
600
+ const data = { label1 : 'Label 1' , label2 : 'Label 2' } ;
601
+
602
+ service [ 'updateLocalStorage' ] ( language , context , data ) ;
603
+
604
+ expect ( localStorage . setItem ) . toHaveBeenCalledTimes ( 2 ) ;
605
+ expect ( localStorage . setItem ) . toHaveBeenCalledWith ( 'en-general-label1' , 'Label 1' ) ;
606
+ expect ( localStorage . setItem ) . toHaveBeenCalledWith ( 'en-general-label2' , 'Label 2' ) ;
607
+ } ) ;
557
608
} ) ;
558
609
} ) ;
559
610
} ) ;
0 commit comments