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 , ViewChild } from '@angular/core' ;
3
+ import { Component , DebugElement , ViewChild , Type } 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' ;
@@ -12,28 +12,14 @@ import {MutationObserverFactory} from '@angular/cdk/observers';
12
12
describe ( 'MatCheckbox' , ( ) => {
13
13
let fixture : ComponentFixture < any > ;
14
14
15
- beforeEach ( fakeAsync ( ( ) = > {
15
+ function createComponent < T > ( componentType : Type < T > ) : ComponentFixture < T > {
16
16
TestBed . configureTestingModule ( {
17
17
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
18
- declarations : [
19
- SingleCheckbox ,
20
- CheckboxWithFormDirectives ,
21
- MultipleCheckboxes ,
22
- CheckboxWithNgModel ,
23
- CheckboxWithTabIndex ,
24
- CheckboxWithAriaLabel ,
25
- CheckboxWithAriaLabelledby ,
26
- CheckboxWithNameAttribute ,
27
- CheckboxWithChangeEvent ,
28
- CheckboxWithFormControl ,
29
- CheckboxWithoutLabel ,
30
- CheckboxWithTabindexAttr ,
31
- CheckboxUsingViewChild ,
32
- ]
33
- } ) ;
18
+ declarations : [ componentType ] ,
19
+ } ) . compileComponents ( ) ;
34
20
35
- TestBed . compileComponents ( ) ;
36
- } ) ) ;
21
+ return TestBed . createComponent < T > ( componentType ) ;
22
+ }
37
23
38
24
describe ( 'basic behaviors' , ( ) => {
39
25
let checkboxDebugElement : DebugElement ;
@@ -44,7 +30,7 @@ describe('MatCheckbox', () => {
44
30
let labelElement : HTMLLabelElement ;
45
31
46
32
beforeEach ( ( ) => {
47
- fixture = TestBed . createComponent ( SingleCheckbox ) ;
33
+ fixture = createComponent ( SingleCheckbox ) ;
48
34
fixture . detectChanges ( ) ;
49
35
50
36
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -547,15 +533,13 @@ describe('MatCheckbox', () => {
547
533
TestBed . resetTestingModule ( ) ;
548
534
TestBed . configureTestingModule ( {
549
535
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
550
- declarations : [
551
- SingleCheckbox ,
552
- ] ,
536
+ declarations : [ SingleCheckbox ] ,
553
537
providers : [
554
538
{ provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'check' }
555
539
]
556
540
} ) ;
557
541
558
- fixture = TestBed . createComponent ( SingleCheckbox ) ;
542
+ fixture = createComponent ( SingleCheckbox ) ;
559
543
fixture . detectChanges ( ) ;
560
544
561
545
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -586,15 +570,13 @@ describe('MatCheckbox', () => {
586
570
TestBed . resetTestingModule ( ) ;
587
571
TestBed . configureTestingModule ( {
588
572
imports : [ MatCheckboxModule , FormsModule , ReactiveFormsModule ] ,
589
- declarations : [
590
- SingleCheckbox ,
591
- ] ,
573
+ declarations : [ SingleCheckbox ] ,
592
574
providers : [
593
575
{ provide : MAT_CHECKBOX_CLICK_ACTION , useValue : 'noop' }
594
576
]
595
577
} ) ;
596
578
597
- fixture = TestBed . createComponent ( SingleCheckbox ) ;
579
+ fixture = createComponent ( SingleCheckbox ) ;
598
580
fixture . detectChanges ( ) ;
599
581
600
582
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -658,7 +640,7 @@ describe('MatCheckbox', () => {
658
640
let labelElement : HTMLLabelElement ;
659
641
660
642
beforeEach ( ( ) => {
661
- fixture = TestBed . createComponent ( CheckboxWithChangeEvent ) ;
643
+ fixture = createComponent ( CheckboxWithChangeEvent ) ;
662
644
fixture . detectChanges ( ) ;
663
645
664
646
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -708,7 +690,7 @@ describe('MatCheckbox', () => {
708
690
let inputElement : HTMLInputElement ;
709
691
710
692
it ( 'should use the provided aria-label' , ( ) => {
711
- fixture = TestBed . createComponent ( CheckboxWithAriaLabel ) ;
693
+ fixture = createComponent ( CheckboxWithAriaLabel ) ;
712
694
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
713
695
checkboxNativeElement = checkboxDebugElement . nativeElement ;
714
696
inputElement = < HTMLInputElement > checkboxNativeElement . querySelector ( 'input' ) ;
@@ -718,7 +700,7 @@ describe('MatCheckbox', () => {
718
700
} ) ;
719
701
720
702
it ( 'should not set the aria-label attribute if no value is provided' , ( ) => {
721
- fixture = TestBed . createComponent ( SingleCheckbox ) ;
703
+ fixture = createComponent ( SingleCheckbox ) ;
722
704
fixture . detectChanges ( ) ;
723
705
724
706
expect ( fixture . nativeElement . querySelector ( 'input' ) . hasAttribute ( 'aria-label' ) ) . toBe ( false ) ;
@@ -731,7 +713,7 @@ describe('MatCheckbox', () => {
731
713
let inputElement : HTMLInputElement ;
732
714
733
715
it ( 'should use the provided aria-labelledby' , ( ) => {
734
- fixture = TestBed . createComponent ( CheckboxWithAriaLabelledby ) ;
716
+ fixture = createComponent ( CheckboxWithAriaLabelledby ) ;
735
717
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
736
718
checkboxNativeElement = checkboxDebugElement . nativeElement ;
737
719
inputElement = < HTMLInputElement > checkboxNativeElement . querySelector ( 'input' ) ;
@@ -741,7 +723,7 @@ describe('MatCheckbox', () => {
741
723
} ) ;
742
724
743
725
it ( 'should not assign aria-labelledby if none is provided' , ( ) => {
744
- fixture = TestBed . createComponent ( SingleCheckbox ) ;
726
+ fixture = createComponent ( SingleCheckbox ) ;
745
727
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
746
728
checkboxNativeElement = checkboxDebugElement . nativeElement ;
747
729
inputElement = < HTMLInputElement > checkboxNativeElement . querySelector ( 'input' ) ;
@@ -758,7 +740,7 @@ describe('MatCheckbox', () => {
758
740
let inputElement : HTMLInputElement ;
759
741
760
742
beforeEach ( ( ) => {
761
- fixture = TestBed . createComponent ( CheckboxWithTabIndex ) ;
743
+ fixture = createComponent ( CheckboxWithTabIndex ) ;
762
744
fixture . detectChanges ( ) ;
763
745
764
746
testComponent = fixture . debugElement . componentInstance ;
@@ -788,7 +770,7 @@ describe('MatCheckbox', () => {
788
770
789
771
describe ( 'with native tabindex attribute' , ( ) => {
790
772
it ( 'should properly detect native tabindex attribute' , fakeAsync ( ( ) => {
791
- fixture = TestBed . createComponent ( CheckboxWithTabindexAttr ) ;
773
+ fixture = createComponent ( CheckboxWithTabindexAttr ) ;
792
774
fixture . detectChanges ( ) ;
793
775
794
776
const checkbox = fixture . debugElement
@@ -805,7 +787,7 @@ describe('MatCheckbox', () => {
805
787
let testComponent : CheckboxUsingViewChild ;
806
788
807
789
beforeEach ( ( ) => {
808
- fixture = TestBed . createComponent ( CheckboxUsingViewChild ) ;
790
+ fixture = createComponent ( CheckboxUsingViewChild ) ;
809
791
fixture . detectChanges ( ) ;
810
792
811
793
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -856,7 +838,7 @@ describe('MatCheckbox', () => {
856
838
857
839
describe ( 'with multiple checkboxes' , ( ) => {
858
840
beforeEach ( ( ) => {
859
- fixture = TestBed . createComponent ( MultipleCheckboxes ) ;
841
+ fixture = createComponent ( MultipleCheckboxes ) ;
860
842
fixture . detectChanges ( ) ;
861
843
} ) ;
862
844
@@ -878,7 +860,7 @@ describe('MatCheckbox', () => {
878
860
let inputElement : HTMLInputElement ;
879
861
880
862
beforeEach ( ( ) => {
881
- fixture = TestBed . createComponent ( CheckboxWithFormDirectives ) ;
863
+ fixture = createComponent ( CheckboxWithFormDirectives ) ;
882
864
fixture . detectChanges ( ) ;
883
865
884
866
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -922,7 +904,7 @@ describe('MatCheckbox', () => {
922
904
let testComponent : CheckboxWithNgModel ;
923
905
924
906
beforeEach ( ( ) => {
925
- fixture = TestBed . createComponent ( CheckboxWithNgModel ) ;
907
+ fixture = createComponent ( CheckboxWithNgModel ) ;
926
908
fixture . detectChanges ( ) ;
927
909
928
910
let checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -953,7 +935,7 @@ describe('MatCheckbox', () => {
953
935
954
936
describe ( 'with name attribute' , ( ) => {
955
937
beforeEach ( ( ) => {
956
- fixture = TestBed . createComponent ( CheckboxWithNameAttribute ) ;
938
+ fixture = createComponent ( CheckboxWithNameAttribute ) ;
957
939
fixture . detectChanges ( ) ;
958
940
} ) ;
959
941
@@ -972,7 +954,7 @@ describe('MatCheckbox', () => {
972
954
let inputElement : HTMLInputElement ;
973
955
974
956
beforeEach ( ( ) => {
975
- fixture = TestBed . createComponent ( CheckboxWithFormControl ) ;
957
+ fixture = createComponent ( CheckboxWithFormControl ) ;
976
958
fixture . detectChanges ( ) ;
977
959
978
960
checkboxDebugElement = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
@@ -1003,7 +985,7 @@ describe('MatCheckbox', () => {
1003
985
let checkboxInnerContainer : HTMLElement ;
1004
986
1005
987
beforeEach ( ( ) => {
1006
- fixture = TestBed . createComponent ( CheckboxWithoutLabel ) ;
988
+ fixture = createComponent ( CheckboxWithoutLabel ) ;
1007
989
1008
990
const checkboxDebugEl = fixture . debugElement . query ( By . directive ( MatCheckbox ) ) ;
1009
991
@@ -1055,7 +1037,7 @@ describe('MatCheckbox', () => {
1055
1037
} )
1056
1038
. compileComponents ( ) ;
1057
1039
1058
- fixture = TestBed . createComponent ( CheckboxWithoutLabel ) ;
1040
+ fixture = createComponent ( CheckboxWithoutLabel ) ;
1059
1041
checkboxInnerContainer = fixture . debugElement
1060
1042
. query ( By . css ( '.mat-checkbox-inner-container' ) ) . nativeElement ;
1061
1043
0 commit comments