1
1
import { ChangeDetectorRef , Component , DebugElement , Input } from '@angular/core' ;
2
- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2
+ import { ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
3
3
import { FormControl , FormGroup , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
4
4
import { By } from '@angular/platform-browser' ;
5
5
import { CaseField } from '../../domain/definition/case-field.model' ;
@@ -10,6 +10,27 @@ import { PlaceholderService } from './services/placeholder.service';
10
10
import createSpyObj = jasmine . createSpyObj ;
11
11
import { RpxTranslatePipe , RpxTranslationConfig , RpxTranslationService } from 'rpx-xui-translation' ;
12
12
import { HttpClient , HttpHandler } from '@angular/common/http' ;
13
+ import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject' ;
14
+
15
+ class MockRpxTranslationService {
16
+ private langSubject = new BehaviorSubject < string > ( 'en' ) ;
17
+ private _language = 'en' ;
18
+ language$ = this . langSubject . asObservable ( ) ;
19
+
20
+ get language ( ) {
21
+ return this . _language ;
22
+ }
23
+
24
+ set language ( lang : string ) {
25
+ this . _language = lang ;
26
+ this . langSubject . next ( lang ) ;
27
+ }
28
+
29
+ setLanguage ( lang : string ) {
30
+ this . _language = lang ;
31
+ this . langSubject . next ( lang ) ;
32
+ }
33
+ }
13
34
14
35
@Component ( {
15
36
template : `
@@ -73,7 +94,7 @@ describe('LabelSubstitutorDirective', () => {
73
94
FieldsUtils ,
74
95
FormatTranslatorService ,
75
96
{ provide : RpxTranslatePipe , useValue : mockTranslationPipe } ,
76
- RpxTranslationService ,
97
+ { provide : RpxTranslationService , useClass : MockRpxTranslationService } ,
77
98
RpxTranslationConfig ,
78
99
HttpClient ,
79
100
HttpHandler ,
@@ -728,4 +749,160 @@ describe('LabelSubstitutorDirective', () => {
728
749
expect ( placeholderService . resolvePlaceholders ) . toHaveBeenCalledWith ( { LabelB : '' , LabelA : TRANSFORMED_VALUES } , label ) ;
729
750
} ) ;
730
751
} ) ;
731
- } ) ;
752
+
753
+ describe ( 'Language change and translation functionality' , ( ) => {
754
+ let translationService : MockRpxTranslationService ;
755
+
756
+ beforeEach ( ( ) => {
757
+ translationService = TestBed . inject ( RpxTranslationService ) as any ;
758
+ } ) ;
759
+
760
+ it ( 'should set isTranslated to false when language is not cy' , fakeAsync ( ( ) => {
761
+ const label = 'English label with ${placeholder}' ;
762
+ comp . caseField = textField ( 'LabelB' , '' , label ) ;
763
+ comp . caseFields = [ comp . caseField , textField ( 'LabelA' , 'ValueA' , '' ) ] ;
764
+
765
+ placeholderService . resolvePlaceholders . and . returnValues ( 'English label with ValueA' , '' , '' ) ;
766
+ fixture . detectChanges ( ) ;
767
+
768
+ placeholderService . resolvePlaceholders . calls . reset ( ) ;
769
+ placeholderService . resolvePlaceholders . and . returnValues ( 'English label with ValueA' , '' , '' ) ;
770
+
771
+ translationService . setLanguage ( 'en' ) ;
772
+
773
+ tick ( 100 ) ;
774
+ fixture . detectChanges ( ) ;
775
+
776
+ expect ( comp . caseField . isTranslated ) . toBe ( false ) ;
777
+ } ) ) ;
778
+
779
+ it ( 'should translate unsubstituted text first then apply substitutions' , ( ) => {
780
+ const label = 'English text with ${LabelA} placeholder' ;
781
+ const translatedTemplate = 'Welsh text with ${LabelA} placeholder' ;
782
+ const finalText = 'Welsh text with ValueA placeholder' ;
783
+
784
+ comp . caseField = textField ( 'LabelB' , '' , label ) ;
785
+ comp . caseFields = [ comp . caseField , textField ( 'LabelA' , 'ValueA' , '' ) ] ;
786
+
787
+ placeholderService . resolvePlaceholders . and . returnValues (
788
+ 'English text with ValueA placeholder' ,
789
+ finalText ,
790
+ '' ,
791
+ ''
792
+ ) ;
793
+ mockTranslationPipe . transform . and . returnValue ( translatedTemplate ) ;
794
+
795
+ fixture . detectChanges ( ) ;
796
+
797
+ expect ( mockTranslationPipe . transform ) . toHaveBeenCalledWith ( label ) ;
798
+ expect ( comp . caseField . label ) . toBe ( finalText ) ;
799
+ expect ( placeholderService . resolvePlaceholders ) . toHaveBeenCalledTimes ( 4 ) ;
800
+ expect ( placeholderService . resolvePlaceholders ) . toHaveBeenCalledWith (
801
+ jasmine . objectContaining ( { LabelA : 'ValueA' } ) ,
802
+ translatedTemplate
803
+ ) ;
804
+ } ) ;
805
+
806
+ it ( 'should handle hint_text during language changes' , fakeAsync ( ( ) => {
807
+ const initialHint = 'Initial hint with ${placeholder}' ;
808
+ comp . caseField = textField ( 'LabelB' , '' , '' , initialHint ) ;
809
+ comp . caseFields = [ comp . caseField , textField ( 'LabelA' , 'ValueA' , '' ) ] ;
810
+
811
+ placeholderService . resolvePlaceholders . and . callFake ( ( _fields : any , str : any ) => {
812
+ if ( str === initialHint ) return 'Initial hint with ValueA' ;
813
+ if ( str === '' ) return '' ;
814
+ return str ;
815
+ } ) ;
816
+ fixture . detectChanges ( ) ;
817
+
818
+ comp . caseField . hint_text = 'Modified hint' ;
819
+
820
+ placeholderService . resolvePlaceholders . calls . reset ( ) ;
821
+ placeholderService . resolvePlaceholders . and . callFake ( ( _fields : any , str : any ) => {
822
+ if ( str === initialHint ) return 'Initial hint with ValueA after change' ;
823
+ if ( str === '' ) return '' ;
824
+ return str ;
825
+ } ) ;
826
+
827
+ translationService . setLanguage ( 'en' ) ;
828
+
829
+ tick ( 100 ) ;
830
+ fixture . detectChanges ( ) ;
831
+
832
+ expect ( comp . caseField . hint_text ) . toBe ( 'Initial hint with ValueA after change' ) ;
833
+ expect ( hintEl . innerText ) . toBe ( 'Initial hint with ValueA after change' ) ;
834
+ } ) ) ;
835
+
836
+ it ( 'should not call translation when label has no placeholders' , ( ) => {
837
+ const label = 'Simple label without placeholders' ;
838
+ comp . caseField = textField ( 'LabelB' , '' , label ) ;
839
+ comp . caseFields = [ comp . caseField ] ;
840
+
841
+ placeholderService . resolvePlaceholders . and . returnValue ( label ) ;
842
+ fixture . detectChanges ( ) ;
843
+
844
+ expect ( mockTranslationPipe . transform ) . not . toHaveBeenCalled ( ) ;
845
+ expect ( comp . caseField . label ) . toBe ( label ) ;
846
+ expect ( comp . caseField . isTranslated ) . toBe ( false ) ;
847
+ } ) ;
848
+
849
+ it ( 'should handle language change timeout correctly' , fakeAsync ( ( ) => {
850
+ const label = 'Label with ${placeholder}' ;
851
+ comp . caseField = textField ( 'LabelB' , '' , label ) ;
852
+ comp . caseFields = [ comp . caseField , textField ( 'LabelA' , 'ValueA' , '' ) ] ;
853
+
854
+ placeholderService . resolvePlaceholders . and . returnValues ( 'Label with ValueA' , '' , '' ) ;
855
+ fixture . detectChanges ( ) ;
856
+
857
+ placeholderService . resolvePlaceholders . calls . reset ( ) ;
858
+
859
+ translationService . setLanguage ( 'cy' ) ;
860
+
861
+ expect ( placeholderService . resolvePlaceholders ) . not . toHaveBeenCalled ( ) ;
862
+
863
+ tick ( 50 ) ;
864
+ expect ( placeholderService . resolvePlaceholders ) . not . toHaveBeenCalled ( ) ;
865
+
866
+ tick ( 50 ) ;
867
+ fixture . detectChanges ( ) ;
868
+ expect ( placeholderService . resolvePlaceholders ) . toHaveBeenCalled ( ) ;
869
+ } ) ) ;
870
+
871
+ it ( 'should clean up language subscription on destroy' , ( ) => {
872
+ const label = 'Test label' ;
873
+ comp . caseField = textField ( 'LabelB' , '' , label ) ;
874
+ comp . caseFields = [ comp . caseField ] ;
875
+
876
+ placeholderService . resolvePlaceholders . and . returnValues ( label , '' , '' ) ;
877
+ fixture . detectChanges ( ) ;
878
+
879
+ const directiveEl = de . query ( By . directive ( LabelSubstitutorDirective ) ) ;
880
+ const directive = directiveEl . injector . get ( LabelSubstitutorDirective ) ;
881
+ spyOn ( directive [ 'languageSubscription' ] , 'unsubscribe' ) ;
882
+
883
+ fixture . destroy ( ) ;
884
+
885
+ expect ( directive [ 'languageSubscription' ] . unsubscribe ) . toHaveBeenCalled ( ) ;
886
+ } ) ;
887
+
888
+ it ( 'should restore initial values on destroy' , ( ) => {
889
+ const initialLabel = 'Initial label' ;
890
+ const initialHint = 'Initial hint' ;
891
+ comp . caseField = textField ( 'LabelB' , '' , initialLabel , initialHint ) ;
892
+ comp . caseFields = [ comp . caseField ] ;
893
+
894
+ placeholderService . resolvePlaceholders . and . returnValues ( 'Modified label' , 'Modified hint' , '' ) ;
895
+ fixture . detectChanges ( ) ;
896
+
897
+ comp . caseField . label = 'Changed label' ;
898
+ comp . caseField . hint_text = 'Changed hint' ;
899
+ comp . caseField . isTranslated = true ;
900
+
901
+ fixture . destroy ( ) ;
902
+
903
+ expect ( comp . caseField . label ) . toBe ( 'Modified label' ) ;
904
+ expect ( comp . caseField . hint_text ) . toBe ( initialHint ) ;
905
+ expect ( comp . caseField . isTranslated ) . toBe ( false ) ;
906
+ } ) ;
907
+ } ) ;
908
+ } ) ;
0 commit comments