Skip to content

Commit 02463d0

Browse files
committed
refactor(input): switch to fakeAsync tests
Moves all of the input tests away from the `async` zone either to the default one or to `fakeAsync`.
1 parent 541a95e commit 02463d0

File tree

1 file changed

+79
-86
lines changed

1 file changed

+79
-86
lines changed

src/lib/input/input.spec.ts

Lines changed: 79 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Platform, PlatformModule} from '@angular/cdk/platform';
22
import {createFakeEvent, dispatchFakeEvent, wrappedErrorMessage} from '@angular/cdk/testing';
33
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';
55
import {
66
FormControl,
77
FormGroup,
@@ -29,7 +29,7 @@ import {MatInputModule} from './index';
2929
import {MatInput} from './input';
3030

3131
describe('MatInput without forms', function () {
32-
beforeEach(async(() => {
32+
beforeEach(fakeAsync(() => {
3333
TestBed.configureTestingModule({
3434
imports: [
3535
FormsModule,
@@ -160,7 +160,7 @@ describe('MatInput without forms', function () {
160160
expect(el.classList.contains('mat-form-field-empty')).toBe(true);
161161
});
162162

163-
it('should not be empty after input entered', async(() => {
163+
it('should not be empty after input entered', () => {
164164
let fixture = TestBed.createComponent(MatInputTextTestController);
165165
fixture.detectChanges();
166166

@@ -176,9 +176,9 @@ describe('MatInput without forms', function () {
176176

177177
el = fixture.debugElement.query(By.css('label')).nativeElement;
178178
expect(el.classList.contains('mat-form-field-empty')).toBe(false, 'should not be empty');
179-
}));
179+
});
180180

181-
it('should update the placeholder when input entered', async(() => {
181+
it('should update the placeholder when input entered', () => {
182182
let fixture = TestBed.createComponent(MatInputWithStaticPlaceholder);
183183
fixture.detectChanges();
184184

@@ -196,9 +196,9 @@ describe('MatInput without forms', function () {
196196

197197
expect(labelEl.classList).not.toContain('mat-form-field-empty');
198198
expect(labelEl.classList).not.toContain('mat-form-field-float');
199-
}));
199+
});
200200

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', () => {
202202
let fixture = TestBed.createComponent(MatInputWithValueBinding);
203203
fixture.detectChanges();
204204

@@ -211,7 +211,7 @@ describe('MatInput without forms', function () {
211211
fixture.detectChanges();
212212

213213
expect(placeholderEl.classList).toContain('mat-form-field-empty');
214-
}));
214+
});
215215

216216
it('should add id', () => {
217217
let fixture = TestBed.createComponent(MatInputTextTestController);
@@ -296,7 +296,7 @@ describe('MatInput without forms', function () {
296296
wrappedErrorMessage(getMatFormFieldMissingControlError()));
297297
});
298298

299-
it('validates that matInput child is present after initialization', async(() => {
299+
it('validates that matInput child is present after initialization', () => {
300300
let fixture = TestBed.createComponent(MatInputWithNgIf);
301301

302302
expect(() => fixture.detectChanges()).not.toThrowError(
@@ -306,7 +306,7 @@ describe('MatInput without forms', function () {
306306

307307
expect(() => fixture.detectChanges()).toThrowError(
308308
wrappedErrorMessage(getMatFormFieldMissingControlError()));
309-
}));
309+
});
310310

311311
it('validates the type', () => {
312312
let fixture = TestBed.createComponent(MatInputInvalidTypeTestController);
@@ -367,7 +367,7 @@ describe('MatInput without forms', function () {
367367
expect(hint.getAttribute('id')).toBeTruthy();
368368
});
369369

370-
it('supports placeholder attribute', async(() => {
370+
it('supports placeholder attribute', () => {
371371
let fixture = TestBed.createComponent(MatInputPlaceholderAttrTestComponent);
372372
fixture.detectChanges();
373373

@@ -385,9 +385,9 @@ describe('MatInput without forms', function () {
385385
expect(labelEl).not.toBeNull();
386386
expect(labelEl.nativeElement.textContent).toMatch('Other placeholder');
387387
expect(labelEl.nativeElement.textContent).not.toMatch(/\*/g);
388-
}));
388+
});
389389

390-
it('supports placeholder element', async(() => {
390+
it('supports placeholder element', () => {
391391
let fixture = TestBed.createComponent(MatInputPlaceholderElementTestComponent);
392392
fixture.detectChanges();
393393

@@ -402,7 +402,7 @@ describe('MatInput without forms', function () {
402402
expect(el).not.toBeNull();
403403
expect(el.nativeElement.textContent).toMatch('Other placeholder');
404404
expect(el.nativeElement.textContent).not.toMatch(/\*/g);
405-
}));
405+
});
406406

407407
it('supports placeholder required star', () => {
408408
let fixture = TestBed.createComponent(MatInputPlaceholderRequiredTestComponent);
@@ -436,7 +436,7 @@ describe('MatInput without forms', function () {
436436
expect(el.nativeElement.textContent).toMatch(/hello/g);
437437
});
438438

439-
it('supports the disabled attribute as binding', async(() => {
439+
it('supports the disabled attribute as binding', () => {
440440
const fixture = TestBed.createComponent(MatInputWithDisabled);
441441
fixture.detectChanges();
442442

@@ -454,9 +454,9 @@ describe('MatInput without forms', function () {
454454
expect(formFieldEl.classList.contains('mat-form-field-disabled'))
455455
.toBe(true, `Expected form field to look disabled after property is set.`);
456456
expect(inputEl.disabled).toBe(true);
457-
}));
457+
});
458458

459-
it('supports the required attribute as binding', async(() => {
459+
it('supports the required attribute as binding', () => {
460460
let fixture = TestBed.createComponent(MatInputWithRequired);
461461
fixture.detectChanges();
462462

@@ -468,9 +468,9 @@ describe('MatInput without forms', function () {
468468
fixture.detectChanges();
469469

470470
expect(inputEl.required).toBe(true);
471-
}));
471+
});
472472

473-
it('supports the type attribute as binding', async(() => {
473+
it('supports the type attribute as binding', () => {
474474
let fixture = TestBed.createComponent(MatInputWithType);
475475
fixture.detectChanges();
476476

@@ -482,7 +482,7 @@ describe('MatInput without forms', function () {
482482
fixture.detectChanges();
483483

484484
expect(inputEl.type).toBe('password');
485-
}));
485+
});
486486

487487
it('supports textarea', () => {
488488
let fixture = TestBed.createComponent(MatInputTextareaWithBindings);
@@ -734,7 +734,7 @@ describe('MatInput without forms', function () {
734734

735735
describe('MatInput with forms', () => {
736736

737-
beforeEach(async(() => {
737+
beforeEach(fakeAsync(() => {
738738
TestBed.configureTestingModule({
739739
imports: [
740740
FormsModule,
@@ -777,43 +777,41 @@ describe('MatInput with forms', () => {
777777
.toBe('false', 'Expected aria-invalid to be set to "false".');
778778
});
779779

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(() => {
781781
expect(testComponent.formControl.invalid).toBe(true, 'Expected form control to be invalid');
782782
expect(containerEl.querySelectorAll('mat-error').length).toBe(0, 'Expected no error message');
783783

784784
testComponent.formControl.markAsTouched();
785785
fixture.detectChanges();
786+
flush();
786787

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".');
795794
}));
796795

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(() => {
798797
expect(testComponent.form.submitted).toBe(false, 'Expected form not to have been submitted');
799798
expect(testComponent.formControl.invalid).toBe(true, 'Expected form control to be invalid');
800799
expect(containerEl.querySelectorAll('mat-error').length).toBe(0, 'Expected no error message');
801800

802801
dispatchFakeEvent(fixture.debugElement.query(By.css('form')).nativeElement, 'submit');
803802
fixture.detectChanges();
803+
flush();
804804

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".');
814812
}));
815813

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(() => {
817815
fixture.destroy();
818816

819817
let groupFixture = TestBed.createComponent(MatInputWithFormGroupErrorMessages);
@@ -833,46 +831,43 @@ describe('MatInput with forms', () => {
833831

834832
dispatchFakeEvent(groupFixture.debugElement.query(By.css('form')).nativeElement, 'submit');
835833
groupFixture.detectChanges();
834+
flush();
836835

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".');
847844
}));
848845

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(() => {
850847
testComponent.formControl.markAsTouched();
851848
fixture.detectChanges();
849+
flush();
852850

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.');
873868
}));
874869

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(() => {
876871
testComponent.renderError = false;
877872
fixture.detectChanges();
878873

@@ -881,11 +876,10 @@ describe('MatInput with forms', () => {
881876

882877
testComponent.formControl.markAsTouched();
883878
fixture.detectChanges();
879+
flush();
884880

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.');
889883
}));
890884

891885
it('should set the proper role on the error messages', () => {
@@ -970,7 +964,7 @@ describe('MatInput with forms', () => {
970964
expect(containerEl.querySelectorAll('mat-error').length).toBe(1, 'Expected an error message');
971965
});
972966

973-
it('should display an error message when using ShowOnDirtyErrorStateMatcher', async(() => {
967+
it('should display an error message when using ShowOnDirtyErrorStateMatcher', () => {
974968
TestBed.resetTestingModule();
975969
TestBed.configureTestingModule({
976970
imports: [
@@ -1006,7 +1000,7 @@ describe('MatInput with forms', () => {
10061000

10071001
expect(containerEl.querySelectorAll('mat-error').length)
10081002
.toBe(1, 'Expected one error message when dirty');
1009-
}));
1003+
});
10101004
});
10111005

10121006
it('should update the value when using FormControl.setValue', () => {
@@ -1043,17 +1037,16 @@ describe('MatInput with forms', () => {
10431037
expect(inputEl.disabled).toBe(true);
10441038
});
10451039

1046-
it('should not treat the number 0 as empty', async(() => {
1040+
it('should not treat the number 0 as empty', fakeAsync(() => {
10471041
let fixture = TestBed.createComponent(MatInputZeroTestController);
10481042
fixture.detectChanges();
1043+
flush();
10491044

1050-
fixture.whenStable().then(() => {
1051-
fixture.detectChanges();
1045+
fixture.detectChanges();
10521046

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);
10571050
}));
10581051
});
10591052

0 commit comments

Comments
 (0)