@@ -9,9 +9,15 @@ import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/f
9
9
import { Component , DebugElement , ViewChild , Type , ChangeDetectionStrategy } from '@angular/core' ;
10
10
import { By } from '@angular/platform-browser' ;
11
11
import { 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' ;
13
18
import { MAT_CHECKBOX_CLICK_ACTION } from './checkbox-config' ;
14
19
import { MutationObserverFactory } from '@angular/cdk/observers' ;
20
+ import { ThemePalette } from '@angular/material/core' ;
15
21
16
22
17
23
describe ( 'MatCheckbox' , ( ) => {
@@ -533,14 +539,47 @@ describe('MatCheckbox', () => {
533
539
} ) ) ;
534
540
} ) ;
535
541
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
+
536
575
describe ( `when MAT_CHECKBOX_CLICK_ACTION is 'check'` , ( ) => {
537
576
beforeEach ( ( ) => {
538
577
TestBed . resetTestingModule ( ) ;
539
578
TestBed . configureTestingModule ( {
540
579
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
541
580
declarations : [ SingleCheckbox ] ,
542
581
providers : [
543
- { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' }
582
+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'check' } }
544
583
]
545
584
} ) ;
546
585
@@ -577,7 +616,7 @@ describe('MatCheckbox', () => {
577
616
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
578
617
declarations : [ SingleCheckbox ] ,
579
618
providers : [
580
- { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'noop' }
619
+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'noop' } }
581
620
]
582
621
} ) ;
583
622
@@ -1155,6 +1194,50 @@ describe('MatCheckbox', () => {
1155
1194
} ) ;
1156
1195
} ) ;
1157
1196
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
+
1158
1241
/** Simple component for testing a single checkbox. */
1159
1242
@Component ( {
1160
1243
template : `
@@ -1185,7 +1268,7 @@ class SingleCheckbox {
1185
1268
parentElementClicked : boolean = false ;
1186
1269
parentElementKeyedUp : boolean = false ;
1187
1270
checkboxId : string | null = 'simple-check' ;
1188
- checkboxColor : string = 'primary' ;
1271
+ checkboxColor : ThemePalette = 'primary' ;
1189
1272
checkboxValue : string = 'single_checkbox' ;
1190
1273
1191
1274
onCheckboxClick : ( event ?: Event ) => void = ( ) => { } ;
@@ -1306,3 +1389,8 @@ class CheckboxWithProjectedLabel {}
1306
1389
class TextBindingComponent {
1307
1390
text : string = 'Some text' ;
1308
1391
}
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