1
- import { Direction , Directionality } from '@angular/cdk/bidi' ;
1
+ import { Directionality } from '@angular/cdk/bidi' ;
2
2
import { DOWN_ARROW , ENTER , ESCAPE , SPACE , UP_ARROW , TAB } from '@angular/cdk/keycodes' ;
3
3
import { OverlayContainer , Overlay } from '@angular/cdk/overlay' ;
4
4
import { map } from 'rxjs/operators/map' ;
@@ -20,6 +20,7 @@ import {
20
20
ViewChild ,
21
21
ViewChildren ,
22
22
NgZone ,
23
+ Provider ,
23
24
} from '@angular/core' ;
24
25
import {
25
26
async ,
@@ -46,6 +47,7 @@ import {
46
47
MatAutocompleteSelectedEvent ,
47
48
MatAutocompleteTrigger ,
48
49
MAT_AUTOCOMPLETE_SCROLL_STRATEGY ,
50
+ MAT_AUTOCOMPLETE_DEFAULT_OPTIONS ,
49
51
} from './index' ;
50
52
51
53
@@ -56,7 +58,7 @@ describe('MatAutocomplete', () => {
56
58
let zone : MockNgZone ;
57
59
58
60
// Creates a test component fixture.
59
- function createComponent ( component : any , dir : Direction = 'ltr' ) : ComponentFixture < any > {
61
+ function createComponent ( component : any , providers : Provider [ ] = [ ] ) : ComponentFixture < any > {
60
62
TestBed . configureTestingModule ( {
61
63
imports : [
62
64
MatAutocompleteModule ,
@@ -68,14 +70,14 @@ describe('MatAutocomplete', () => {
68
70
] ,
69
71
declarations : [ component ] ,
70
72
providers : [
71
- { provide : Directionality , useFactory : ( ) => ( { value : dir } ) } ,
72
73
{ provide : ScrollDispatcher , useFactory : ( ) => ( {
73
74
scrolled : ( ) => scrolledSubject . asObservable ( )
74
75
} ) } ,
75
76
{ provide : NgZone , useFactory : ( ) => {
76
77
zone = new MockNgZone ( ) ;
77
78
return zone ;
78
- } }
79
+ } } ,
80
+ ...providers
79
81
]
80
82
} ) ;
81
83
@@ -397,9 +399,11 @@ describe('MatAutocomplete', () => {
397
399
} ) ;
398
400
399
401
it ( 'should have the correct text direction in RTL' , ( ) => {
400
- const rtlFixture = createComponent ( SimpleAutocomplete , 'rtl' ) ;
401
- rtlFixture . detectChanges ( ) ;
402
+ const rtlFixture = createComponent ( SimpleAutocomplete , [
403
+ { provide : Directionality , useFactory : ( ) => ( { value : 'rtl' } ) } ,
404
+ ] ) ;
402
405
406
+ rtlFixture . detectChanges ( ) ;
403
407
rtlFixture . componentInstance . trigger . openPanel ( ) ;
404
408
rtlFixture . detectChanges ( ) ;
405
409
@@ -1259,12 +1263,12 @@ describe('MatAutocomplete', () => {
1259
1263
beforeEach ( ( ) => {
1260
1264
fixture = createComponent ( SimpleAutocomplete ) ;
1261
1265
fixture . detectChanges ( ) ;
1266
+ } ) ;
1262
1267
1268
+ it ( 'should deselect any other selected option' , fakeAsync ( ( ) => {
1263
1269
fixture . componentInstance . trigger . openPanel ( ) ;
1264
1270
fixture . detectChanges ( ) ;
1265
- } ) ;
1266
1271
1267
- it ( 'should deselect any other selected option' , fakeAsync ( ( ) => {
1268
1272
let options =
1269
1273
overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
1270
1274
options [ 0 ] . click ( ) ;
@@ -1288,6 +1292,9 @@ describe('MatAutocomplete', () => {
1288
1292
} ) ) ;
1289
1293
1290
1294
it ( 'should call deselect only on the previous selected option' , fakeAsync ( ( ) => {
1295
+ fixture . componentInstance . trigger . openPanel ( ) ;
1296
+ fixture . detectChanges ( ) ;
1297
+
1291
1298
let options =
1292
1299
overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
1293
1300
options [ 0 ] . click ( ) ;
@@ -1309,6 +1316,35 @@ describe('MatAutocomplete', () => {
1309
1316
expect ( componentOptions [ 0 ] . deselect ) . toHaveBeenCalled ( ) ;
1310
1317
componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
1311
1318
} ) ) ;
1319
+
1320
+ it ( 'should be able to preselect the first option' , fakeAsync ( ( ) => {
1321
+ fixture . componentInstance . trigger . autocomplete . highlightFirstOption = true ;
1322
+ fixture . componentInstance . trigger . openPanel ( ) ;
1323
+ fixture . detectChanges ( ) ;
1324
+ zone . simulateZoneExit ( ) ;
1325
+ fixture . detectChanges ( ) ;
1326
+
1327
+ expect ( overlayContainerElement . querySelectorAll ( 'mat-option' ) [ 0 ] . classList )
1328
+ . toContain ( 'mat-active' , 'Expected first option to be highlighted.' ) ;
1329
+ } ) ) ;
1330
+
1331
+ it ( 'should be able to configure preselecting the first option globally' , fakeAsync ( ( ) => {
1332
+ overlayContainer . ngOnDestroy ( ) ;
1333
+ fixture . destroy ( ) ;
1334
+ TestBed . resetTestingModule ( ) ;
1335
+ fixture = createComponent ( SimpleAutocomplete , [
1336
+ { provide : MAT_AUTOCOMPLETE_DEFAULT_OPTIONS , useValue : { highlightFirstOption : true } }
1337
+ ] ) ;
1338
+
1339
+ fixture . detectChanges ( ) ;
1340
+ fixture . componentInstance . trigger . openPanel ( ) ;
1341
+ fixture . detectChanges ( ) ;
1342
+ zone . simulateZoneExit ( ) ;
1343
+ fixture . detectChanges ( ) ;
1344
+
1345
+ expect ( overlayContainerElement . querySelectorAll ( 'mat-option' ) [ 0 ] . classList )
1346
+ . toContain ( 'mat-active' , 'Expected first option to be highlighted.' ) ;
1347
+ } ) ) ;
1312
1348
} ) ;
1313
1349
1314
1350
describe ( 'panel closing' , ( ) => {
@@ -1672,8 +1708,8 @@ describe('MatAutocomplete', () => {
1672
1708
<input matInput placeholder="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
1673
1709
</mat-form-field>
1674
1710
1675
- <mat-autocomplete class="class-one class-two" #auto="matAutocomplete"
1676
- [displayWith]="displayFn" [ disableRipple]="disableRipple">
1711
+ <mat-autocomplete class="class-one class-two" #auto="matAutocomplete" [displayWith]="displayFn"
1712
+ [disableRipple]="disableRipple">
1677
1713
<mat-option *ngFor="let state of filteredStates" [value]="state">
1678
1714
<span> {{ state.code }}: {{ state.name }} </span>
1679
1715
</mat-option>
0 commit comments