@@ -12,6 +12,7 @@ import {By} from '@angular/platform-browser';
12
12
import { dispatchFakeEvent } from '@angular/cdk/testing' ;
13
13
import { MatCheckbox , MatCheckboxChange , MatCheckboxModule } from './index' ;
14
14
import { RIPPLE_FADE_IN_DURATION , RIPPLE_FADE_OUT_DURATION } from '@angular/material/core' ;
15
+ import { MAT_CHECKBOX_CLICK_ACTION } from './checkbox-config' ;
15
16
16
17
17
18
describe ( 'MatCheckbox' , ( ) => {
@@ -544,6 +545,112 @@ describe('MatCheckbox', () => {
544
545
expect ( checkboxNativeElement ) . not . toMatch ( / ^ m a t \- c h e c k b o x \- a n i m / g) ;
545
546
} ) ;
546
547
} ) ;
548
+
549
+ describe ( `when MAT_CHECKBOX_CLICK_ACTION is 'check'` , ( ) => {
550
+ beforeEach ( ( ) => {
551
+ TestBed . resetTestingModule ( ) ;
552
+ TestBed . configureTestingModule ( {
553
+ imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
554
+ declarations : [
555
+ SingleCheckbox ,
556
+ ] ,
557
+ providers : [
558
+ { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' }
559
+ ]
560
+ } ) ;
561
+
562
+ fixture = TestBed . createComponent ( SingleCheckbox ) ;
563
+ fixture . detectChanges ( ) ;
564
+
565
+ checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
566
+ checkboxNativeElement = checkboxDebugElement . nativeElement ;
567
+ checkboxInstance = checkboxDebugElement . componentInstance ;
568
+ testComponent = fixture . debugElement . componentInstance ;
569
+
570
+ inputElement = checkboxNativeElement . querySelector ( 'input' ) as HTMLInputElement ;
571
+ labelElement = checkboxNativeElement . querySelector ( 'label' ) as HTMLLabelElement ;
572
+ } ) ;
573
+
574
+ it ( 'should not set `indeterminate` to false on click if check is set' , fakeAsync ( ( ) => {
575
+ testComponent . isIndeterminate = true ;
576
+ inputElement . click ( ) ;
577
+
578
+ fixture . detectChanges ( ) ;
579
+ flushMicrotasks ( ) ;
580
+ fixture . detectChanges ( ) ;
581
+ expect ( inputElement . checked ) . toBe ( true ) ;
582
+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-checked' ) ;
583
+ expect ( inputElement . indeterminate ) . toBe ( true ) ;
584
+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-indeterminate' ) ;
585
+ } ) ) ;
586
+ } ) ;
587
+
588
+ describe ( `when MAT_CHECKBOX_CLICK_ACTION is 'noop'` , ( ) => {
589
+ beforeEach ( ( ) => {
590
+ TestBed . resetTestingModule ( ) ;
591
+ TestBed . configureTestingModule ( {
592
+ imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
593
+ declarations : [
594
+ SingleCheckbox ,
595
+ ] ,
596
+ providers : [
597
+ { provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'noop' }
598
+ ]
599
+ } ) ;
600
+
601
+ fixture = TestBed . createComponent ( SingleCheckbox ) ;
602
+ fixture . detectChanges ( ) ;
603
+
604
+ checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
605
+ checkboxNativeElement = checkboxDebugElement . nativeElement ;
606
+ checkboxInstance = checkboxDebugElement . componentInstance ;
607
+ testComponent = fixture . debugElement . componentInstance ;
608
+ inputElement = checkboxNativeElement . querySelector ( 'input' ) as HTMLInputElement ;
609
+ labelElement = checkboxNativeElement . querySelector ( 'label' ) as HTMLLabelElement ;
610
+ } ) ;
611
+
612
+ it ( 'should not change `indeterminate` on click if noop is set' , fakeAsync ( ( ) => {
613
+ testComponent . isIndeterminate = true ;
614
+ inputElement . click ( ) ;
615
+
616
+ fixture . detectChanges ( ) ;
617
+ flushMicrotasks ( ) ;
618
+ fixture . detectChanges ( ) ;
619
+
620
+ expect ( inputElement . checked ) . toBe ( false ) ;
621
+ expect ( checkboxNativeElement . classList ) . not . toContain ( 'mat-checkbox-checked' ) ;
622
+ expect ( inputElement . indeterminate ) . toBe ( true ) ;
623
+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-indeterminate' ) ;
624
+ } ) ) ;
625
+
626
+
627
+ it ( `should not change 'checked' or 'indeterminate' on click if noop is set` , fakeAsync ( ( ) => {
628
+ testComponent . isChecked = true ;
629
+ testComponent . isIndeterminate = true ;
630
+ inputElement . click ( ) ;
631
+
632
+ fixture . detectChanges ( ) ;
633
+ flushMicrotasks ( ) ;
634
+ fixture . detectChanges ( ) ;
635
+
636
+ expect ( inputElement . checked ) . toBe ( true ) ;
637
+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-checked' ) ;
638
+ expect ( inputElement . indeterminate ) . toBe ( true ) ;
639
+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-indeterminate' ) ;
640
+
641
+ testComponent . isChecked = false ;
642
+ inputElement . click ( ) ;
643
+
644
+ fixture . detectChanges ( ) ;
645
+ flushMicrotasks ( ) ;
646
+ fixture . detectChanges ( ) ;
647
+
648
+ expect ( inputElement . checked ) . toBe ( false ) ;
649
+ expect ( checkboxNativeElement . classList ) . not . toContain ( 'mat-checkbox-checked' ) ;
650
+ expect ( inputElement . indeterminate ) . toBe ( true , 'indeterminate should not change' ) ;
651
+ expect ( checkboxNativeElement . classList ) . toContain ( 'mat-checkbox-indeterminate' ) ;
652
+ } ) ) ;
653
+ } ) ;
547
654
} ) ;
548
655
549
656
describe ( 'with change event and no initial value' , ( ) => {
0 commit comments