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
@@ -410,9 +412,11 @@ describe('MatAutocomplete', () => {
410
412
} ) ;
411
413
412
414
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
+ ] ) ;
415
418
419
+ rtlFixture . detectChanges ( ) ;
416
420
rtlFixture . componentInstance . trigger . openPanel ( ) ;
417
421
rtlFixture . detectChanges ( ) ;
418
422
@@ -1291,12 +1295,12 @@ describe('MatAutocomplete', () => {
1291
1295
beforeEach ( ( ) => {
1292
1296
fixture = createComponent ( SimpleAutocomplete ) ;
1293
1297
fixture . detectChanges ( ) ;
1298
+ } ) ;
1294
1299
1300
+ it ( 'should deselect any other selected option' , fakeAsync ( ( ) => {
1295
1301
fixture . componentInstance . trigger . openPanel ( ) ;
1296
1302
fixture . detectChanges ( ) ;
1297
- } ) ;
1298
1303
1299
- it ( 'should deselect any other selected option' , fakeAsync ( ( ) => {
1300
1304
let options =
1301
1305
overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
1302
1306
options [ 0 ] . click ( ) ;
@@ -1320,6 +1324,9 @@ describe('MatAutocomplete', () => {
1320
1324
} ) ) ;
1321
1325
1322
1326
it ( 'should call deselect only on the previous selected option' , fakeAsync ( ( ) => {
1327
+ fixture . componentInstance . trigger . openPanel ( ) ;
1328
+ fixture . detectChanges ( ) ;
1329
+
1323
1330
let options =
1324
1331
overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
1325
1332
options [ 0 ] . click ( ) ;
@@ -1342,15 +1349,33 @@ describe('MatAutocomplete', () => {
1342
1349
componentOptions . slice ( 1 ) . forEach ( option => expect ( option . deselect ) . not . toHaveBeenCalled ( ) ) ;
1343
1350
} ) ) ;
1344
1351
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 ( ) ;
1350
1357
fixture . detectChanges ( ) ;
1351
1358
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.' ) ;
1354
1379
} ) ) ;
1355
1380
1356
1381
it ( 'should handle `optionSelections` being accessed too early' , fakeAsync ( ( ) => {
@@ -1743,8 +1768,8 @@ describe('MatAutocomplete', () => {
1743
1768
<input matInput placeholder="State" [matAutocomplete]="auto" [formControl]="stateCtrl">
1744
1769
</mat-form-field>
1745
1770
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">
1748
1773
<mat-option *ngFor="let state of filteredStates" [value]="state">
1749
1774
<span> {{ state.code }}: {{ state.name }} </span>
1750
1775
</mat-option>
0 commit comments