Skip to content

Commit 7432ccc

Browse files
committed
fix change detection
1 parent 0aaee36 commit 7432ccc

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ describe('MatAutocomplete', () => {
102102
let fixture: ComponentFixture<SimpleAutocomplete>;
103103
let input: HTMLInputElement;
104104

105-
beforeEach(fakeAsync(() => {
105+
beforeEach(() => {
106106
fixture = createComponent(SimpleAutocomplete);
107107
fixture.detectChanges();
108108
input = fixture.debugElement.query(By.css('input')).nativeElement;
109-
}));
109+
});
110110

111111
it('should open the panel when the input is focused', () => {
112112
expect(fixture.componentInstance.trigger.panelOpen)
@@ -412,7 +412,7 @@ describe('MatAutocomplete', () => {
412412
}));
413413
});
414414

415-
it('should have the correct text direction in RTL', fakeAsync(() => {
415+
it('should have the correct text direction in RTL', () => {
416416
const rtlFixture = createComponent(SimpleAutocomplete, [
417417
{provide: Directionality, useFactory: () => ({value: 'rtl'})},
418418
]);
@@ -424,18 +424,18 @@ describe('MatAutocomplete', () => {
424424
const overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
425425
expect(overlayPane.getAttribute('dir')).toEqual('rtl');
426426

427-
}));
427+
});
428428

429429
describe('forms integration', () => {
430430
let fixture: ComponentFixture<SimpleAutocomplete>;
431431
let input: HTMLInputElement;
432432

433-
beforeEach(fakeAsync(() => {
433+
beforeEach(() => {
434434
fixture = createComponent(SimpleAutocomplete);
435435
fixture.detectChanges();
436436

437437
input = fixture.debugElement.query(By.css('input')).nativeElement;
438-
}));
438+
});
439439

440440
it('should update control value as user types with input value', () => {
441441
fixture.componentInstance.trigger.openPanel();
@@ -616,23 +616,22 @@ describe('MatAutocomplete', () => {
616616
.toBe(true, `Expected control to become touched on blur.`);
617617
});
618618

619-
it('should disable the input when used with a value accessor and without `matInput`',
620-
fakeAsync(() => {
621-
overlayContainer.ngOnDestroy();
622-
fixture.destroy();
623-
TestBed.resetTestingModule();
619+
it('should disable the input when used with a value accessor and without `matInput`', () => {
620+
overlayContainer.ngOnDestroy();
621+
fixture.destroy();
622+
TestBed.resetTestingModule();
624623

625-
const plainFixture = createComponent(PlainAutocompleteInputWithFormControl);
626-
plainFixture.detectChanges();
627-
input = plainFixture.nativeElement.querySelector('input');
624+
const plainFixture = createComponent(PlainAutocompleteInputWithFormControl);
625+
plainFixture.detectChanges();
626+
input = plainFixture.nativeElement.querySelector('input');
628627

629-
expect(input.disabled).toBe(false);
628+
expect(input.disabled).toBe(false);
630629

631-
plainFixture.componentInstance.formControl.disable();
632-
plainFixture.detectChanges();
630+
plainFixture.componentInstance.formControl.disable();
631+
plainFixture.detectChanges();
633632

634-
expect(input.disabled).toBe(true);
635-
}));
633+
expect(input.disabled).toBe(true);
634+
});
636635

637636
});
638637

@@ -1116,12 +1115,12 @@ describe('MatAutocomplete', () => {
11161115
let fixture: ComponentFixture<SimpleAutocomplete>;
11171116
let input: HTMLInputElement;
11181117

1119-
beforeEach(fakeAsync(() => {
1118+
beforeEach(() => {
11201119
fixture = createComponent(SimpleAutocomplete);
11211120
fixture.detectChanges();
11221121

11231122
input = fixture.debugElement.query(By.css('input')).nativeElement;
1124-
}));
1123+
});
11251124

11261125
it('should set role of input to combobox', () => {
11271126
expect(input.getAttribute('role'))
@@ -1233,13 +1232,13 @@ describe('MatAutocomplete', () => {
12331232
let input: HTMLInputElement;
12341233
let inputReference: HTMLInputElement;
12351234

1236-
beforeEach(fakeAsync(() => {
1235+
beforeEach(() => {
12371236
fixture = createComponent(SimpleAutocomplete);
12381237
fixture.detectChanges();
12391238

12401239
input = fixture.debugElement.query(By.css('input')).nativeElement;
12411240
inputReference = fixture.debugElement.query(By.css('.mat-input-flex')).nativeElement;
1242-
}));
1241+
});
12431242

12441243
it('should use below positioning by default', fakeAsync(() => {
12451244
fixture.componentInstance.trigger.openPanel();
@@ -1320,10 +1319,10 @@ describe('MatAutocomplete', () => {
13201319
describe('Option selection', () => {
13211320
let fixture: ComponentFixture<SimpleAutocomplete>;
13221321

1323-
beforeEach(fakeAsync(() => {
1322+
beforeEach(() => {
13241323
fixture = createComponent(SimpleAutocomplete);
13251324
fixture.detectChanges();
1326-
}));
1325+
});
13271326

13281327
it('should deselect any other selected option', fakeAsync(() => {
13291328
fixture.componentInstance.trigger.openPanel();
@@ -1513,10 +1512,10 @@ describe('MatAutocomplete', () => {
15131512
describe('without matInput', () => {
15141513
let fixture: ComponentFixture<AutocompleteWithNativeInput>;
15151514

1516-
beforeEach(fakeAsync(() => {
1515+
beforeEach(() => {
15171516
fixture = createComponent(AutocompleteWithNativeInput);
15181517
fixture.detectChanges();
1519-
}));
1518+
});
15201519

15211520
it('should not throw when clicking outside', fakeAsync(() => {
15221521
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focus');
@@ -1529,28 +1528,27 @@ describe('MatAutocomplete', () => {
15291528

15301529
describe('misc', () => {
15311530

1532-
it('should allow basic use without any forms directives', fakeAsync(() => {
1531+
it('should allow basic use without any forms directives', () => {
15331532
expect(() => {
15341533
const fixture = createComponent(AutocompleteWithoutForms);
15351534
fixture.detectChanges();
15361535

15371536
const input = fixture.debugElement.query(By.css('input')).nativeElement;
15381537
typeInElement('d', input);
15391538
fixture.detectChanges();
1540-
tick();
15411539

15421540
const options =
15431541
overlayContainerElement.querySelectorAll('mat-option') as NodeListOf<HTMLElement>;
15441542
expect(options.length).toBe(1);
15451543
}).not.toThrowError();
1546-
}));
1544+
});
15471545

1548-
it('should display an empty input when the value is undefined with ngModel', fakeAsync(() => {
1546+
it('should display an empty input when the value is undefined with ngModel', () => {
15491547
const fixture = createComponent(AutocompleteWithNgModel);
15501548
fixture.detectChanges();
15511549

15521550
expect(fixture.debugElement.query(By.css('input')).nativeElement.value).toBe('');
1553-
}));
1551+
});
15541552

15551553
it('should display the number when the selected option is the number zero', fakeAsync(() => {
15561554
const fixture = createComponent(AutocompleteWithNumbers);
@@ -1562,7 +1560,7 @@ describe('MatAutocomplete', () => {
15621560
expect(fixture.debugElement.query(By.css('input')).nativeElement.value).toBe('0');
15631561
}));
15641562

1565-
it('should work when input is wrapped in ngIf', fakeAsync(() => {
1563+
it('should work when input is wrapped in ngIf', () => {
15661564
const fixture = createComponent(NgIfAutocomplete);
15671565
fixture.detectChanges();
15681566

@@ -1575,9 +1573,9 @@ describe('MatAutocomplete', () => {
15751573
.toContain('One', `Expected panel to display when input is focused.`);
15761574
expect(overlayContainerElement.textContent)
15771575
.toContain('Two', `Expected panel to display when input is focused.`);
1578-
}));
1576+
});
15791577

1580-
it('should filter properly with ngIf after setting the active item', fakeAsync(() => {
1578+
it('should filter properly with ngIf after setting the active item', () => {
15811579
const fixture = createComponent(NgIfAutocomplete);
15821580
fixture.detectChanges();
15831581

@@ -1591,19 +1589,18 @@ describe('MatAutocomplete', () => {
15911589
const input = fixture.debugElement.query(By.css('input')).nativeElement;
15921590
typeInElement('o', input);
15931591
fixture.detectChanges();
1594-
tick();
15951592

15961593
expect(fixture.componentInstance.matOptions.length).toBe(2);
1597-
}));
1594+
});
15981595

1599-
it('should throw if the user attempts to open the panel too early', fakeAsync(() => {
1596+
it('should throw if the user attempts to open the panel too early', () => {
16001597
const fixture = createComponent(AutocompleteWithoutPanel);
16011598
fixture.detectChanges();
16021599

16031600
expect(() => {
16041601
fixture.componentInstance.trigger.openPanel();
16051602
}).toThrow(getMatAutocompleteMissingPanelError());
1606-
}));
1603+
});
16071604

16081605
it('should not throw on init, even if the panel is not defined', fakeAsync(() => {
16091606
expect(() => {
@@ -1682,7 +1679,7 @@ describe('MatAutocomplete', () => {
16821679

16831680
});
16841681

1685-
it('should have correct width when opened', fakeAsync(() => {
1682+
it('should have correct width when opened', () => {
16861683
const widthFixture = createComponent(SimpleAutocomplete);
16871684
widthFixture.componentInstance.width = 300;
16881685
widthFixture.detectChanges();
@@ -1705,9 +1702,9 @@ describe('MatAutocomplete', () => {
17051702

17061703
// Firefox, edge return a decimal value for width, so we need to parse and round it to verify
17071704
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(500);
1708-
}));
1705+
});
17091706

1710-
it('should update the width while the panel is open', fakeAsync(() => {
1707+
it('should update the width while the panel is open', () => {
17111708
const widthFixture = createComponent(SimpleAutocomplete);
17121709

17131710
widthFixture.componentInstance.width = 300;
@@ -1729,7 +1726,7 @@ describe('MatAutocomplete', () => {
17291726
widthFixture.detectChanges();
17301727

17311728
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(500);
1732-
}));
1729+
});
17331730

17341731
it('should show the panel when the options are initialized later within a component with ' +
17351732
'OnPush change detection', fakeAsync(() => {

src/lib/form-field/form-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export class MatFormField extends _MatFormFieldMixinBase
268268

269269
Promise.resolve().then(() => {
270270
this.updateOutlineGap();
271-
this._changeDetectorRef.detectChanges();
271+
this._changeDetectorRef.markForCheck();
272272
});
273273
}
274274

0 commit comments

Comments
 (0)