8
8
MatButtonToggleChange ,
9
9
MatButtonToggleGroup ,
10
10
MatButtonToggleGroupMultiple ,
11
+ MatButtonToggle ,
12
+ MatButtonToggleChange ,
11
13
MatButtonToggleModule ,
12
14
} from './index' ;
13
15
@@ -19,6 +21,7 @@ describe('MatButtonToggle with forms', () => {
19
21
declarations : [
20
22
ButtonToggleGroupWithNgModel ,
21
23
ButtonToggleGroupWithFormControl ,
24
+ ButtonToggleGroupMultipleWithFormControl ,
22
25
] ,
23
26
} ) ;
24
27
@@ -71,6 +74,46 @@ describe('MatButtonToggle with forms', () => {
71
74
} ) ;
72
75
} ) ;
73
76
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
+
74
117
describe ( 'button toggle group with ngModel and change event' , ( ) => {
75
118
let fixture : ComponentFixture < ButtonToggleGroupWithNgModel > ;
76
119
let groupDebugElement : DebugElement ;
@@ -748,6 +791,32 @@ class ButtonTogglesInsideButtonToggleGroupMultiple {
748
791
isVertical : boolean = false ;
749
792
}
750
793
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
+
751
820
@Component ( {
752
821
template : `
753
822
<mat-button-toggle>Yes</mat-button-toggle>
0 commit comments