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