55 ComponentFixture ,
66 TestBed ,
77} from '@angular/core/testing' ;
8- import { NgControl , FormsModule } from '@angular/forms' ;
8+ import { NgControl , FormsModule , ReactiveFormsModule , FormControl } from '@angular/forms' ;
99import { Component , DebugElement } from '@angular/core' ;
1010import { By } from '@angular/platform-browser' ;
1111import {
@@ -20,12 +20,13 @@ describe('MdButtonToggle', () => {
2020
2121 beforeEach ( async ( ( ) => {
2222 TestBed . configureTestingModule ( {
23- imports : [ MdButtonToggleModule . forRoot ( ) , FormsModule ] ,
23+ imports : [ MdButtonToggleModule . forRoot ( ) , FormsModule , ReactiveFormsModule ] ,
2424 declarations : [
2525 ButtonTogglesInsideButtonToggleGroup ,
2626 ButtonToggleGroupWithNgModel ,
2727 ButtonTogglesInsideButtonToggleGroupMultiple ,
2828 ButtonToggleGroupWithInitialValue ,
29+ ButtonToggleGroupWithFormControl ,
2930 StandaloneButtonToggle ,
3031 ] ,
3132 } ) ;
@@ -464,6 +465,52 @@ describe('MdButtonToggle', () => {
464465
465466 } ) ;
466467
468+ describe ( 'using FormControl' , ( ) => {
469+ let fixture : ComponentFixture < ButtonToggleGroupWithFormControl > ;
470+ let groupDebugElement : DebugElement ;
471+ let groupInstance : MdButtonToggleGroup ;
472+ let testComponent : ButtonToggleGroupWithFormControl ;
473+
474+ beforeEach ( async ( ( ) => {
475+ fixture = TestBed . createComponent ( ButtonToggleGroupWithFormControl ) ;
476+ fixture . detectChanges ( ) ;
477+
478+ testComponent = fixture . debugElement . componentInstance ;
479+
480+ groupDebugElement = fixture . debugElement . query ( By . directive ( MdButtonToggleGroup ) ) ;
481+ groupInstance = groupDebugElement . injector . get ( MdButtonToggleGroup ) ;
482+ } ) ) ;
483+
484+ it ( 'should toggle the disabled state' , ( ) => {
485+ testComponent . control . disable ( ) ;
486+
487+ expect ( groupInstance . disabled ) . toBe ( true ) ;
488+
489+ testComponent . control . enable ( ) ;
490+
491+ expect ( groupInstance . disabled ) . toBe ( false ) ;
492+ } ) ;
493+
494+ it ( 'should set the value' , ( ) => {
495+ testComponent . control . setValue ( 'green' ) ;
496+
497+ expect ( groupInstance . value ) . toBe ( 'green' ) ;
498+
499+ testComponent . control . setValue ( 'red' ) ;
500+
501+ expect ( groupInstance . value ) . toBe ( 'red' ) ;
502+ } ) ;
503+
504+ it ( 'should register the on change callback' , ( ) => {
505+ let spy = jasmine . createSpy ( 'onChange callback' ) ;
506+
507+ testComponent . control . registerOnChange ( spy ) ;
508+ testComponent . control . setValue ( 'blue' ) ;
509+
510+ expect ( spy ) . toHaveBeenCalled ( ) ;
511+ } ) ;
512+ } ) ;
513+
467514 describe ( 'as standalone' , ( ) => {
468515 let fixture : ComponentFixture < StandaloneButtonToggle > ;
469516 let buttonToggleDebugElement : DebugElement ;
@@ -598,3 +645,16 @@ class StandaloneButtonToggle { }
598645class ButtonToggleGroupWithInitialValue {
599646 lastEvent : MdButtonToggleChange ;
600647}
648+
649+ @Component ( {
650+ template : `
651+ <md-button-toggle-group [formControl]="control">
652+ <md-button-toggle value="red">Value Red</md-button-toggle>
653+ <md-button-toggle value="green">Value Green</md-button-toggle>
654+ <md-button-toggle value="blue">Value Blue</md-button-toggle>
655+ </md-button-toggle-group>
656+ `
657+ } )
658+ class ButtonToggleGroupWithFormControl {
659+ control = new FormControl ( ) ;
660+ }
0 commit comments