11import { dispatchMouseEvent } from '@angular/cdk/testing' ;
2- import { Component , DebugElement , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
2+ import { Component , DebugElement , OnInit , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
33import { ComponentFixture , fakeAsync , flush , TestBed , tick } from '@angular/core/testing' ;
44import { FormControl , FormsModule , NgModel , ReactiveFormsModule } from '@angular/forms' ;
55import { By } from '@angular/platform-browser' ;
@@ -19,6 +19,7 @@ describe('MatButtonToggle with forms', () => {
1919 declarations : [
2020 ButtonToggleGroupWithNgModel ,
2121 ButtonToggleGroupWithFormControl ,
22+ ButtonToggleGroupMultipleWithFormControl ,
2223 ] ,
2324 } ) ;
2425
@@ -71,6 +72,46 @@ describe('MatButtonToggle with forms', () => {
7172 } ) ;
7273 } ) ;
7374
75+ describe ( 'multiple using FormControl' , ( ) => {
76+ let fixture : ComponentFixture < ButtonToggleGroupMultipleWithFormControl > ;
77+ let groupDebugElement : DebugElement ;
78+ let groupInstance : MatButtonToggleGroup ;
79+ let testComponent : ButtonToggleGroupMultipleWithFormControl ;
80+
81+ beforeEach ( fakeAsync ( ( ) => {
82+ fixture = TestBed . createComponent ( ButtonToggleGroupMultipleWithFormControl ) ;
83+ fixture . detectChanges ( ) ;
84+
85+ testComponent = fixture . debugElement . componentInstance ;
86+
87+ groupDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggleGroup ) ) ;
88+ groupInstance = groupDebugElement . injector . get < MatButtonToggleGroup > ( MatButtonToggleGroup ) ;
89+ } ) ) ;
90+
91+ it ( 'should initialize with falsy value' , ( ) => {
92+ expect ( groupInstance . value ) . toEqual ( [ 0 , 1 , 2 ] ) ;
93+ } ) ;
94+
95+ it ( 'should set the value' , ( ) => {
96+ testComponent . control . setValue ( [ 0 , 1 ] ) ;
97+
98+ expect ( groupInstance . value ) . toEqual ( [ 0 , 1 ] ) ;
99+
100+ testComponent . control . setValue ( [ 3 , 5 ] ) ;
101+
102+ expect ( groupInstance . value ) . toEqual ( [ 3 , 5 ] ) ;
103+ } ) ;
104+
105+ it ( 'should register the on change callback' , ( ) => {
106+ let spy = jasmine . createSpy ( 'onChange callback' ) ;
107+
108+ testComponent . control . registerOnChange ( spy ) ;
109+ testComponent . control . setValue ( [ 0 , 1 ] ) ;
110+
111+ expect ( spy ) . toHaveBeenCalled ( ) ;
112+ } ) ;
113+ } ) ;
114+
74115 describe ( 'button toggle group with ngModel and change event' , ( ) => {
75116 let fixture : ComponentFixture < ButtonToggleGroupWithNgModel > ;
76117 let groupDebugElement : DebugElement ;
@@ -748,6 +789,32 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
748789 isVertical : boolean = false ;
749790}
750791
792+ @Component ( {
793+ template : `
794+ <mat-button-toggle-group [formControl]="control" multiple>
795+ <mat-button-toggle *ngFor="let option of options" [value]="option.value">
796+ {{option.label}}
797+ </mat-button-toggle>
798+ </mat-button-toggle-group>
799+ `
800+ } )
801+ class ButtonToggleGroupMultipleWithFormControl implements OnInit {
802+ control = new FormControl ( [ ] ) ;
803+ options = [
804+ { value : 0 , label : 'Sunday' } ,
805+ { value : 1 , label : 'Monday' } ,
806+ { value : 2 , label : 'Tuesday' } ,
807+ { value : 3 , label : 'Wednesday' } ,
808+ { value : 4 , label : 'Thursday' } ,
809+ { value : 5 , label : 'Friday' } ,
810+ { value : 6 , label : 'Saturday' } ,
811+ ] ;
812+
813+ ngOnInit ( ) : void {
814+ this . control . patchValue ( [ 0 , 1 , 2 ] ) ;
815+ }
816+ }
817+
751818@Component ( {
752819 template : `
753820 <mat-button-toggle>Yes</mat-button-toggle>
0 commit comments