11import { ComponentFixture , fakeAsync , TestBed , tick , flush } from '@angular/core/testing' ;
22import { FormControl , FormsModule , NgModel , ReactiveFormsModule } from '@angular/forms' ;
3- import { Component , DebugElement } from '@angular/core' ;
3+ import { Component , DebugElement , ViewChild } from '@angular/core' ;
44import { By } from '@angular/platform-browser' ;
55import { dispatchFakeEvent } from '@angular/cdk/testing' ;
66import { MatCheckbox , MatCheckboxChange , MatCheckboxModule } from './index' ;
@@ -28,6 +28,7 @@ describe('MatCheckbox', () => {
2828 CheckboxWithFormControl ,
2929 CheckboxWithoutLabel ,
3030 CheckboxWithTabindexAttr ,
31+ CheckboxUsingViewChild ,
3132 ]
3233 } ) ;
3334
@@ -786,7 +787,6 @@ describe('MatCheckbox', () => {
786787 } ) ;
787788
788789 describe ( 'with native tabindex attribute' , ( ) => {
789-
790790 it ( 'should properly detect native tabindex attribute' , fakeAsync ( ( ) => {
791791 fixture = TestBed . createComponent ( CheckboxWithTabindexAttr ) ;
792792 fixture . detectChanges ( ) ;
@@ -799,6 +799,61 @@ describe('MatCheckbox', () => {
799799 } ) ) ;
800800 } ) ;
801801
802+ describe ( 'using ViewChild' , ( ) => {
803+ let checkboxDebugElement : DebugElement ;
804+ let checkboxNativeElement : HTMLElement ;
805+ let testComponent : CheckboxUsingViewChild ;
806+
807+ beforeEach ( ( ) => {
808+ fixture = TestBed . createComponent ( CheckboxUsingViewChild ) ;
809+ fixture . detectChanges ( ) ;
810+
811+ checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
812+ checkboxNativeElement = checkboxDebugElement . nativeElement ;
813+ testComponent = fixture . debugElement . componentInstance ;
814+ } ) ;
815+
816+ it ( 'should toggle checkbox disabledness correctly' , ( ) => {
817+ const checkboxInstance = checkboxDebugElement . componentInstance ;
818+ const inputElement = < HTMLInputElement > checkboxNativeElement . querySelector ( 'input' ) ;
819+ expect ( checkboxInstance . disabled ) . toBe ( false ) ;
820+ expect ( checkboxNativeElement . classList ) . not . toContain ( 'mat-checkbox-disabled' ) ;
821+ expect ( inputElement . tabIndex ) . toBe ( 0 ) ;
822+ expect ( inputElement . disabled ) . toBe ( false ) ;
823+
824+ testComponent . isDisabled = true ;
825+ fixture . detectChanges ( ) ;
826+
827+ expect ( checkboxInstance . disabled ) . toBe ( true ) ;
828+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-disabled' ) ;
829+ expect ( inputElement . disabled ) . toBe ( true ) ;
830+
831+ testComponent . isDisabled = false ;
832+ fixture . detectChanges ( ) ;
833+
834+ expect ( checkboxInstance . disabled ) . toBe ( false ) ;
835+ expect ( checkboxNativeElement . classList ) . not . toContain ( 'mat-checkbox-disabled' ) ;
836+ expect ( inputElement . tabIndex ) . toBe ( 0 ) ;
837+ expect ( inputElement . disabled ) . toBe ( false ) ;
838+ } ) ;
839+
840+ it ( 'should toggle checkbox ripple disabledness correctly' , ( ) => {
841+ const labelElement = checkboxNativeElement . querySelector ( 'label' ) as HTMLLabelElement ;
842+
843+ testComponent . isDisabled = true ;
844+ fixture . detectChanges ( ) ;
845+ dispatchFakeEvent ( labelElement , 'mousedown' ) ;
846+ dispatchFakeEvent ( labelElement , 'mouseup' ) ;
847+ expect ( checkboxNativeElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
848+
849+ testComponent . isDisabled = false ;
850+ fixture . detectChanges ( ) ;
851+ dispatchFakeEvent ( labelElement , 'mousedown' ) ;
852+ dispatchFakeEvent ( labelElement , 'mouseup' ) ;
853+ expect ( checkboxNativeElement . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
854+ } ) ;
855+ } ) ;
856+
802857 describe ( 'with multiple checkboxes' , ( ) => {
803858 beforeEach ( ( ) => {
804859 fixture = TestBed . createComponent ( MultipleCheckboxes ) ;
@@ -1116,6 +1171,20 @@ class CheckboxWithTabIndex {
11161171 isDisabled : boolean = false ;
11171172}
11181173
1174+
1175+ /** Simple test component that accesses MatCheckbox using ViewChild. */
1176+ @Component ( {
1177+ template : `
1178+ <mat-checkbox></mat-checkbox>` ,
1179+ } )
1180+ class CheckboxUsingViewChild {
1181+ @ViewChild ( MatCheckbox ) checkbox ;
1182+
1183+ set isDisabled ( value : boolean ) {
1184+ this . checkbox . disabled = value ;
1185+ }
1186+ }
1187+
11191188/** Simple test component with an aria-label set. */
11201189@Component ( {
11211190 template : `<mat-checkbox aria-label="Super effective"></mat-checkbox>`
0 commit comments