1
1
import { ComponentFixture , fakeAsync , TestBed , tick , flush } from '@angular/core/testing' ;
2
2
import { FormControl , FormsModule , NgModel , ReactiveFormsModule } from '@angular/forms' ;
3
- import { Component , DebugElement } from '@angular/core' ;
3
+ import { Component , DebugElement , ViewChild } from '@angular/core' ;
4
4
import { By } from '@angular/platform-browser' ;
5
5
import { dispatchFakeEvent } from '@angular/cdk/testing' ;
6
6
import { MatCheckbox , MatCheckboxChange , MatCheckboxModule } from './index' ;
@@ -28,6 +28,7 @@ describe('MatCheckbox', () => {
28
28
CheckboxWithFormControl ,
29
29
CheckboxWithoutLabel ,
30
30
CheckboxWithTabindexAttr ,
31
+ CheckboxUsingViewChild ,
31
32
]
32
33
} ) ;
33
34
@@ -786,7 +787,6 @@ describe('MatCheckbox', () => {
786
787
} ) ;
787
788
788
789
describe ( 'with native tabindex attribute' , ( ) => {
789
-
790
790
it ( 'should properly detect native tabindex attribute' , fakeAsync ( ( ) => {
791
791
fixture = TestBed . createComponent ( CheckboxWithTabindexAttr ) ;
792
792
fixture . detectChanges ( ) ;
@@ -799,6 +799,61 @@ describe('MatCheckbox', () => {
799
799
} ) ) ;
800
800
} ) ;
801
801
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
+
802
857
describe ( 'with multiple checkboxes' , ( ) => {
803
858
beforeEach ( ( ) => {
804
859
fixture = TestBed . createComponent ( MultipleCheckboxes ) ;
@@ -1116,6 +1171,20 @@ class CheckboxWithTabIndex {
1116
1171
isDisabled : boolean = false ;
1117
1172
}
1118
1173
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
+
1119
1188
/** Simple test component with an aria-label set. */
1120
1189
@Component ( {
1121
1190
template : `<mat-checkbox aria-label="Super effective"></mat-checkbox>`
0 commit comments