88 MatButtonToggleChange ,
99 MatButtonToggleGroup ,
1010 MatButtonToggleGroupMultiple ,
11+ MatButtonToggle ,
12+ MatButtonToggleChange ,
1113 MatButtonToggleModule ,
1214} from './index' ;
1315
@@ -19,6 +21,7 @@ describe('MatButtonToggle with forms', () => {
1921 declarations : [
2022 ButtonToggleGroupWithNgModel ,
2123 ButtonToggleGroupWithFormControl ,
24+ ButtonToggleGroupMultipleWithFormControl ,
2225 ] ,
2326 } ) ;
2427
@@ -71,6 +74,46 @@ describe('MatButtonToggle with forms', () => {
7174 } ) ;
7275 } ) ;
7376
77+ describe ( 'multiple using FormControl' , ( ) => {
78+ let fixture : ComponentFixture < ButtonToggleGroupMultipleWithFormControl > ;
79+ let groupDebugElement : DebugElement ;
80+ let groupInstance : MatButtonToggleGroup ;
81+ let testComponent : ButtonToggleGroupMultipleWithFormControl ;
82+
83+ beforeEach ( fakeAsync ( ( ) => {
84+ fixture = TestBed . createComponent ( ButtonToggleGroupMultipleWithFormControl ) ;
85+ fixture . detectChanges ( ) ;
86+
87+ testComponent = fixture . debugElement . componentInstance ;
88+
89+ groupDebugElement = fixture . debugElement . query ( By . directive ( MatButtonToggleGroup ) ) ;
90+ groupInstance = groupDebugElement . injector . get < MatButtonToggleGroup > ( MatButtonToggleGroup ) ;
91+ } ) ) ;
92+
93+ it ( 'should initialize with falsy value' , ( ) => {
94+ expect ( groupInstance . value ) . toEqual ( [ 0 , 1 , 2 ] ) ;
95+ } ) ;
96+
97+ it ( 'should set the value' , ( ) => {
98+ testComponent . control . setValue ( [ 0 , 1 ] ) ;
99+
100+ expect ( groupInstance . value ) . toEqual ( [ 0 , 1 ] ) ;
101+
102+ testComponent . control . setValue ( [ 3 , 5 ] ) ;
103+
104+ expect ( groupInstance . value ) . toEqual ( [ 3 , 5 ] ) ;
105+ } ) ;
106+
107+ it ( 'should register the on change callback' , ( ) => {
108+ let spy = jasmine . createSpy ( 'onChange callback' ) ;
109+
110+ testComponent . control . registerOnChange ( spy ) ;
111+ testComponent . control . setValue ( [ 0 , 1 ] ) ;
112+
113+ expect ( spy ) . toHaveBeenCalled ( ) ;
114+ } ) ;
115+ } ) ;
116+
74117 describe ( 'button toggle group with ngModel and change event' , ( ) => {
75118 let fixture : ComponentFixture < ButtonToggleGroupWithNgModel > ;
76119 let groupDebugElement : DebugElement ;
@@ -748,6 +791,32 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
748791 isVertical : boolean = false ;
749792}
750793
794+ @Component ( {
795+ template : `
796+ <mat-button-toggle-group [formControl]="control" multiple>
797+ <mat-button-toggle *ngFor="let option of options" [value]="option.value">
798+ {{option.label}}
799+ </mat-button-toggle>
800+ </mat-button-toggle-group>
801+ `
802+ } )
803+ class ButtonToggleGroupMultipleWithFormControl implements OnInit {
804+ control = new FormControl ( [ ] ) ;
805+ options = [
806+ { value : 0 , label : 'Sunday' } ,
807+ { value : 1 , label : 'Monday' } ,
808+ { value : 2 , label : 'Tuesday' } ,
809+ { value : 3 , label : 'Wednesday' } ,
810+ { value : 4 , label : 'Thursday' } ,
811+ { value : 5 , label : 'Friday' } ,
812+ { value : 6 , label : 'Saturday' } ,
813+ ] ;
814+
815+ ngOnInit ( ) : void {
816+ this . control . patchValue ( [ 0 , 1 , 2 ] ) ;
817+ }
818+ }
819+
751820@Component ( {
752821 template : `
753822 <mat-button-toggle>Yes</mat-button-toggle>
0 commit comments