11import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2-
3- import { SwitchComponent } from './switch.component' ;
2+ import { SwitchColor , SwitchComponent , SwitchOption } from './switch.component' ;
3+ import { By } from '@angular/platform-browser' ;
4+ import { TranslateLoader , TranslateModule } from '@ngx-translate/core' ;
5+ import { TranslateLoaderMock } from '../mocks/translate-loader.mock' ;
46
57describe ( 'SwitchComponent' , ( ) => {
68 let component : SwitchComponent ;
79 let fixture : ComponentFixture < SwitchComponent > ;
10+ const mockOptions : SwitchOption [ ] = [
11+ { value : 1 , icon : 'icon-1' , label : 'Option 1' , backgroundColor : SwitchColor . Success , iconColor : SwitchColor . Primary , labelColor : SwitchColor . Primary } ,
12+ { value : 2 , icon : 'icon-2' , label : 'Option 2' , backgroundColor : SwitchColor . Danger , iconColor : SwitchColor . Warning , labelColor : SwitchColor . Success } ,
13+ ] ;
814
915 beforeEach ( async ( ) => {
1016 await TestBed . configureTestingModule ( {
11- declarations : [ SwitchComponent ]
17+ declarations : [ SwitchComponent ] ,
18+ imports : [
19+ TranslateModule . forRoot ( {
20+ loader : {
21+ provide : TranslateLoader ,
22+ useClass : TranslateLoaderMock
23+ }
24+ } )
25+ ] ,
1226 } )
1327 . compileComponents ( ) ;
1428
@@ -20,4 +34,65 @@ describe('SwitchComponent', () => {
2034 it ( 'should create' , ( ) => {
2135 expect ( component ) . toBeTruthy ( ) ;
2236 } ) ;
37+
38+ it ( 'should render all switch options' , ( ) => {
39+ component . options = mockOptions ;
40+ fixture . detectChanges ( ) ;
41+
42+ const optionElements = fixture . debugElement . queryAll ( By . css ( '.switch-opt' ) ) ;
43+ expect ( optionElements . length ) . toBe ( mockOptions . length ) ;
44+ } ) ;
45+
46+ it ( 'should select an option and emit selected value' , ( ) => {
47+ component . options = mockOptions ;
48+ component . selectedValue = mockOptions [ 0 ] . value ;
49+ fixture . detectChanges ( ) ;
50+
51+ spyOn ( component . selectedValueChange , 'emit' ) ;
52+
53+ const secondOption = fixture . debugElement . queryAll ( By . css ( '.switch-opt' ) ) [ 1 ] ;
54+ secondOption . triggerEventHandler ( 'click' ) ;
55+ fixture . detectChanges ( ) ;
56+
57+ expect ( component . selectedValue ) . toBe ( mockOptions [ 1 ] . value ) ;
58+ expect ( component . selectedValueChange . emit ) . toHaveBeenCalledWith ( mockOptions [ 1 ] . value ) ;
59+ } ) ;
60+
61+ it ( 'should apply the correct background color class' , ( ) => {
62+ component . options = mockOptions ;
63+ component . selectedValue = mockOptions [ 1 ] . value ;
64+ fixture . detectChanges ( ) ;
65+
66+ const containerElement = fixture . debugElement . query ( By . css ( '.switch-container' ) ) ;
67+ expect ( containerElement . classes [ 'bg-danger' ] ) . toBeTruthy ( ) ;
68+ } ) ;
69+
70+ it ( 'should apply the correct icon color class for selected option' , ( ) => {
71+ component . options = mockOptions ;
72+ component . selectedValue = mockOptions [ 1 ] . value ;
73+ fixture . detectChanges ( ) ;
74+
75+ const iconElement = fixture . debugElement . query ( By . css ( '.switch-opt .icon-2' ) ) ;
76+ expect ( iconElement . classes [ 'text-warning' ] ) . toBeTruthy ( ) ;
77+ } ) ;
78+
79+ it ( 'should display the correct label with the selected color' , ( ) => {
80+ component . options = mockOptions ;
81+ component . selectedValue = mockOptions [ 1 ] . value ;
82+ fixture . detectChanges ( ) ;
83+
84+ const labelElement = fixture . debugElement . query ( By . css ( '.visibility-label' ) ) ;
85+ expect ( labelElement . nativeElement . textContent . trim ( ) ) . toBe ( 'Option 2' ) ;
86+ expect ( labelElement . classes [ 'text-success' ] ) . toBeTruthy ( ) ;
87+ } ) ;
88+
89+ it ( 'should apply bg-white class to selected option' , ( ) => {
90+ component . options = mockOptions ;
91+ component . selectedValue = mockOptions [ 1 ] . value ;
92+ fixture . detectChanges ( ) ;
93+
94+ const selectedOptionElement = fixture . debugElement . query ( By . css ( '.switch-opt.bg-white' ) ) ;
95+ expect ( selectedOptionElement ) . toBeTruthy ( ) ;
96+ } ) ;
97+
2398} ) ;
0 commit comments