@@ -6,8 +6,6 @@ import { runValueAccessorTests } from 'ngx-cva-test-suite';
66import { SegmentedButtonComponent } from './segmented-button.component' ;
77import { SegmentedButtonModule } from './segmented-button.module' ;
88
9- const isSelectedClass = 'fd-button--toggled' ;
10-
119@Component ( {
1210 selector : 'fd-test-component' ,
1311 template : `
@@ -50,16 +48,46 @@ describe('SegmentedButtonComponent', () => {
5048 expect ( component ) . toBeTruthy ( ) ;
5149 } ) ;
5250
51+ it ( 'should set segmented button group classes and attributes correctly' , ( ) => {
52+ const segmentedButtonElement = fixture . debugElement . nativeElement . querySelector ( 'fd-segmented-button' ) ;
53+ expect ( segmentedButtonElement . classList ) . toContain ( 'fd-segmented-button' ) ;
54+ expect ( segmentedButtonElement . classList ) . not . toContain ( 'fd-segmented-button--vertical' ) ;
55+
56+ expect ( segmentedButtonElement . getAttribute ( 'role' ) ) . toBe ( 'listbox' ) ;
57+ expect ( segmentedButtonElement . getAttribute ( 'aria-multiselectable' ) ) . toBe ( 'false' ) ;
58+ expect ( segmentedButtonElement . getAttribute ( 'aria-orientation' ) ) . toBe ( 'horizontal' ) ;
59+ expect ( segmentedButtonElement . getAttribute ( 'aria-roledescription' ) ) . toBe ( 'Segmented Button Group' ) ;
60+ } ) ;
61+
62+ it ( 'should set button attributes correctly' , ( ) => {
63+ expect ( component . firstButton . nativeElement . getAttribute ( 'role' ) ) . toBe ( 'option' ) ;
64+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-roledescription' ) ) . toBe ( 'Segmented Button' ) ;
65+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-posinset' ) ) . toBe ( '1' ) ;
66+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-setsize' ) ) . toBe ( '3' ) ;
67+
68+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'role' ) ) . toBe ( 'option' ) ;
69+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-roledescription' ) ) . toBe (
70+ 'Segmented Button'
71+ ) ;
72+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-posinset' ) ) . toBe ( '2' ) ;
73+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-setsize' ) ) . toBe ( '3' ) ;
74+
75+ expect ( component . thirdButton . nativeElement . getAttribute ( 'role' ) ) . toBe ( 'option' ) ;
76+ expect ( component . thirdButton . nativeElement . getAttribute ( 'aria-roledescription' ) ) . toBe ( 'Segmented Button' ) ;
77+ expect ( component . thirdButton . nativeElement . getAttribute ( 'aria-posinset' ) ) . toBe ( '3' ) ;
78+ expect ( component . thirdButton . nativeElement . getAttribute ( 'aria-setsize' ) ) . toBe ( '3' ) ;
79+ } ) ;
80+
5381 // Default Example
5482 it ( 'should correctly select and deselect single value in non-toggle mode' , ( ) => {
5583 component . segmentedButton . writeValue ( 'second' ) ;
5684 fixture . detectChanges ( ) ;
57- expect ( component . secondButton . elementRef . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
85+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
5886
5987 component . segmentedButton . writeValue ( 'first' ) ;
6088 fixture . detectChanges ( ) ;
61- expect ( component . firstButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
62- expect ( component . secondButton . elementRef . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( false ) ;
89+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
90+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' false' ) ;
6391 } ) ;
6492
6593 // Toggle Example
@@ -69,31 +97,31 @@ describe('SegmentedButtonComponent', () => {
6997
7098 component . firstButton . nativeElement . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
7199 fixture . detectChanges ( ) ;
72- expect ( component . firstButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
100+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
73101
74102 component . secondButton . elementRef . nativeElement . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
75103 fixture . detectChanges ( ) ;
76- expect ( component . secondButton . elementRef . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
104+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
77105
78106 component . thirdButton . nativeElement . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
79107 fixture . detectChanges ( ) ;
80- expect ( component . thirdButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
108+ expect ( component . thirdButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
81109
82110 // Deselect
83111 component . firstButton . nativeElement . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
84112 fixture . detectChanges ( ) ;
85- expect ( component . firstButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( false ) ;
113+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' false' ) ;
86114 } ) ;
87115
88116 // Form Example
89117 it ( 'should update form value correctly' , ( ) => {
90118 component . segmentedButton . writeValue ( 'first' ) ;
91119 fixture . detectChanges ( ) ;
92- expect ( component . firstButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
120+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
93121
94122 component . segmentedButton . writeValue ( 'second' ) ;
95123 fixture . detectChanges ( ) ;
96- expect ( component . secondButton . elementRef . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
124+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
97125 } ) ;
98126
99127 // Disabled State Check
@@ -120,15 +148,15 @@ describe('SegmentedButtonComponent', () => {
120148
121149 component . firstButton . nativeElement . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
122150 fixture . detectChanges ( ) ;
123- expect ( component . firstButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
151+ expect ( component . firstButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
124152
125153 component . secondButton . elementRef . nativeElement . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'Enter' } ) ) ;
126154 fixture . detectChanges ( ) ;
127- expect ( component . secondButton . elementRef . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
155+ expect ( component . secondButton . elementRef . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
128156
129157 component . thirdButton . nativeElement . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : ' ' } ) ) ;
130158 fixture . detectChanges ( ) ;
131- expect ( component . thirdButton . nativeElement . classList . contains ( isSelectedClass ) ) . toBe ( true ) ;
159+ expect ( component . thirdButton . nativeElement . getAttribute ( 'aria-selected' ) ) . toBe ( ' true' ) ;
132160 } ) ;
133161} ) ;
134162
0 commit comments