1
1
import { Platform , PlatformModule } from '@angular/cdk/platform' ;
2
2
import { createFakeEvent , dispatchFakeEvent , wrappedErrorMessage } from '@angular/cdk/testing' ;
3
3
import { ChangeDetectionStrategy , Component , ViewChild } from '@angular/core' ;
4
- import { async , ComponentFixture , inject , TestBed } from '@angular/core/testing' ;
4
+ import { ComponentFixture , inject , TestBed , fakeAsync , flush } from '@angular/core/testing' ;
5
5
import {
6
6
FormControl ,
7
7
FormGroup ,
@@ -29,7 +29,7 @@ import {MatInputModule} from './index';
29
29
import { MatInput } from './input' ;
30
30
31
31
describe ( 'MatInput without forms' , function ( ) {
32
- beforeEach ( async ( ( ) => {
32
+ beforeEach ( fakeAsync ( ( ) => {
33
33
TestBed . configureTestingModule ( {
34
34
imports : [
35
35
FormsModule ,
@@ -160,7 +160,7 @@ describe('MatInput without forms', function () {
160
160
expect ( el . classList . contains ( 'mat-form-field-empty' ) ) . toBe ( true ) ;
161
161
} ) ;
162
162
163
- it ( 'should not be empty after input entered' , async ( ( ) => {
163
+ it ( 'should not be empty after input entered' , ( ) => {
164
164
let fixture = TestBed . createComponent ( MatInputTextTestController ) ;
165
165
fixture . detectChanges ( ) ;
166
166
@@ -176,9 +176,9 @@ describe('MatInput without forms', function () {
176
176
177
177
el = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
178
178
expect ( el . classList . contains ( 'mat-form-field-empty' ) ) . toBe ( false , 'should not be empty' ) ;
179
- } ) ) ;
179
+ } ) ;
180
180
181
- it ( 'should update the placeholder when input entered' , async ( ( ) => {
181
+ it ( 'should update the placeholder when input entered' , ( ) => {
182
182
let fixture = TestBed . createComponent ( MatInputWithStaticPlaceholder ) ;
183
183
fixture . detectChanges ( ) ;
184
184
@@ -196,9 +196,9 @@ describe('MatInput without forms', function () {
196
196
197
197
expect ( labelEl . classList ) . not . toContain ( 'mat-form-field-empty' ) ;
198
198
expect ( labelEl . classList ) . not . toContain ( 'mat-form-field-float' ) ;
199
- } ) ) ;
199
+ } ) ;
200
200
201
- it ( 'should not be empty when the value set before view init' , async ( ( ) => {
201
+ it ( 'should not be empty when the value set before view init' , ( ) => {
202
202
let fixture = TestBed . createComponent ( MatInputWithValueBinding ) ;
203
203
fixture . detectChanges ( ) ;
204
204
@@ -211,7 +211,7 @@ describe('MatInput without forms', function () {
211
211
fixture . detectChanges ( ) ;
212
212
213
213
expect ( placeholderEl . classList ) . toContain ( 'mat-form-field-empty' ) ;
214
- } ) ) ;
214
+ } ) ;
215
215
216
216
it ( 'should add id' , ( ) => {
217
217
let fixture = TestBed . createComponent ( MatInputTextTestController ) ;
@@ -296,7 +296,7 @@ describe('MatInput without forms', function () {
296
296
wrappedErrorMessage ( getMatFormFieldMissingControlError ( ) ) ) ;
297
297
} ) ;
298
298
299
- it ( 'validates that matInput child is present after initialization' , async ( ( ) => {
299
+ it ( 'validates that matInput child is present after initialization' , ( ) => {
300
300
let fixture = TestBed . createComponent ( MatInputWithNgIf ) ;
301
301
302
302
expect ( ( ) => fixture . detectChanges ( ) ) . not . toThrowError (
@@ -306,7 +306,7 @@ describe('MatInput without forms', function () {
306
306
307
307
expect ( ( ) => fixture . detectChanges ( ) ) . toThrowError (
308
308
wrappedErrorMessage ( getMatFormFieldMissingControlError ( ) ) ) ;
309
- } ) ) ;
309
+ } ) ;
310
310
311
311
it ( 'validates the type' , ( ) => {
312
312
let fixture = TestBed . createComponent ( MatInputInvalidTypeTestController ) ;
@@ -367,7 +367,7 @@ describe('MatInput without forms', function () {
367
367
expect ( hint . getAttribute ( 'id' ) ) . toBeTruthy ( ) ;
368
368
} ) ;
369
369
370
- it ( 'supports placeholder attribute' , async ( ( ) => {
370
+ it ( 'supports placeholder attribute' , ( ) => {
371
371
let fixture = TestBed . createComponent ( MatInputPlaceholderAttrTestComponent ) ;
372
372
fixture . detectChanges ( ) ;
373
373
@@ -385,9 +385,9 @@ describe('MatInput without forms', function () {
385
385
expect ( labelEl ) . not . toBeNull ( ) ;
386
386
expect ( labelEl . nativeElement . textContent ) . toMatch ( 'Other placeholder' ) ;
387
387
expect ( labelEl . nativeElement . textContent ) . not . toMatch ( / \* / g) ;
388
- } ) ) ;
388
+ } ) ;
389
389
390
- it ( 'supports placeholder element' , async ( ( ) => {
390
+ it ( 'supports placeholder element' , ( ) => {
391
391
let fixture = TestBed . createComponent ( MatInputPlaceholderElementTestComponent ) ;
392
392
fixture . detectChanges ( ) ;
393
393
@@ -402,7 +402,7 @@ describe('MatInput without forms', function () {
402
402
expect ( el ) . not . toBeNull ( ) ;
403
403
expect ( el . nativeElement . textContent ) . toMatch ( 'Other placeholder' ) ;
404
404
expect ( el . nativeElement . textContent ) . not . toMatch ( / \* / g) ;
405
- } ) ) ;
405
+ } ) ;
406
406
407
407
it ( 'supports placeholder required star' , ( ) => {
408
408
let fixture = TestBed . createComponent ( MatInputPlaceholderRequiredTestComponent ) ;
@@ -436,7 +436,7 @@ describe('MatInput without forms', function () {
436
436
expect ( el . nativeElement . textContent ) . toMatch ( / h e l l o / g) ;
437
437
} ) ;
438
438
439
- it ( 'supports the disabled attribute as binding' , async ( ( ) => {
439
+ it ( 'supports the disabled attribute as binding' , ( ) => {
440
440
const fixture = TestBed . createComponent ( MatInputWithDisabled ) ;
441
441
fixture . detectChanges ( ) ;
442
442
@@ -454,9 +454,9 @@ describe('MatInput without forms', function () {
454
454
expect ( formFieldEl . classList . contains ( 'mat-form-field-disabled' ) )
455
455
. toBe ( true , `Expected form field to look disabled after property is set.` ) ;
456
456
expect ( inputEl . disabled ) . toBe ( true ) ;
457
- } ) ) ;
457
+ } ) ;
458
458
459
- it ( 'supports the required attribute as binding' , async ( ( ) => {
459
+ it ( 'supports the required attribute as binding' , ( ) => {
460
460
let fixture = TestBed . createComponent ( MatInputWithRequired ) ;
461
461
fixture . detectChanges ( ) ;
462
462
@@ -468,9 +468,9 @@ describe('MatInput without forms', function () {
468
468
fixture . detectChanges ( ) ;
469
469
470
470
expect ( inputEl . required ) . toBe ( true ) ;
471
- } ) ) ;
471
+ } ) ;
472
472
473
- it ( 'supports the type attribute as binding' , async ( ( ) => {
473
+ it ( 'supports the type attribute as binding' , ( ) => {
474
474
let fixture = TestBed . createComponent ( MatInputWithType ) ;
475
475
fixture . detectChanges ( ) ;
476
476
@@ -482,7 +482,7 @@ describe('MatInput without forms', function () {
482
482
fixture . detectChanges ( ) ;
483
483
484
484
expect ( inputEl . type ) . toBe ( 'password' ) ;
485
- } ) ) ;
485
+ } ) ;
486
486
487
487
it ( 'supports textarea' , ( ) => {
488
488
let fixture = TestBed . createComponent ( MatInputTextareaWithBindings ) ;
@@ -734,7 +734,7 @@ describe('MatInput without forms', function () {
734
734
735
735
describe ( 'MatInput with forms' , ( ) => {
736
736
737
- beforeEach ( async ( ( ) => {
737
+ beforeEach ( fakeAsync ( ( ) => {
738
738
TestBed . configureTestingModule ( {
739
739
imports : [
740
740
FormsModule ,
@@ -777,43 +777,41 @@ describe('MatInput with forms', () => {
777
777
. toBe ( 'false' , 'Expected aria-invalid to be set to "false".' ) ;
778
778
} ) ;
779
779
780
- it ( 'should display an error message when the input is touched and invalid' , async ( ( ) => {
780
+ it ( 'should display an error message when the input is touched and invalid' , fakeAsync ( ( ) => {
781
781
expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
782
782
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
783
783
784
784
testComponent . formControl . markAsTouched ( ) ;
785
785
fixture . detectChanges ( ) ;
786
+ flush ( ) ;
786
787
787
- fixture . whenStable ( ) . then ( ( ) => {
788
- expect ( containerEl . classList )
789
- . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
790
- expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
791
- . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
792
- expect ( inputEl . getAttribute ( 'aria-invalid' ) )
793
- . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
794
- } ) ;
788
+ expect ( containerEl . classList )
789
+ . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
790
+ expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
791
+ . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
792
+ expect ( inputEl . getAttribute ( 'aria-invalid' ) )
793
+ . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
795
794
} ) ) ;
796
795
797
- it ( 'should display an error message when the parent form is submitted' , async ( ( ) => {
796
+ it ( 'should display an error message when the parent form is submitted' , fakeAsync ( ( ) => {
798
797
expect ( testComponent . form . submitted ) . toBe ( false , 'Expected form not to have been submitted' ) ;
799
798
expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid' ) ;
800
799
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 0 , 'Expected no error message' ) ;
801
800
802
801
dispatchFakeEvent ( fixture . debugElement . query ( By . css ( 'form' ) ) . nativeElement , 'submit' ) ;
803
802
fixture . detectChanges ( ) ;
803
+ flush ( ) ;
804
804
805
- fixture . whenStable ( ) . then ( ( ) => {
806
- expect ( testComponent . form . submitted ) . toBe ( true , 'Expected form to have been submitted' ) ;
807
- expect ( containerEl . classList )
808
- . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
809
- expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
810
- . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
811
- expect ( inputEl . getAttribute ( 'aria-invalid' ) )
812
- . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
813
- } ) ;
805
+ expect ( testComponent . form . submitted ) . toBe ( true , 'Expected form to have been submitted' ) ;
806
+ expect ( containerEl . classList )
807
+ . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
808
+ expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
809
+ . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
810
+ expect ( inputEl . getAttribute ( 'aria-invalid' ) )
811
+ . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
814
812
} ) ) ;
815
813
816
- it ( 'should display an error message when the parent form group is submitted' , async ( ( ) => {
814
+ it ( 'should display an error message when the parent form group is submitted' , fakeAsync ( ( ) => {
817
815
fixture . destroy ( ) ;
818
816
819
817
let groupFixture = TestBed . createComponent ( MatInputWithFormGroupErrorMessages ) ;
@@ -833,46 +831,43 @@ describe('MatInput with forms', () => {
833
831
834
832
dispatchFakeEvent ( groupFixture . debugElement . query ( By . css ( 'form' ) ) . nativeElement , 'submit' ) ;
835
833
groupFixture . detectChanges ( ) ;
834
+ flush ( ) ;
836
835
837
- groupFixture . whenStable ( ) . then ( ( ) => {
838
- expect ( component . formGroupDirective . submitted )
839
- . toBe ( true , 'Expected form to have been submitted' ) ;
840
- expect ( containerEl . classList )
841
- . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
842
- expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
843
- . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
844
- expect ( inputEl . getAttribute ( 'aria-invalid' ) )
845
- . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
846
- } ) ;
836
+ expect ( component . formGroupDirective . submitted )
837
+ . toBe ( true , 'Expected form to have been submitted' ) ;
838
+ expect ( containerEl . classList )
839
+ . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
840
+ expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
841
+ . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
842
+ expect ( inputEl . getAttribute ( 'aria-invalid' ) )
843
+ . toBe ( 'true' , 'Expected aria-invalid to be set to "true".' ) ;
847
844
} ) ) ;
848
845
849
- it ( 'should hide the errors and show the hints once the input becomes valid' , async ( ( ) => {
846
+ it ( 'should hide the errors and show the hints once the input becomes valid' , fakeAsync ( ( ) => {
850
847
testComponent . formControl . markAsTouched ( ) ;
851
848
fixture . detectChanges ( ) ;
849
+ flush ( ) ;
852
850
853
- fixture . whenStable ( ) . then ( ( ) => {
854
- expect ( containerEl . classList )
855
- . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
856
- expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
857
- . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
858
- expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
859
- . toBe ( 0 , 'Expected no hints to be shown.' ) ;
860
-
861
- testComponent . formControl . setValue ( 'something' ) ;
862
- fixture . detectChanges ( ) ;
863
-
864
- fixture . whenStable ( ) . then ( ( ) => {
865
- expect ( containerEl . classList ) . not . toContain ( 'mat-form-field-invalid' ,
866
- 'Expected container not to have the invalid class when valid.' ) ;
867
- expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
868
- . toBe ( 0 , 'Expected no error messages when the input is valid.' ) ;
869
- expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
870
- . toBe ( 1 , 'Expected one hint to be shown once the input is valid.' ) ;
871
- } ) ;
872
- } ) ;
851
+ expect ( containerEl . classList )
852
+ . toContain ( 'mat-form-field-invalid' , 'Expected container to have the invalid CSS class.' ) ;
853
+ expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
854
+ . toBe ( 1 , 'Expected one error message to have been rendered.' ) ;
855
+ expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
856
+ . toBe ( 0 , 'Expected no hints to be shown.' ) ;
857
+
858
+ testComponent . formControl . setValue ( 'something' ) ;
859
+ fixture . detectChanges ( ) ;
860
+ flush ( ) ;
861
+
862
+ expect ( containerEl . classList ) . not . toContain ( 'mat-form-field-invalid' ,
863
+ 'Expected container not to have the invalid class when valid.' ) ;
864
+ expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
865
+ . toBe ( 0 , 'Expected no error messages when the input is valid.' ) ;
866
+ expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
867
+ . toBe ( 1 , 'Expected one hint to be shown once the input is valid.' ) ;
873
868
} ) ) ;
874
869
875
- it ( 'should not hide the hint if there are no error messages' , async ( ( ) => {
870
+ it ( 'should not hide the hint if there are no error messages' , fakeAsync ( ( ) => {
876
871
testComponent . renderError = false ;
877
872
fixture . detectChanges ( ) ;
878
873
@@ -881,11 +876,10 @@ describe('MatInput with forms', () => {
881
876
882
877
testComponent . formControl . markAsTouched ( ) ;
883
878
fixture . detectChanges ( ) ;
879
+ flush ( ) ;
884
880
885
- fixture . whenStable ( ) . then ( ( ) => {
886
- expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
887
- . toBe ( 1 , 'Expected one hint to still be shown.' ) ;
888
- } ) ;
881
+ expect ( containerEl . querySelectorAll ( 'mat-hint' ) . length )
882
+ . toBe ( 1 , 'Expected one hint to still be shown.' ) ;
889
883
} ) ) ;
890
884
891
885
it ( 'should set the proper role on the error messages' , ( ) => {
@@ -970,7 +964,7 @@ describe('MatInput with forms', () => {
970
964
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length ) . toBe ( 1 , 'Expected an error message' ) ;
971
965
} ) ;
972
966
973
- it ( 'should display an error message when using ShowOnDirtyErrorStateMatcher' , async ( ( ) => {
967
+ it ( 'should display an error message when using ShowOnDirtyErrorStateMatcher' , ( ) => {
974
968
TestBed . resetTestingModule ( ) ;
975
969
TestBed . configureTestingModule ( {
976
970
imports : [
@@ -1006,7 +1000,7 @@ describe('MatInput with forms', () => {
1006
1000
1007
1001
expect ( containerEl . querySelectorAll ( 'mat-error' ) . length )
1008
1002
. toBe ( 1 , 'Expected one error message when dirty' ) ;
1009
- } ) ) ;
1003
+ } ) ;
1010
1004
} ) ;
1011
1005
1012
1006
it ( 'should update the value when using FormControl.setValue' , ( ) => {
@@ -1043,17 +1037,16 @@ describe('MatInput with forms', () => {
1043
1037
expect ( inputEl . disabled ) . toBe ( true ) ;
1044
1038
} ) ;
1045
1039
1046
- it ( 'should not treat the number 0 as empty' , async ( ( ) => {
1040
+ it ( 'should not treat the number 0 as empty' , fakeAsync ( ( ) => {
1047
1041
let fixture = TestBed . createComponent ( MatInputZeroTestController ) ;
1048
1042
fixture . detectChanges ( ) ;
1043
+ flush ( ) ;
1049
1044
1050
- fixture . whenStable ( ) . then ( ( ) => {
1051
- fixture . detectChanges ( ) ;
1045
+ fixture . detectChanges ( ) ;
1052
1046
1053
- let el = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
1054
- expect ( el ) . not . toBeNull ( ) ;
1055
- expect ( el . classList . contains ( 'mat-form-field-empty' ) ) . toBe ( false ) ;
1056
- } ) ;
1047
+ let el = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
1048
+ expect ( el ) . not . toBeNull ( ) ;
1049
+ expect ( el . classList . contains ( 'mat-form-field-empty' ) ) . toBe ( false ) ;
1057
1050
} ) ) ;
1058
1051
} ) ;
1059
1052
0 commit comments