11import { dispatchMouseEvent } from '@angular/cdk/testing' ;
22import { Component , DebugElement , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
3+ import { CommonModule } from '@angular/common' ;
34import { ComponentFixture , fakeAsync , flush , TestBed , tick } from '@angular/core/testing' ;
45import { FormControl , FormsModule , NgModel , ReactiveFormsModule } from '@angular/forms' ;
56import { By } from '@angular/platform-browser' ;
@@ -15,10 +16,11 @@ describe('MatButtonToggle with forms', () => {
1516
1617 beforeEach ( fakeAsync ( ( ) => {
1718 TestBed . configureTestingModule ( {
18- imports : [ MatButtonToggleModule , FormsModule , ReactiveFormsModule ] ,
19+ imports : [ MatButtonToggleModule , FormsModule , ReactiveFormsModule , CommonModule ] ,
1920 declarations : [
2021 ButtonToggleGroupWithNgModel ,
2122 ButtonToggleGroupWithFormControl ,
23+ ButtonToggleGroupWithIndirectDescendantToggles ,
2224 ] ,
2325 } ) ;
2426
@@ -232,6 +234,24 @@ describe('MatButtonToggle with forms', () => {
232234 } ) ) ;
233235
234236 } ) ;
237+
238+ it ( 'should be able to pick up toggles that are not direct descendants' , fakeAsync ( ( ) => {
239+ const fixture = TestBed . createComponent ( ButtonToggleGroupWithIndirectDescendantToggles ) ;
240+ fixture . detectChanges ( ) ;
241+
242+ const button = fixture . nativeElement . querySelector ( '.mat-button-toggle button' ) ;
243+ const groupDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggleGroup ) ) ! ;
244+ const groupInstance =
245+ groupDebugElement . injector . get < MatButtonToggleGroup > ( MatButtonToggleGroup ) ;
246+
247+ button . click ( ) ;
248+ fixture . detectChanges ( ) ;
249+ tick ( ) ;
250+
251+ expect ( groupInstance . value ) . toBe ( 'red' ) ;
252+ expect ( fixture . componentInstance . control . value ) . toBe ( 'red' ) ;
253+ expect ( groupInstance . _buttonToggles . length ) . toBe ( 3 ) ;
254+ } ) ) ;
235255} ) ;
236256
237257describe ( 'MatButtonToggle without forms' , ( ) => {
@@ -966,6 +986,22 @@ class ButtonToggleGroupWithFormControl {
966986 control = new FormControl ( ) ;
967987}
968988
989+ @Component ( {
990+ // We need the `ngSwitch` so that there's a directive between the group and the toggles.
991+ template : `
992+ <mat-button-toggle-group [formControl]="control">
993+ <ng-container [ngSwitch]="true">
994+ <mat-button-toggle value="red">Value Red</mat-button-toggle>
995+ <mat-button-toggle value="green">Value Green</mat-button-toggle>
996+ <mat-button-toggle value="blue">Value Blue</mat-button-toggle>
997+ </ng-container>
998+ </mat-button-toggle-group>
999+ `
1000+ } )
1001+ class ButtonToggleGroupWithIndirectDescendantToggles {
1002+ control = new FormControl ( ) ;
1003+ }
1004+
9691005/** Simple test component with an aria-label set. */
9701006@Component ( {
9711007 template : `<mat-button-toggle aria-label="Super effective"></mat-button-toggle>`
0 commit comments