Skip to content

Commit e60439f

Browse files
committed
refactor: address feedback
1 parent 58d161a commit e60439f

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
MdAutocomplete,
2222
MdAutocompleteModule,
2323
MdAutocompleteTrigger,
24-
MdAutocompleteSelect,
24+
MdAutocompleteSelectedEvent,
2525
} from './index';
2626
import {MdInputModule} from '../input/index';
2727
import {Subscription} from 'rxjs/Subscription';
@@ -1551,7 +1551,7 @@ describe('MdAutocomplete', () => {
15511551
});
15521552
}));
15531553

1554-
it('should call emit an event when an option is selected', fakeAsync(() => {
1554+
it('should emit an event when an option is selected', fakeAsync(() => {
15551555
let fixture = TestBed.createComponent(AutocompleteWithSelectEvent);
15561556

15571557
fixture.detectChanges();
@@ -1560,19 +1560,45 @@ describe('MdAutocomplete', () => {
15601560
fixture.detectChanges();
15611561

15621562
let options = overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
1563-
let spy = fixture.componentInstance.select;
1563+
let spy = fixture.componentInstance.optionSelected;
15641564

15651565
options[1].click();
15661566
tick();
15671567
fixture.detectChanges();
15681568

15691569
expect(spy).toHaveBeenCalledTimes(1);
15701570

1571-
let event = spy.calls.mostRecent().args[0] as MdAutocompleteSelect;
1571+
let event = spy.calls.mostRecent().args[0] as MdAutocompleteSelectedEvent;
15721572

15731573
expect(event.source).toBe(fixture.componentInstance.autocomplete);
15741574
expect(event.option.value).toBe('Washington');
15751575
}));
1576+
1577+
it('should emit an event when a newly-added option is selected', fakeAsync(() => {
1578+
let fixture = TestBed.createComponent(AutocompleteWithSelectEvent);
1579+
1580+
fixture.detectChanges();
1581+
fixture.componentInstance.trigger.openPanel();
1582+
tick();
1583+
fixture.detectChanges();
1584+
1585+
fixture.componentInstance.states.push('Puerto Rico');
1586+
fixture.detectChanges();
1587+
1588+
let options = overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
1589+
let spy = fixture.componentInstance.optionSelected;
1590+
1591+
options[3].click();
1592+
tick();
1593+
fixture.detectChanges();
1594+
1595+
expect(spy).toHaveBeenCalledTimes(1);
1596+
1597+
let event = spy.calls.mostRecent().args[0] as MdAutocompleteSelectedEvent;
1598+
1599+
expect(event.source).toBe(fixture.componentInstance.autocomplete);
1600+
expect(event.option.value).toBe('Puerto Rico');
1601+
}));
15761602
});
15771603

15781604
@Component({
@@ -1857,7 +1883,8 @@ class AutocompleteWithGroups {
18571883
<md-input-container>
18581884
<input mdInput placeholder="State" [mdAutocomplete]="auto" [(ngModel)]="selectedState">
18591885
</md-input-container>
1860-
<md-autocomplete #auto="mdAutocomplete" (select)="select($event)">
1886+
1887+
<md-autocomplete #auto="mdAutocomplete" (optionSelected)="optionSelected($event)">
18611888
<md-option *ngFor="let state of states" [value]="state">
18621889
<span>{{ state }}</span>
18631890
</md-option>
@@ -1867,7 +1894,7 @@ class AutocompleteWithGroups {
18671894
class AutocompleteWithSelectEvent {
18681895
selectedState: string;
18691896
states = ['New York', 'Washington', 'Oregon'];
1870-
select = jasmine.createSpy('select callback');
1897+
optionSelected = jasmine.createSpy('optionSelected callback');
18711898

18721899
@ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger;
18731900
@ViewChild(MdAutocomplete) autocomplete: MdAutocomplete;

src/lib/autocomplete/autocomplete.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ import {ActiveDescendantKeyManager} from '@angular/cdk/a11y';
3232
let _uniqueAutocompleteIdCounter = 0;
3333

3434
/** Event object that is emitted when an autocomplete option is selected */
35-
export class MdAutocompleteSelect {
35+
export class MdAutocompleteSelectedEvent {
3636
constructor(public source: MdAutocomplete, public option: MdOption) { }
3737
}
3838

39-
export type AutocompletePositionY = 'above' | 'below';
4039

4140
@Component({
4241
moduleId: module.id,
@@ -74,7 +73,8 @@ export class MdAutocomplete implements AfterContentInit {
7473
@Input() displayWith: ((value: any) => string) | null = null;
7574

7675
/** Event that is emitted whenever an option from the list is selected. */
77-
@Output() select: EventEmitter<MdAutocompleteSelect> = new EventEmitter<MdAutocompleteSelect>();
76+
@Output() optionSelected: EventEmitter<MdAutocompleteSelectedEvent> =
77+
new EventEmitter<MdAutocompleteSelectedEvent>();
7878

7979
/** Unique ID to be used by autocomplete trigger's "aria-owns" property. */
8080
id: string = `md-autocomplete-${_uniqueAutocompleteIdCounter++}`;
@@ -110,8 +110,8 @@ export class MdAutocomplete implements AfterContentInit {
110110

111111
/** Emits the `select` event. */
112112
_emitSelectEvent(option: MdOption): void {
113-
const selectEvent = new MdAutocompleteSelect(this, option);
114-
this.select.emit(selectEvent);
113+
const event = new MdAutocompleteSelectedEvent(this, option);
114+
this.optionSelected.emit(event);
115115
}
116116

117117
/** Sets a class on the panel based on whether it is visible. */

0 commit comments

Comments
 (0)