Skip to content

Commit fa7de2a

Browse files
authored
fix(form-field, chips): fix tests & lint (#9767)
1 parent 94e4ea8 commit fa7de2a

File tree

7 files changed

+68
-71
lines changed

7 files changed

+68
-71
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 43 additions & 40 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(() => {
105+
beforeEach(fakeAsync(() => {
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)
@@ -411,7 +411,7 @@ describe('MatAutocomplete', () => {
411411
}));
412412
});
413413

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

426-
});
426+
}));
427427

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

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

436436
input = fixture.debugElement.query(By.css('input')).nativeElement;
437-
});
437+
}));
438438

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

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

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

627-
expect(input.disabled).toBe(false);
628+
expect(input.disabled).toBe(false);
628629

629-
plainFixture.componentInstance.formControl.disable();
630-
plainFixture.detectChanges();
630+
plainFixture.componentInstance.formControl.disable();
631+
plainFixture.detectChanges();
631632

632-
expect(input.disabled).toBe(true);
633-
});
633+
expect(input.disabled).toBe(true);
634+
}));
634635

635636
});
636637

@@ -1088,12 +1089,12 @@ describe('MatAutocomplete', () => {
10881089
let fixture: ComponentFixture<SimpleAutocomplete>;
10891090
let input: HTMLInputElement;
10901091

1091-
beforeEach(() => {
1092+
beforeEach(fakeAsync(() => {
10921093
fixture = createComponent(SimpleAutocomplete);
10931094
fixture.detectChanges();
10941095

10951096
input = fixture.debugElement.query(By.css('input')).nativeElement;
1096-
});
1097+
}));
10971098

10981099
it('should set role of input to combobox', () => {
10991100
expect(input.getAttribute('role'))
@@ -1205,13 +1206,13 @@ describe('MatAutocomplete', () => {
12051206
let input: HTMLInputElement;
12061207
let inputReference: HTMLInputElement;
12071208

1208-
beforeEach(() => {
1209+
beforeEach(fakeAsync(() => {
12091210
fixture = createComponent(SimpleAutocomplete);
12101211
fixture.detectChanges();
12111212

12121213
input = fixture.debugElement.query(By.css('input')).nativeElement;
12131214
inputReference = fixture.debugElement.query(By.css('.mat-input-flex')).nativeElement;
1214-
});
1215+
}));
12151216

12161217
it('should use below positioning by default', fakeAsync(() => {
12171218
fixture.componentInstance.trigger.openPanel();
@@ -1292,10 +1293,10 @@ describe('MatAutocomplete', () => {
12921293
describe('Option selection', () => {
12931294
let fixture: ComponentFixture<SimpleAutocomplete>;
12941295

1295-
beforeEach(() => {
1296+
beforeEach(fakeAsync(() => {
12961297
fixture = createComponent(SimpleAutocomplete);
12971298
fixture.detectChanges();
1298-
});
1299+
}));
12991300

13001301
it('should deselect any other selected option', fakeAsync(() => {
13011302
fixture.componentInstance.trigger.openPanel();
@@ -1485,10 +1486,10 @@ describe('MatAutocomplete', () => {
14851486
describe('without matInput', () => {
14861487
let fixture: ComponentFixture<AutocompleteWithNativeInput>;
14871488

1488-
beforeEach(() => {
1489+
beforeEach(fakeAsync(() => {
14891490
fixture = createComponent(AutocompleteWithNativeInput);
14901491
fixture.detectChanges();
1491-
});
1492+
}));
14921493

14931494
it('should not throw when clicking outside', fakeAsync(() => {
14941495
dispatchFakeEvent(fixture.debugElement.query(By.css('input')).nativeElement, 'focus');
@@ -1501,27 +1502,28 @@ describe('MatAutocomplete', () => {
15011502

15021503
describe('misc', () => {
15031504

1504-
it('should allow basic use without any forms directives', () => {
1505+
it('should allow basic use without any forms directives', fakeAsync(() => {
15051506
expect(() => {
15061507
const fixture = createComponent(AutocompleteWithoutForms);
15071508
fixture.detectChanges();
15081509

15091510
const input = fixture.debugElement.query(By.css('input')).nativeElement;
15101511
typeInElement('d', input);
15111512
fixture.detectChanges();
1513+
tick();
15121514

15131515
const options =
15141516
overlayContainerElement.querySelectorAll('mat-option') as NodeListOf<HTMLElement>;
15151517
expect(options.length).toBe(1);
15161518
}).not.toThrowError();
1517-
});
1519+
}));
15181520

1519-
it('should display an empty input when the value is undefined with ngModel', () => {
1521+
it('should display an empty input when the value is undefined with ngModel', fakeAsync(() => {
15201522
const fixture = createComponent(AutocompleteWithNgModel);
15211523
fixture.detectChanges();
15221524

15231525
expect(fixture.debugElement.query(By.css('input')).nativeElement.value).toBe('');
1524-
});
1526+
}));
15251527

15261528
it('should display the number when the selected option is the number zero', fakeAsync(() => {
15271529
const fixture = createComponent(AutocompleteWithNumbers);
@@ -1533,7 +1535,7 @@ describe('MatAutocomplete', () => {
15331535
expect(fixture.debugElement.query(By.css('input')).nativeElement.value).toBe('0');
15341536
}));
15351537

1536-
it('should work when input is wrapped in ngIf', () => {
1538+
it('should work when input is wrapped in ngIf', fakeAsync(() => {
15371539
const fixture = createComponent(NgIfAutocomplete);
15381540
fixture.detectChanges();
15391541

@@ -1546,9 +1548,9 @@ describe('MatAutocomplete', () => {
15461548
.toContain('One', `Expected panel to display when input is focused.`);
15471549
expect(overlayContainerElement.textContent)
15481550
.toContain('Two', `Expected panel to display when input is focused.`);
1549-
});
1551+
}));
15501552

1551-
it('should filter properly with ngIf after setting the active item', () => {
1553+
it('should filter properly with ngIf after setting the active item', fakeAsync(() => {
15521554
const fixture = createComponent(NgIfAutocomplete);
15531555
fixture.detectChanges();
15541556

@@ -1562,18 +1564,19 @@ describe('MatAutocomplete', () => {
15621564
const input = fixture.debugElement.query(By.css('input')).nativeElement;
15631565
typeInElement('o', input);
15641566
fixture.detectChanges();
1567+
tick();
15651568

15661569
expect(fixture.componentInstance.matOptions.length).toBe(2);
1567-
});
1570+
}));
15681571

1569-
it('should throw if the user attempts to open the panel too early', () => {
1572+
it('should throw if the user attempts to open the panel too early', fakeAsync(() => {
15701573
const fixture = createComponent(AutocompleteWithoutPanel);
15711574
fixture.detectChanges();
15721575

15731576
expect(() => {
15741577
fixture.componentInstance.trigger.openPanel();
15751578
}).toThrow(getMatAutocompleteMissingPanelError());
1576-
});
1579+
}));
15771580

15781581
it('should not throw on init, even if the panel is not defined', fakeAsync(() => {
15791582
expect(() => {
@@ -1652,7 +1655,7 @@ describe('MatAutocomplete', () => {
16521655

16531656
});
16541657

1655-
it('should have correct width when opened', () => {
1658+
it('should have correct width when opened', fakeAsync(() => {
16561659
const widthFixture = createComponent(SimpleAutocomplete);
16571660
widthFixture.componentInstance.width = 300;
16581661
widthFixture.detectChanges();
@@ -1675,9 +1678,9 @@ describe('MatAutocomplete', () => {
16751678

16761679
// Firefox, edge return a decimal value for width, so we need to parse and round it to verify
16771680
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(500);
1678-
});
1681+
}));
16791682

1680-
it('should update the width while the panel is open', () => {
1683+
it('should update the width while the panel is open', fakeAsync(() => {
16811684
const widthFixture = createComponent(SimpleAutocomplete);
16821685

16831686
widthFixture.componentInstance.width = 300;
@@ -1699,7 +1702,7 @@ describe('MatAutocomplete', () => {
16991702
widthFixture.detectChanges();
17001703

17011704
expect(Math.ceil(parseFloat(overlayPane.style.width as string))).toBe(500);
1702-
});
1705+
}));
17031706

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

src/lib/chips/chip.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export class MatChipBase {
6363
constructor(public _elementRef: ElementRef) {}
6464
}
6565

66-
export const _MatChipMixinBase = mixinColor(mixinDisableRipple(mixinDisabled(MatChipBase)), 'primary');
66+
export const _MatChipMixinBase =
67+
mixinColor(mixinDisableRipple(mixinDisabled(MatChipBase)), 'primary');
6768

6869
const CHIP_ATTRIBUTE_NAMES = ['mat-basic-chip'];
6970

src/lib/chips/chips-module.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {PlatformModule} from '@angular/cdk/platform';
910
import {NgModule} from '@angular/core';
1011
import {ErrorStateMatcher} from '@angular/material/core';
11-
import {MatChipList} from './chip-list';
12-
import {MatChip, MatChipRemove, MatChipAvatar, MatChipTrailingIcon} from './chip';
12+
import {MatChip, MatChipAvatar, MatChipRemove, MatChipTrailingIcon} from './chip';
1313
import {MatChipInput} from './chip-input';
14+
import {MatChipList} from './chip-list';
1415

1516
const CHIP_DECLARATIONS = [
1617
MatChipList,
@@ -22,7 +23,7 @@ const CHIP_DECLARATIONS = [
2223
];
2324

2425
@NgModule({
25-
imports: [],
26+
imports: [PlatformModule],
2627
exports: CHIP_DECLARATIONS,
2728
declarations: CHIP_DECLARATIONS,
2829
providers: [ErrorStateMatcher]

src/lib/datepicker/datepicker.spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -790,12 +790,6 @@ describe('MatDatepicker', () => {
790790
fixture.detectChanges();
791791
}));
792792

793-
it('should attach popup to mat-form-field underline', () => {
794-
let attachToRef = testComponent.datepickerInput.getConnectedOverlayOrigin();
795-
expect(attachToRef.nativeElement.classList.contains('mat-form-field-underline'))
796-
.toBe(true, 'popup should be attached to mat-form-field underline');
797-
});
798-
799793
it('should float the placeholder when an invalid value is entered', () => {
800794
testComponent.datepickerInput.value = 'totally-not-a-date' as any;
801795
fixture.debugElement.nativeElement.querySelector('input').value = 'totally-not-a-date';
@@ -804,7 +798,6 @@ describe('MatDatepicker', () => {
804798
expect(fixture.debugElement.nativeElement.querySelector('mat-form-field').classList)
805799
.toContain('mat-form-field-should-float');
806800
});
807-
808801
});
809802

810803
describe('datepicker with min and max dates and validation', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export class MatFormField extends _MatFormFieldMixinBase
289289
}
290290

291291
_hasPlaceholder() {
292-
return !!(this._control.placeholder || this._placeholderChild);
292+
return !!(this._control && this._control.placeholder || this._placeholderChild);
293293
}
294294

295295
_hasLabel() {

src/lib/input/autofill.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
ElementRef,
1313
EventEmitter,
1414
Injectable,
15-
NgZone,
1615
OnDestroy,
16+
OnInit,
1717
Output
1818
} from '@angular/core';
1919
import {Observable} from 'rxjs/Observable';
@@ -50,7 +50,7 @@ const listenerOptions: any = supportsPassiveEventListeners() ? {passive: true} :
5050
export class AutofillMonitor implements OnDestroy {
5151
private _monitoredElements = new Map<Element, MonitoredElementInfo>();
5252

53-
constructor(private _platform: Platform, private _ngZone: NgZone) {}
53+
constructor(private _platform: Platform) {}
5454

5555
/**
5656
* Monitor for changes in the autofill state of the given input element.
@@ -78,9 +78,7 @@ export class AutofillMonitor implements OnDestroy {
7878
}
7979
};
8080

81-
this._ngZone.runOutsideAngular(() => {
82-
element.addEventListener('animationstart', listener, listenerOptions);
83-
});
81+
element.addEventListener('animationstart', listener, listenerOptions);
8482
element.classList.add('mat-input-autofill-monitored');
8583

8684
this._monitoredElements.set(element, {
@@ -120,13 +118,14 @@ export class AutofillMonitor implements OnDestroy {
120118
@Directive({
121119
selector: '[matAutofill]',
122120
})
123-
export class MatAutofill implements OnDestroy {
121+
export class MatAutofill implements OnDestroy, OnInit {
124122
@Output() matAutofill = new EventEmitter<AutofillEvent>();
125123

126-
constructor(private _elementRef: ElementRef, private _autofillMonitor: AutofillMonitor,
127-
ngZone: NgZone) {
124+
constructor(private _elementRef: ElementRef, private _autofillMonitor: AutofillMonitor) {}
125+
126+
ngOnInit() {
128127
this._autofillMonitor.monitor(this._elementRef.nativeElement)
129-
.subscribe(event => ngZone.run(() => this.matAutofill.emit(event)));
128+
.subscribe(event => this.matAutofill.emit(event));
130129
}
131130

132131
ngOnDestroy() {

0 commit comments

Comments
 (0)