@@ -15,6 +15,7 @@ import {
15
15
MatCheckboxChange ,
16
16
MatCheckboxModule
17
17
} from './index' ;
18
+ import { MAT_CHECKBOX_DEFAULT_OPTIONS } from '@angular/material/checkbox' ;
18
19
19
20
20
21
describe ( 'MatCheckbox' , ( ) => {
@@ -468,13 +469,48 @@ describe('MatCheckbox', () => {
468
469
} ) ) ;
469
470
} ) ;
470
471
472
+ describe ( `when MAT_CHECKBOX_CLICK_ACTION is set` , ( ) => {
473
+ beforeEach ( ( ) => {
474
+ TestBed . resetTestingModule ( ) ;
475
+ TestBed . configureTestingModule ( {
476
+ imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
477
+ declarations : [ SingleCheckbox ] ,
478
+ providers : [
479
+ { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' } ,
480
+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'noop' } }
481
+ ]
482
+ } ) ;
483
+
484
+ fixture = createComponent ( SingleCheckbox ) ;
485
+ fixture . detectChanges ( ) ;
486
+
487
+ checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ! ;
488
+ checkboxNativeElement = checkboxDebugElement . nativeElement ;
489
+ testComponent = fixture . debugElement . componentInstance ;
490
+
491
+ inputElement = checkboxNativeElement . querySelector ( 'input' ) as HTMLInputElement ;
492
+ } ) ;
493
+
494
+ it ( 'should override the value set in the default options' , fakeAsync ( ( ) => {
495
+ testComponent . isIndeterminate = true ;
496
+ inputElement . click ( ) ;
497
+ fixture . detectChanges ( ) ;
498
+ flush ( ) ;
499
+
500
+ expect ( inputElement . checked ) . toBe ( true ) ;
501
+ expect ( inputElement . indeterminate ) . toBe ( true ) ;
502
+ } ) ) ;
503
+ } ) ;
504
+
471
505
describe ( `when MAT_CHECKBOX_CLICK_ACTION is 'check'` , ( ) => {
472
506
beforeEach ( ( ) => {
473
507
TestBed . resetTestingModule ( ) ;
474
508
TestBed . configureTestingModule ( {
475
509
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
476
510
declarations : [ SingleCheckbox ] ,
477
- providers : [ { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' } ]
511
+ providers : [
512
+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'check' } }
513
+ ]
478
514
} ) ;
479
515
480
516
fixture = createComponent ( SingleCheckbox ) ;
@@ -506,7 +542,9 @@ describe('MatCheckbox', () => {
506
542
TestBed . configureTestingModule ( {
507
543
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
508
544
declarations : [ SingleCheckbox ] ,
509
- providers : [ { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'noop' } ]
545
+ providers : [
546
+ { provide : MAT_CHECKBOX_DEFAULT_OPTIONS , useValue : { clickAction : 'noop' } }
547
+ ]
510
548
} ) ;
511
549
512
550
fixture = createComponent ( SingleCheckbox ) ;
@@ -933,6 +971,50 @@ describe('MatCheckbox', () => {
933
971
} ) ;
934
972
} ) ;
935
973
974
+ describe ( 'MatCheckboxDefaultOptions' , ( ) => {
975
+ describe ( 'when MAT_CHECKBOX_DEFAULT_OPTIONS overridden' , ( ) => {
976
+ beforeEach ( ( ) => {
977
+ TestBed . configureTestingModule ( {
978
+ imports : [ MatCheckboxModule , FormsModule ] ,
979
+ declarations : [ SingleCheckbox , SingleCheckbox ] ,
980
+ providers : [ {
981
+ provide : MAT_CHECKBOX_DEFAULT_OPTIONS ,
982
+ useValue : { color : 'primary' } ,
983
+ } ] ,
984
+ } ) ;
985
+
986
+ TestBed . compileComponents ( ) ;
987
+ } ) ;
988
+
989
+ it ( 'should override default color in component' , ( ) => {
990
+ const fixture : ComponentFixture < SingleCheckbox > =
991
+ TestBed . createComponent ( SingleCheckbox ) ;
992
+ fixture . detectChanges ( ) ;
993
+ const checkboxDebugElement : DebugElement =
994
+ fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ! ;
995
+ expect (
996
+ checkboxDebugElement . nativeElement . classList
997
+ ) . toContain ( 'mat-primary' ) ;
998
+ } ) ;
999
+
1000
+ it ( 'should not override explicit input bindings' , ( ) => {
1001
+ const fixture : ComponentFixture < SingleCheckbox > =
1002
+ TestBed . createComponent ( SingleCheckbox ) ;
1003
+ fixture . componentInstance . checkboxColor = 'warn' ;
1004
+ fixture . detectChanges ( ) ;
1005
+ const checkboxDebugElement : DebugElement =
1006
+ fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ! ;
1007
+ expect (
1008
+ checkboxDebugElement . nativeElement . classList
1009
+ ) . not . toContain ( 'mat-primary' ) ;
1010
+ expect (
1011
+ checkboxDebugElement . nativeElement . classList
1012
+ ) . toContain ( 'mat-warn' ) ;
1013
+ expect ( checkboxDebugElement . nativeElement . classList ) . toContain ( 'mat-warn' ) ;
1014
+ } ) ;
1015
+ } ) ;
1016
+ } ) ;
1017
+
936
1018
/** Simple component for testing a single checkbox. */
937
1019
@Component ( {
938
1020
template : `
0 commit comments