@@ -21,6 +21,7 @@ import {
21
21
MdAutocomplete ,
22
22
MdAutocompleteModule ,
23
23
MdAutocompleteTrigger ,
24
+ MdAutocompleteSelectedEvent ,
24
25
} from './index' ;
25
26
import { MdInputModule } from '../input/index' ;
26
27
import { Subscription } from 'rxjs/Subscription' ;
@@ -57,7 +58,8 @@ describe('MdAutocomplete', () => {
57
58
AutocompleteWithNativeInput ,
58
59
AutocompleteWithoutPanel ,
59
60
AutocompleteWithFormsAndNonfloatingPlaceholder ,
60
- AutocompleteWithGroups
61
+ AutocompleteWithGroups ,
62
+ AutocompleteWithSelectEvent ,
61
63
] ,
62
64
providers : [
63
65
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -1548,6 +1550,55 @@ describe('MdAutocomplete', () => {
1548
1550
expect ( panel . classList ) . toContain ( visibleClass , `Expected panel to be visible.` ) ;
1549
1551
} ) ;
1550
1552
} ) ) ;
1553
+
1554
+ it ( 'should emit an event when an option is selected' , fakeAsync ( ( ) => {
1555
+ let fixture = TestBed . createComponent ( AutocompleteWithSelectEvent ) ;
1556
+
1557
+ fixture . detectChanges ( ) ;
1558
+ fixture . componentInstance . trigger . openPanel ( ) ;
1559
+ tick ( ) ;
1560
+ fixture . detectChanges ( ) ;
1561
+
1562
+ let options = overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
1563
+ let spy = fixture . componentInstance . optionSelected ;
1564
+
1565
+ options [ 1 ] . click ( ) ;
1566
+ tick ( ) ;
1567
+ fixture . detectChanges ( ) ;
1568
+
1569
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
1570
+
1571
+ let event = spy . calls . mostRecent ( ) . args [ 0 ] as MdAutocompleteSelectedEvent ;
1572
+
1573
+ expect ( event . source ) . toBe ( fixture . componentInstance . autocomplete ) ;
1574
+ expect ( event . option . value ) . toBe ( 'Washington' ) ;
1575
+ } ) ) ;
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
+ } ) ) ;
1551
1602
} ) ;
1552
1603
1553
1604
@Component ( {
@@ -1826,3 +1877,25 @@ class AutocompleteWithGroups {
1826
1877
}
1827
1878
] ;
1828
1879
}
1880
+
1881
+ @Component ( {
1882
+ template : `
1883
+ <md-input-container>
1884
+ <input mdInput placeholder="State" [mdAutocomplete]="auto" [(ngModel)]="selectedState">
1885
+ </md-input-container>
1886
+
1887
+ <md-autocomplete #auto="mdAutocomplete" (optionSelected)="optionSelected($event)">
1888
+ <md-option *ngFor="let state of states" [value]="state">
1889
+ <span>{{ state }}</span>
1890
+ </md-option>
1891
+ </md-autocomplete>
1892
+ `
1893
+ } )
1894
+ class AutocompleteWithSelectEvent {
1895
+ selectedState : string ;
1896
+ states = [ 'New York' , 'Washington' , 'Oregon' ] ;
1897
+ optionSelected = jasmine . createSpy ( 'optionSelected callback' ) ;
1898
+
1899
+ @ViewChild ( MdAutocompleteTrigger ) trigger : MdAutocompleteTrigger ;
1900
+ @ViewChild ( MdAutocomplete ) autocomplete : MdAutocomplete ;
1901
+ }
0 commit comments