@@ -9,9 +9,15 @@ import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/f
99import { Component , DebugElement , ViewChild , Type , ChangeDetectionStrategy } from '@angular/core' ;
1010import { By } from '@angular/platform-browser' ;
1111import { dispatchFakeEvent } from '@angular/cdk/testing/private' ;
12- import { MatCheckbox , MatCheckboxChange , MatCheckboxModule } from './index' ;
12+ import {
13+ MAT_CHECKBOX_DEFAULT_OPTIONS ,
14+ MatCheckbox ,
15+ MatCheckboxChange ,
16+ MatCheckboxModule
17+ } from './index' ;
1318import { MAT_CHECKBOX_CLICK_ACTION } from './checkbox-config' ;
1419import { MutationObserverFactory } from '@angular/cdk/observers' ;
20+ import { ThemePalette } from '@angular/material/core' ;
1521
1622
1723describe ( 'MatCheckbox' , ( ) => {
@@ -533,14 +539,47 @@ describe('MatCheckbox', () => {
533539 } ) ) ;
534540 } ) ;
535541
542+ describe ( `when MAT_CHECKBOX_CLICK_ACTION is set` , ( ) => {
543+ beforeEach ( ( ) => {
544+ TestBed . resetTestingModule ( ) ;
545+ TestBed . configureTestingModule ( {
546+ imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
547+ declarations : [ SingleCheckbox ] ,
548+ providers : [
549+ { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' } ,
550+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'noop' } }
551+ ]
552+ } ) ;
553+
554+ fixture = createComponent ( SingleCheckbox ) ;
555+ fixture . detectChanges ( ) ;
556+
557+ checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ! ;
558+ checkboxNativeElement = checkboxDebugElement . nativeElement ;
559+ testComponent = fixture . debugElement . componentInstance ;
560+
561+ inputElement = checkboxNativeElement . querySelector ( 'input' ) as HTMLInputElement ;
562+ } ) ;
563+
564+ it ( 'should override the value set in the default options' , fakeAsync ( ( ) => {
565+ testComponent . isIndeterminate = true ;
566+ inputElement . click ( ) ;
567+ fixture . detectChanges ( ) ;
568+ flush ( ) ;
569+
570+ expect ( inputElement . checked ) . toBe ( true ) ;
571+ expect ( inputElement . indeterminate ) . toBe ( true ) ;
572+ } ) ) ;
573+ } ) ;
574+
536575 describe ( `when MAT_CHECKBOX_CLICK_ACTION is 'check'` , ( ) => {
537576 beforeEach ( ( ) => {
538577 TestBed . resetTestingModule ( ) ;
539578 TestBed . configureTestingModule ( {
540579 imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
541580 declarations : [ SingleCheckbox ] ,
542581 providers : [
543- { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' }
582+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'check' } }
544583 ]
545584 } ) ;
546585
@@ -577,7 +616,7 @@ describe('MatCheckbox', () => {
577616 imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
578617 declarations : [ SingleCheckbox ] ,
579618 providers : [
580- { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'noop' }
619+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'noop' } }
581620 ]
582621 } ) ;
583622
@@ -1155,6 +1194,50 @@ describe('MatCheckbox', () => {
11551194 } ) ;
11561195} ) ;
11571196
1197+ describe ( 'MatCheckboxDefaultOptions' , ( ) => {
1198+ describe ( 'when MAT_CHECKBOX_DEFAULT_OPTIONS overridden' , ( ) => {
1199+ beforeEach ( ( ) => {
1200+ TestBed . configureTestingModule ( {
1201+ imports : [ MatCheckboxModule , FormsModule ] ,
1202+ declarations : [ SingleCheckbox , SimpleCheckbox ] ,
1203+ providers : [ {
1204+ provide : MAT_CHECKBOX_DEFAULT_OPTIONS ,
1205+ useValue : { color : 'primary' } ,
1206+ } ] ,
1207+ } ) ;
1208+
1209+ TestBed . compileComponents ( ) ;
1210+ } ) ;
1211+
1212+ it ( 'should override default color in Component' , ( ) => {
1213+ const fixture : ComponentFixture < SimpleCheckbox > =
1214+ TestBed . createComponent ( SimpleCheckbox ) ;
1215+ fixture . detectChanges ( ) ;
1216+ const checkboxDebugElement : DebugElement =
1217+ fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ! ;
1218+ expect (
1219+ checkboxDebugElement . nativeElement . classList
1220+ ) . toContain ( 'mat-primary' ) ;
1221+ } ) ;
1222+
1223+ it ( 'should not override explicit input bindings' , ( ) => {
1224+ const fixture : ComponentFixture < SingleCheckbox > =
1225+ TestBed . createComponent ( SingleCheckbox ) ;
1226+ fixture . componentInstance . checkboxColor = 'warn' ;
1227+ fixture . detectChanges ( ) ;
1228+ const checkboxDebugElement : DebugElement =
1229+ fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ! ;
1230+ expect (
1231+ checkboxDebugElement . nativeElement . classList
1232+ ) . not . toContain ( 'mat-primary' ) ;
1233+ expect (
1234+ checkboxDebugElement . nativeElement . classList
1235+ ) . toContain ( 'mat-warn' ) ;
1236+ expect ( checkboxDebugElement . nativeElement . classList ) . toContain ( 'mat-warn' ) ;
1237+ } ) ;
1238+ } ) ;
1239+ } ) ;
1240+
11581241/** Simple component for testing a single checkbox. */
11591242@Component ( {
11601243 template : `
@@ -1185,7 +1268,7 @@ class SingleCheckbox {
11851268 parentElementClicked : boolean = false ;
11861269 parentElementKeyedUp : boolean = false ;
11871270 checkboxId : string | null = 'simple-check' ;
1188- checkboxColor : string = 'primary' ;
1271+ checkboxColor : ThemePalette = 'primary' ;
11891272 checkboxValue : string = 'single_checkbox' ;
11901273
11911274 onCheckboxClick : ( event ?: Event ) => void = ( ) => { } ;
@@ -1306,3 +1389,8 @@ class CheckboxWithProjectedLabel {}
13061389class TextBindingComponent {
13071390 text : string = 'Some text' ;
13081391}
1392+
1393+ /** Test component with a simple checkbox with no inputs. */
1394+ @Component ( { template : `<mat-checkbox></mat-checkbox>` } )
1395+ class SimpleCheckbox {
1396+ }
0 commit comments