@@ -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 ( ( ) => {
@@ -608,66 +612,77 @@ describe('MatButtonToggle without forms', () => {
608
612
} ) ) ;
609
613
610
614
it ( 'should focus on underlying input element when focus() is called' , ( ) => {
611
- let nativeRadioInput = buttonToggleDebugElement . query ( By . css ( 'input ' ) ) . nativeElement ;
612
- expect ( document . activeElement ) . not . toBe ( nativeRadioInput ) ;
615
+ let nativeButton = buttonToggleDebugElement . query ( By . css ( 'button ' ) ) . nativeElement ;
616
+ expect ( document . activeElement ) . not . toBe ( nativeButton ) ;
613
617
614
618
buttonToggleInstance . focus ( ) ;
615
619
fixture . detectChanges ( ) ;
616
620
617
- expect ( document . activeElement ) . toBe ( nativeRadioInput ) ;
621
+ expect ( document . activeElement ) . toBe ( nativeButton ) ;
618
622
} ) ;
619
623
620
624
it ( 'should not assign a name to the underlying input if one is not passed in' , ( ) => {
621
- expect ( buttonToggleNativeElement . querySelector ( 'input' ) ! . getAttribute ( 'name' ) ) . toBeFalsy ( ) ;
625
+ expect ( buttonToggleButtonElement . getAttribute ( 'name' ) ) . toBeFalsy ( ) ;
622
626
} ) ;
623
627
628
+ it ( 'should have correct aria-pressed attribute' , ( ) => {
629
+ expect ( buttonToggleButtonElement . getAttribute ( 'aria-pressed' ) )
630
+ . toBe ( 'false' ) ;
631
+
632
+ buttonToggleLabelElement . click ( ) ;
633
+
634
+ fixture . detectChanges ( ) ;
635
+
636
+ expect ( buttonToggleButtonElement . getAttribute ( 'aria-pressed' ) )
637
+ . toBe ( 'true' ) ;
638
+ } ) ;
624
639
} ) ;
625
640
626
641
describe ( 'aria-label handling ' , ( ) => {
627
642
it ( 'should not set the aria-label attribute if none is provided' , ( ) => {
628
643
let fixture = TestBed . createComponent ( StandaloneButtonToggle ) ;
629
644
let checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
630
645
let checkboxNativeElement = checkboxDebugElement . nativeElement ;
631
- let inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
646
+ let buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
632
647
633
648
fixture . detectChanges ( ) ;
634
- expect ( inputElement . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
649
+ expect ( buttonElement . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
635
650
} ) ;
636
651
637
652
it ( 'should use the provided aria-label' , ( ) => {
638
653
let fixture = TestBed . createComponent ( ButtonToggleWithAriaLabel ) ;
639
654
let checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
640
655
let checkboxNativeElement = checkboxDebugElement . nativeElement ;
641
- let inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
656
+ let buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
642
657
643
658
fixture . detectChanges ( ) ;
644
- expect ( inputElement . getAttribute ( 'aria-label' ) ) . toBe ( 'Super effective' ) ;
659
+ expect ( buttonElement . getAttribute ( 'aria-label' ) ) . toBe ( 'Super effective' ) ;
645
660
} ) ;
646
661
} ) ;
647
662
648
663
describe ( 'with provided aria-labelledby ' , ( ) => {
649
664
let checkboxDebugElement : DebugElement ;
650
665
let checkboxNativeElement : HTMLElement ;
651
- let inputElement : HTMLInputElement ;
666
+ let buttonElement : HTMLButtonElement ;
652
667
653
668
it ( 'should use the provided aria-labelledby' , ( ) => {
654
669
let fixture = TestBed . createComponent ( ButtonToggleWithAriaLabelledby ) ;
655
670
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
656
671
checkboxNativeElement = checkboxDebugElement . nativeElement ;
657
- inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
672
+ buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
658
673
659
674
fixture . detectChanges ( ) ;
660
- expect ( inputElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( 'some-id' ) ;
675
+ expect ( buttonElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( 'some-id' ) ;
661
676
} ) ;
662
677
663
678
it ( 'should not assign aria-labelledby if none is provided' , ( ) => {
664
679
let fixture = TestBed . createComponent ( StandaloneButtonToggle ) ;
665
680
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggle ) ) ;
666
681
checkboxNativeElement = checkboxDebugElement . nativeElement ;
667
- inputElement = checkboxNativeElement . querySelector ( 'input ' ) as HTMLInputElement ;
682
+ buttonElement = checkboxNativeElement . querySelector ( 'button ' ) as HTMLButtonElement ;
668
683
669
684
fixture . detectChanges ( ) ;
670
- expect ( inputElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( null ) ;
685
+ expect ( buttonElement . getAttribute ( 'aria-labelledby' ) ) . toBe ( null ) ;
671
686
} ) ;
672
687
} ) ;
673
688
0 commit comments