1- import { Direction , Directionality } from '@angular/cdk/bidi' ;
1+ import { Directionality } from '@angular/cdk/bidi' ;
22import { DOWN_ARROW , ENTER , ESCAPE , SPACE , UP_ARROW , TAB } from '@angular/cdk/keycodes' ;
33import { OverlayContainer , Overlay } from '@angular/cdk/overlay' ;
44import { map } from 'rxjs/operators/map' ;
@@ -20,6 +20,7 @@ import {
2020 ViewChild ,
2121 ViewChildren ,
2222 NgZone ,
23+ Provider ,
2324} from '@angular/core' ;
2425import {
2526 async ,
@@ -46,6 +47,7 @@ import {
4647 MatAutocompleteSelectedEvent ,
4748 MatAutocompleteTrigger ,
4849 MAT_AUTOCOMPLETE_SCROLL_STRATEGY ,
50+ MAT_AUTOCOMPLETE_DEFAULT_OPTIONS ,
4951} from './index' ;
5052
5153
@@ -56,7 +58,7 @@ describe('MatAutocomplete', () => {
5658 let zone : MockNgZone ;
5759
5860 // Creates a test component fixture.
59- function createComponent ( component : any , dir : Direction = 'ltr' ) : ComponentFixture < any > {
61+ function createComponent ( component : any , providers : Provider [ ] = [ ] ) : ComponentFixture < any > {
6062 TestBed . configureTestingModule ( {
6163 imports : [
6264 MatAutocompleteModule ,
@@ -68,14 +70,14 @@ describe('MatAutocomplete', () => {
6870 ] ,
6971 declarations : [ component ] ,
7072 providers : [
71- { provide : Directionality , useFactory : ( ) => ( { value : dir } ) } ,
7273 { provide : ScrollDispatcher , useFactory : ( ) => ( {
7374 scrolled : ( ) => scrolledSubject . asObservable ( )
7475 } ) } ,
7576 { provide : NgZone , useFactory : ( ) => {
7677 zone = new MockNgZone ( ) ;
7778 return zone ;
78- } }
79+ } } ,
80+ ...providers
7981 ]
8082 } ) ;
8183
@@ -410,9 +412,11 @@ describe('MatAutocomplete', () => {
410412 } ) ;
411413
412414 it ( 'should have the correct text direction in RTL' , ( ) => {
413- const rtlFixture = createComponent ( SimpleAutocomplete , 'rtl' ) ;
414- rtlFixture . detectChanges ( ) ;
415+ const rtlFixture = createComponent ( SimpleAutocomplete , [
416+ { provide : Directionality , useFactory : ( ) => ( { value : 'rtl' } ) } ,
417+ ] ) ;
415418
419+ rtlFixture . detectChanges ( ) ;
416420 rtlFixture . componentInstance . trigger . openPanel ( ) ;
417421 rtlFixture . detectChanges ( ) ;
418422
@@ -1291,12 +1295,12 @@ describe('MatAutocomplete', () => {
12911295 beforeEach ( ( ) => {
12921296 fixture = createComponent ( SimpleAutocomplete ) ;
12931297 fixture . detectChanges ( ) ;
1298+ } ) ;
12941299
1300+ it ( 'should deselect any other selected option' , fakeAsync ( ( ) => {
12951301 fixture . componentInstance . trigger . openPanel ( ) ;
12961302 fixture . detectChanges ( ) ;
1297- } ) ;
12981303
1299- it ( 'should deselect any other selected option' , fakeAsync ( ( ) => {
13001304 let options =
13011305 overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
13021306 options [ 0 ] . click ( ) ;
@@ -1320,6 +1324,9 @@ describe('MatAutocomplete', () => {
13201324 } ) ) ;
13211325
13221326 it ( 'should call deselect only on the previous selected option' , fakeAsync ( ( ) => {
1327+ fixture . componentInstance . trigger . openPanel ( ) ;
1328+ fixture . detectChanges ( ) ;
1329+
13231330 let options =
13241331 overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
13251332 options [ 0 ] . click ( ) ;
@@ -1342,15 +1349,33 @@ describe('MatAutocomplete', () => {
13421349 componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
13431350 } ) ) ;
13441351
1345- it ( 'should emit an event when an option is selected ' , fakeAsync ( ( ) => {
1346- const spy = jasmine . createSpy ( 'option selection spy' ) ;
1347- const subscription = fixture . componentInstance . trigger . optionSelections . subscribe ( spy ) ;
1348- const option = overlayContainerElement . querySelector ( 'mat-option' ) as HTMLElement ;
1349- option . click ( ) ;
1352+ it ( 'should be able to preselect the first option ' , fakeAsync ( ( ) => {
1353+ fixture . componentInstance . trigger . autocomplete . autoActiveFirstOption = true ;
1354+ fixture . componentInstance . trigger . openPanel ( ) ;
1355+ fixture . detectChanges ( ) ;
1356+ zone . simulateZoneExit ( ) ;
13501357 fixture . detectChanges ( ) ;
13511358
1352- expect ( spy ) . toHaveBeenCalledWith ( jasmine . any ( MatOptionSelectionChange ) ) ;
1353- subscription . unsubscribe ( ) ;
1359+ expect ( overlayContainerElement . querySelectorAll ( 'mat-option' ) [ 0 ] . classList )
1360+ . toContain ( 'mat-active' , 'Expected first option to be highlighted.' ) ;
1361+ } ) ) ;
1362+
1363+ it ( 'should be able to configure preselecting the first option globally' , fakeAsync ( ( ) => {
1364+ overlayContainer . ngOnDestroy ( ) ;
1365+ fixture . destroy ( ) ;
1366+ TestBed . resetTestingModule ( ) ;
1367+ fixture = createComponent ( SimpleAutocomplete , [
1368+ { provide : MAT_AUTOCOMPLETE_DEFAULT_OPTIONS , useValue : { autoActiveFirstOption : true } }
1369+ ] ) ;
1370+
1371+ fixture . detectChanges ( ) ;
1372+ fixture . componentInstance . trigger . openPanel ( ) ;
1373+ fixture . detectChanges ( ) ;
1374+ zone . simulateZoneExit ( ) ;
1375+ fixture . detectChanges ( ) ;
1376+
1377+ expect ( overlayContainerElement . querySelectorAll ( 'mat-option' ) [ 0 ] . classList )
1378+ . toContain ( 'mat-active' , 'Expected first option to be highlighted.' ) ;
13541379 } ) ) ;
13551380
13561381 it ( 'should handle `optionSelections` being accessed too early' , fakeAsync ( ( ) => {
@@ -1743,8 +1768,8 @@ describe('MatAutocomplete', () => {
17431768 <input matInput placeholder="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
17441769 </mat-form-field>
17451770
1746- <mat-autocomplete class="class-one class-two" #auto="matAutocomplete"
1747- [displayWith]="displayFn" [ disableRipple]="disableRipple">
1771+ <mat-autocomplete class="class-one class-two" #auto="matAutocomplete" [displayWith]="displayFn"
1772+ [disableRipple]="disableRipple">
17481773 <mat-option *ngFor="let state of filteredStates" [value]="state">
17491774 <span> {{ state.code }}: {{ state.name }} </span>
17501775 </mat-option>
0 commit comments