@@ -20,7 +20,7 @@ import {ENTER, DOWN_ARROW, SPACE, UP_ARROW, HOME, END} from '../core/keyboard/ke
20
20
import { MdOption } from '../core/option/option' ;
21
21
import { ViewportRuler } from '../core/overlay/position/viewport-ruler' ;
22
22
import { FakeViewportRuler } from '../core/overlay/position/fake-viewport-ruler' ;
23
- import { MdAutocomplete } from './autocomplete' ;
23
+ import { MdAutocomplete , MdAutocompleteSelect } from './autocomplete' ;
24
24
import { MdInputContainer } from '../input/input-container' ;
25
25
import { Observable } from 'rxjs/Observable' ;
26
26
import { dispatchFakeEvent } from '../core/testing/dispatch-events' ;
@@ -47,7 +47,8 @@ describe('MdAutocomplete', () => {
47
47
AutocompleteWithoutForms ,
48
48
NgIfAutocomplete ,
49
49
AutocompleteWithNgModel ,
50
- AutocompleteWithOnPushDelay
50
+ AutocompleteWithOnPushDelay ,
51
+ AutocompleteWithSelectEvent
51
52
] ,
52
53
providers : [
53
54
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -1146,6 +1147,29 @@ describe('MdAutocomplete', () => {
1146
1147
expect ( panel . classList ) . toContain ( visibleClass , `Expected panel to be visible.` ) ;
1147
1148
} ) ;
1148
1149
} ) ) ;
1150
+
1151
+ it ( 'should call emit an event when an option is selected' , fakeAsync ( ( ) => {
1152
+ let fixture = TestBed . createComponent ( AutocompleteWithSelectEvent ) ;
1153
+
1154
+ fixture . detectChanges ( ) ;
1155
+ fixture . componentInstance . trigger . openPanel ( ) ;
1156
+ tick ( ) ;
1157
+ fixture . detectChanges ( ) ;
1158
+
1159
+ let options = overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
1160
+ let spy = fixture . componentInstance . select ;
1161
+
1162
+ options [ 1 ] . click ( ) ;
1163
+ tick ( ) ;
1164
+ fixture . detectChanges ( ) ;
1165
+
1166
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
1167
+
1168
+ let event = spy . calls . mostRecent ( ) . args [ 0 ] as MdAutocompleteSelect ;
1169
+
1170
+ expect ( event . source ) . toBe ( fixture . componentInstance . autocomplete ) ;
1171
+ expect ( event . option . value ) . toBe ( 'Washington' ) ;
1172
+ } ) ) ;
1149
1173
} ) ;
1150
1174
1151
1175
@Component ( {
@@ -1319,6 +1343,29 @@ class AutocompleteWithOnPushDelay implements OnInit {
1319
1343
}
1320
1344
1321
1345
1346
+ @Component ( {
1347
+ template : `
1348
+ <md-input-container>
1349
+ <input mdInput placeholder="State" [mdAutocomplete]="auto" [(ngModel)]="selectedState">
1350
+ </md-input-container>
1351
+
1352
+ <md-autocomplete #auto="mdAutocomplete" (select)="select($event)">
1353
+ <md-option *ngFor="let state of states" [value]="state">
1354
+ <span>{{ state }}</span>
1355
+ </md-option>
1356
+ </md-autocomplete>
1357
+ `
1358
+ } )
1359
+ class AutocompleteWithSelectEvent {
1360
+ selectedState : string ;
1361
+ states = [ 'New York' , 'Washington' , 'Oregon' ] ;
1362
+ select = jasmine . createSpy ( 'select callback' ) ;
1363
+
1364
+ @ViewChild ( MdAutocompleteTrigger ) trigger : MdAutocompleteTrigger ;
1365
+ @ViewChild ( MdAutocomplete ) autocomplete : MdAutocomplete ;
1366
+ }
1367
+
1368
+
1322
1369
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
1323
1370
class MockKeyboardEvent {
1324
1371
constructor ( public keyCode : number ) { }
0 commit comments