@@ -1253,26 +1253,24 @@ describe('MatAutocomplete', () => {
1253
1253
} ) ;
1254
1254
1255
1255
describe ( 'option groups' , ( ) => {
1256
- let fixture : ComponentFixture < AutocompleteWithGroups > ;
1257
1256
let DOWN_ARROW_EVENT : KeyboardEvent ;
1258
1257
let UP_ARROW_EVENT : KeyboardEvent ;
1259
- let container : HTMLElement ;
1260
-
1261
- beforeEach ( fakeAsync ( ( ) => {
1262
- fixture = createComponent ( AutocompleteWithGroups ) ;
1263
- fixture . detectChanges ( ) ;
1264
1258
1259
+ beforeEach ( ( ) => {
1265
1260
DOWN_ARROW_EVENT = createKeyboardEvent ( 'keydown' , DOWN_ARROW ) ;
1266
1261
UP_ARROW_EVENT = createKeyboardEvent ( 'keydown' , UP_ARROW ) ;
1262
+ } ) ;
1263
+
1264
+ it ( 'should scroll to active options below the fold' , fakeAsync ( ( ) => {
1265
+ const fixture = createComponent ( AutocompleteWithGroups ) ;
1266
+ fixture . detectChanges ( ) ;
1267
1267
1268
1268
fixture . componentInstance . trigger . openPanel ( ) ;
1269
1269
fixture . detectChanges ( ) ;
1270
1270
tick ( ) ;
1271
1271
fixture . detectChanges ( ) ;
1272
- container = document . querySelector ( '.mat-autocomplete-panel' ) as HTMLElement ;
1273
- } ) ) ;
1272
+ const container = document . querySelector ( '.mat-autocomplete-panel' ) as HTMLElement ;
1274
1273
1275
- it ( 'should scroll to active options below the fold' , fakeAsync ( ( ) => {
1276
1274
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
1277
1275
tick ( ) ;
1278
1276
fixture . detectChanges ( ) ;
@@ -1291,6 +1289,15 @@ describe('MatAutocomplete', () => {
1291
1289
} ) ) ;
1292
1290
1293
1291
it ( 'should scroll to active options on UP arrow' , fakeAsync ( ( ) => {
1292
+ const fixture = createComponent ( AutocompleteWithGroups ) ;
1293
+ fixture . detectChanges ( ) ;
1294
+
1295
+ fixture . componentInstance . trigger . openPanel ( ) ;
1296
+ fixture . detectChanges ( ) ;
1297
+ tick ( ) ;
1298
+ fixture . detectChanges ( ) ;
1299
+ const container = document . querySelector ( '.mat-autocomplete-panel' ) as HTMLElement ;
1300
+
1294
1301
fixture . componentInstance . trigger . _handleKeydown ( UP_ARROW_EVENT ) ;
1295
1302
tick ( ) ;
1296
1303
fixture . detectChanges ( ) ;
@@ -1301,6 +1308,15 @@ describe('MatAutocomplete', () => {
1301
1308
} ) ) ;
1302
1309
1303
1310
it ( 'should scroll to active options that are above the panel' , fakeAsync ( ( ) => {
1311
+ const fixture = createComponent ( AutocompleteWithGroups ) ;
1312
+ fixture . detectChanges ( ) ;
1313
+
1314
+ fixture . componentInstance . trigger . openPanel ( ) ;
1315
+ fixture . detectChanges ( ) ;
1316
+ tick ( ) ;
1317
+ fixture . detectChanges ( ) ;
1318
+ const container = document . querySelector ( '.mat-autocomplete-panel' ) as HTMLElement ;
1319
+
1304
1320
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
1305
1321
tick ( ) ;
1306
1322
fixture . detectChanges ( ) ;
@@ -1326,6 +1342,15 @@ describe('MatAutocomplete', () => {
1326
1342
1327
1343
it ( 'should scroll back to the top when reaching the first option with preceding group label' ,
1328
1344
fakeAsync ( ( ) => {
1345
+ const fixture = createComponent ( AutocompleteWithGroups ) ;
1346
+ fixture . detectChanges ( ) ;
1347
+
1348
+ fixture . componentInstance . trigger . openPanel ( ) ;
1349
+ fixture . detectChanges ( ) ;
1350
+ tick ( ) ;
1351
+ fixture . detectChanges ( ) ;
1352
+ const container = document . querySelector ( '.mat-autocomplete-panel' ) as HTMLElement ;
1353
+
1329
1354
fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
1330
1355
tick ( ) ;
1331
1356
fixture . detectChanges ( ) ;
@@ -1345,6 +1370,33 @@ describe('MatAutocomplete', () => {
1345
1370
1346
1371
expect ( container . scrollTop ) . toBe ( 0 , 'Expected panel to be scrolled to the top.' ) ;
1347
1372
} ) ) ;
1373
+
1374
+ it ( 'should scroll to active option when group is indirect descendant' , fakeAsync ( ( ) => {
1375
+ const fixture = createComponent ( AutocompleteWithIndirectGroups ) ;
1376
+ fixture . detectChanges ( ) ;
1377
+
1378
+ fixture . componentInstance . trigger . openPanel ( ) ;
1379
+ fixture . detectChanges ( ) ;
1380
+ tick ( ) ;
1381
+ fixture . detectChanges ( ) ;
1382
+ const container = document . querySelector ( '.mat-autocomplete-panel' ) as HTMLElement ;
1383
+
1384
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
1385
+ tick ( ) ;
1386
+ fixture . detectChanges ( ) ;
1387
+ expect ( container . scrollTop ) . toBe ( 0 , 'Expected the panel not to scroll.' ) ;
1388
+
1389
+ // Press the down arrow five times.
1390
+ [ 1 , 2 , 3 , 4 , 5 ] . forEach ( ( ) => {
1391
+ fixture . componentInstance . trigger . _handleKeydown ( DOWN_ARROW_EVENT ) ;
1392
+ tick ( ) ;
1393
+ } ) ;
1394
+
1395
+ // <option bottom> - <panel height> + <2x group labels> = 128
1396
+ // 288 - 256 + 96 = 128
1397
+ expect ( container . scrollTop )
1398
+ . toBe ( 128 , 'Expected panel to reveal the sixth option.' ) ;
1399
+ } ) ) ;
1348
1400
} ) ;
1349
1401
1350
1402
describe ( 'aria' , ( ) => {
@@ -2808,6 +2860,25 @@ class AutocompleteWithGroups {
2808
2860
] ;
2809
2861
}
2810
2862
2863
+ @Component ( {
2864
+ template : `
2865
+ <mat-form-field>
2866
+ <input matInput placeholder="State" [matAutocomplete]="auto" [(ngModel)]="selectedState">
2867
+ </mat-form-field>
2868
+
2869
+ <mat-autocomplete #auto="matAutocomplete">
2870
+ <ng-container [ngSwitch]="true">
2871
+ <mat-optgroup *ngFor="let group of stateGroups" [label]="group.label">
2872
+ <mat-option *ngFor="let state of group.states" [value]="state">
2873
+ <span>{{ state }}</span>
2874
+ </mat-option>
2875
+ </mat-optgroup>
2876
+ </ng-container>
2877
+ </mat-autocomplete>
2878
+ `
2879
+ } )
2880
+ class AutocompleteWithIndirectGroups extends AutocompleteWithGroups { }
2881
+
2811
2882
@Component ( {
2812
2883
template : `
2813
2884
<mat-form-field>
0 commit comments