@@ -93,7 +93,7 @@ describe('MatButtonToggle with forms', () => {
93
93
buttonToggleDebugElements = fixture . debugElement . queryAll ( By . directive ( MatButtonToggle ) ) ;
94
94
buttonToggleInstances = buttonToggleDebugElements . map ( debugEl => debugEl . componentInstance ) ;
95
95
buttonToggleLabels = buttonToggleDebugElements . map (
96
- debugEl => debugEl . query ( By . css ( 'label ' ) ) . nativeElement ) ;
96
+ debugEl => debugEl . query ( By . css ( 'button ' ) ) . nativeElement ) ;
97
97
98
98
fixture . detectChanges ( ) ;
99
99
} ) ) ;
@@ -243,7 +243,7 @@ describe('MatButtonToggle without forms', () => {
243
243
buttonToggleNativeElements = buttonToggleDebugElements
244
244
. map ( debugEl => debugEl . nativeElement ) ;
245
245
246
- buttonToggleLabelElements = fixture . debugElement . queryAll ( By . css ( 'label ' ) )
246
+ buttonToggleLabelElements = fixture . debugElement . queryAll ( By . css ( 'button ' ) )
247
247
. map ( debugEl => debugEl . nativeElement ) ;
248
248
249
249
buttonToggleInstances = buttonToggleDebugElements . map ( debugEl => debugEl . componentInstance ) ;
@@ -338,7 +338,7 @@ describe('MatButtonToggle without forms', () => {
338
338
buttonToggleLabelElements [ 0 ] . click ( ) ;
339
339
fixture . detectChanges ( ) ;
340
340
tick ( ) ;
341
- expect ( changeSpy ) . toHaveBeenCalled ( ) ;
341
+ expect ( changeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
342
342
343
343
buttonToggleLabelElements [ 0 ] . click ( ) ;
344
344
fixture . detectChanges ( ) ;
@@ -446,7 +446,7 @@ describe('MatButtonToggle without forms', () => {
446
446
buttonToggleDebugElements = fixture . debugElement . queryAll ( By . directive ( MatButtonToggle ) ) ;
447
447
buttonToggleNativeElements = buttonToggleDebugElements
448
448
. map ( debugEl => debugEl . nativeElement ) ;
449
- buttonToggleLabelElements = fixture . debugElement . queryAll ( By . css ( 'label ' ) )
449
+ buttonToggleLabelElements = fixture . debugElement . queryAll ( By . css ( 'button ' ) )
450
450
. map ( debugEl => debugEl . nativeElement ) ;
451
451
buttonToggleInstances = buttonToggleDebugElements . map ( debugEl => debugEl . componentInstance ) ;
452
452
} ) ) ;
@@ -462,7 +462,7 @@ describe('MatButtonToggle without forms', () => {
462
462
it ( 'should check a button toggle when clicked' , ( ) => {
463
463
expect ( buttonToggleInstances . every ( buttonToggle => ! buttonToggle . checked ) ) . toBe ( true ) ;
464
464
465
- let nativeCheckboxLabel = buttonToggleDebugElements [ 0 ] . query ( By . css ( 'label ' ) ) . nativeElement ;
465
+ let nativeCheckboxLabel = buttonToggleDebugElements [ 0 ] . query ( By . css ( 'button ' ) ) . nativeElement ;
466
466
467
467
nativeCheckboxLabel . click ( ) ;
468
468
@@ -486,9 +486,9 @@ describe('MatButtonToggle without forms', () => {
486
486
} ) ;
487
487
488
488
it ( 'should check a button toggle upon interaction with underlying native checkbox' , ( ) => {
489
- let nativeCheckboxInput = buttonToggleDebugElements [ 0 ] . query ( By . css ( 'input ' ) ) . nativeElement ;
489
+ let nativeCheckboxButton = buttonToggleDebugElements [ 0 ] . query ( By . css ( 'button ' ) ) . nativeElement ;
490
490
491
- nativeCheckboxInput . click ( ) ;
491
+ nativeCheckboxButton . click ( ) ;
492
492
fixture . detectChanges ( ) ;
493
493
494
494
expect ( groupInstance . value ) . toEqual ( [ 'eggs' ] ) ;
@@ -561,15 +561,19 @@ describe('MatButtonToggle without forms', () => {
561
561
let buttonToggleNativeElement : HTMLElement ;
562
562
let buttonToggleLabelElement : HTMLLabelElement ;
563
563
let buttonToggleInstance : MatButtonToggle ;
564
+ let buttonToggleButtonElement : HTMLButtonElement ;
564
565
565
566
beforeEach ( fakeAsync ( ( ) => {
566
567
fixture = TestBed . createComponent ( StandaloneButtonToggle ) ;
567
568
fixture . detectChanges ( ) ;
568
569
569
570
buttonToggleDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
570
571
buttonToggleNativeElement = buttonToggleDebugElement . nativeElement ;
571
- buttonToggleLabelElement = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
572
+ buttonToggleLabelElement = fixture . debugElement
573
+ . query ( By . css ( '.mat-button-toggle-label-content' ) ) . nativeElement ;
572
574
buttonToggleInstance = buttonToggleDebugElement . componentInstance ;
575
+ buttonToggleButtonElement =
576
+ buttonToggleNativeElement . querySelector ( 'button' ) ! as HTMLButtonElement ;
573
577
} ) ) ;
574
578
575
579
it ( 'should toggle when clicked' , fakeAsync ( ( ) => {
@@ -607,66 +611,77 @@ describe('MatButtonToggle without forms', () => {
607
611
} ) ) ;
608
612
609
613
it ( 'should focus on underlying input element when focus() is called' , ( ) => {
610
- let nativeRadioInput = buttonToggleDebugElement . query ( By . css ( 'input ' ) ) . nativeElement ;
611
- expect ( document . activeElement ) . not . toBe ( nativeRadioInput ) ;
614
+ let nativeButton = buttonToggleDebugElement . query ( By . css ( 'button ' ) ) . nativeElement ;
615
+ expect ( document . activeElement ) . not . toBe ( nativeButton ) ;
612
616
613
617
buttonToggleInstance . focus ( ) ;
614
618
fixture . detectChanges ( ) ;
615
619
616
- expect ( document . activeElement ) . toBe ( nativeRadioInput ) ;
620
+ expect ( document . activeElement ) . toBe ( nativeButton ) ;
617
621
} ) ;
618
622
619
623
it ( 'should not assign a name to the underlying input if one is not passed in' , ( ) => {
620
- expect ( buttonToggleNativeElement . querySelector ( 'input' ) ! . getAttribute ( 'name' ) ) . toBeFalsy ( ) ;
624
+ expect ( buttonToggleButtonElement . getAttribute ( 'name' ) ) . toBeFalsy ( ) ;
621
625
} ) ;
622
626
627
+ it ( 'should have correct aria-pressed attribute' , ( ) => {
628
+ expect ( buttonToggleButtonElement . getAttribute ( 'aria-pressed' ) )
629
+ . toBe ( 'false' ) ;
630
+
631
+ buttonToggleLabelElement . click ( ) ;
632
+
633
+ fixture . detectChanges ( ) ;
634
+
635
+ expect ( buttonToggleButtonElement . getAttribute ( 'aria-pressed' ) )
636
+ . toBe ( 'true' ) ;
637
+ } )
623
638
} ) ;
624
639
625
640
describe ( 'aria-label handling ' , ( ) => {
626
641
it ( 'should not set the aria-label attribute if none is provided' , ( ) => {
627
642
let fixture = TestBed . createComponent ( StandaloneButtonToggle ) ;
628
643
let checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
629
644
let checkboxNativeElement = checkboxDebugElement . nativeElement ;
630
- let inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
645
+ let buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
631
646
632
647
fixture . detectChanges ( ) ;
633
- expect ( inputElement . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
648
+ expect ( buttonElement . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
634
649
} ) ;
635
650
636
651
it ( 'should use the provided aria-label' , ( ) => {
637
652
let fixture = TestBed . createComponent ( ButtonToggleWithAriaLabel ) ;
638
653
let checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
639
654
let checkboxNativeElement = checkboxDebugElement . nativeElement ;
640
- let inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
655
+ let buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
641
656
642
657
fixture . detectChanges ( ) ;
643
- expect ( inputElement . getAttribute ( 'aria-label' ) ) . toBe ( 'Super effective' ) ;
658
+ expect ( buttonElement . getAttribute ( 'aria-label' ) ) . toBe ( 'Super effective' ) ;
644
659
} ) ;
645
660
} ) ;
646
661
647
662
describe ( 'with provided aria-labelledby ' , ( ) => {
648
663
let checkboxDebugElement : DebugElement ;
649
664
let checkboxNativeElement : HTMLElement ;
650
- let inputElement : HTMLInputElement ;
665
+ let buttonElement : HTMLButtonElement ;
651
666
652
667
it ( 'should use the provided aria-labelledby' , ( ) => {
653
668
let fixture = TestBed . createComponent ( ButtonToggleWithAriaLabelledby ) ;
654
669
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
655
670
checkboxNativeElement = checkboxDebugElement . nativeElement ;
656
- inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
671
+ buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
657
672
658
673
fixture . detectChanges ( ) ;
659
- expect ( inputElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( 'some-id' ) ;
674
+ expect ( buttonElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( 'some-id' ) ;
660
675
} ) ;
661
676
662
677
it ( 'should not assign aria-labelledby if none is provided' , ( ) => {
663
678
let fixture = TestBed . createComponent ( StandaloneButtonToggle ) ;
664
679
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
665
680
checkboxNativeElement = checkboxDebugElement . nativeElement ;
666
- inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
681
+ buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
667
682
668
683
fixture . detectChanges ( ) ;
669
- expect ( inputElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( null ) ;
684
+ expect ( buttonElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( null ) ;
670
685
} ) ;
671
686
} ) ;
672
687
0 commit comments