@@ -19,6 +19,7 @@ describe('MatButtonToggle with forms', () => {
19
19
declarations : [
20
20
ButtonToggleGroupWithNgModel ,
21
21
ButtonToggleGroupWithFormControl ,
22
+ ButtonToggleGroupMultipleWithFormControl ,
22
23
] ,
23
24
} ) ;
24
25
@@ -71,6 +72,46 @@ describe('MatButtonToggle with forms', () => {
71
72
} ) ;
72
73
} ) ;
73
74
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
+
74
115
describe ( 'button toggle group with ngModel and change event' , ( ) => {
75
116
let fixture : ComponentFixture < ButtonToggleGroupWithNgModel > ;
76
117
let groupDebugElement : DebugElement ;
@@ -748,6 +789,32 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
748
789
isVertical : boolean = false ;
749
790
}
750
791
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
+
751
818
@Component ( {
752
819
template : `
753
820
<mat-button-toggle>Yes</mat-button-toggle>
0 commit comments