Skip to content

Commit 60a3527

Browse files
crisbetowagnermaciel
authored andcommitted
fix(tabs): always defaulting focusIndex to 0 on initialization (#20384)
We were always defaulting the `focusIndex` to 0 on init, rather than taking the index of the selected tab. We actually had tests for this behavior, but they were all testing against 0 so we never caught the issue. Fixes #20374. (cherry picked from commit a0b44d4)
1 parent f7e0f31 commit 60a3527

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/material-experimental/mdc-tabs/tab-group.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ describe('MDC-based MatTabGroup', () => {
248248

249249
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(0);
250250

251-
tabLabels[1].nativeElement.click();
251+
tabLabels[2].nativeElement.click();
252252
fixture.detectChanges();
253253

254254
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(1);
255255
expect(fixture.componentInstance.handleFocus)
256-
.toHaveBeenCalledWith(jasmine.objectContaining({index: 1}));
256+
.toHaveBeenCalledWith(jasmine.objectContaining({index: 2}));
257257
});
258258

259259
it('should emit focusChange on arrow key navigation', () => {
@@ -267,8 +267,8 @@ describe('MDC-based MatTabGroup', () => {
267267
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(0);
268268

269269
// In order to verify that the `focusChange` event also fires with the correct
270-
// index, we focus the second tab before testing the keyboard navigation.
271-
tabLabels[1].nativeElement.click();
270+
// index, we focus the third tab before testing the keyboard navigation.
271+
tabLabels[2].nativeElement.click();
272272
fixture.detectChanges();
273273

274274
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(1);
@@ -277,7 +277,7 @@ describe('MDC-based MatTabGroup', () => {
277277

278278
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(2);
279279
expect(fixture.componentInstance.handleFocus)
280-
.toHaveBeenCalledWith(jasmine.objectContaining({index: 0}));
280+
.toHaveBeenCalledWith(jasmine.objectContaining({index: 1}));
281281
});
282282

283283
it('should clean up the tabs QueryList on destroy', () => {

src/material-experimental/mdc-tabs/tab-header.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,15 @@ describe('MDC-based MatTabHeader', () => {
6363
});
6464

6565
it('should initialize to the selected index', () => {
66+
// Recreate the fixture so we can test that it works with a non-default selected index
67+
fixture.destroy();
68+
fixture = TestBed.createComponent(SimpleTabHeaderApp);
69+
fixture.componentInstance.selectedIndex = 1;
6670
fixture.detectChanges();
67-
expect(appComponent.tabHeader.focusIndex).toBe(appComponent.selectedIndex);
71+
appComponent = fixture.componentInstance;
72+
tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement;
73+
74+
expect(appComponent.tabHeader.focusIndex).toBe(1);
6875
});
6976

7077
it('should send focus change event', () => {

src/material/tabs/paginated-tab-header.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export abstract class MatPaginatedTabHeader implements AfterContentChecked, Afte
196196
.withHomeAndEnd()
197197
.withWrap();
198198

199-
this._keyManager.updateActiveItem(0);
199+
this._keyManager.updateActiveItem(this._selectedIndex);
200200

201201
// Defer the first call in order to allow for slower browsers to lay out the elements.
202202
// This helps in cases where the user lands directly on a page with paginated tabs.

src/material/tabs/tab-group.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ describe('MatTabGroup', () => {
247247

248248
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(0);
249249

250-
tabLabels[1].nativeElement.click();
250+
tabLabels[2].nativeElement.click();
251251
fixture.detectChanges();
252252

253253
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(1);
254254
expect(fixture.componentInstance.handleFocus)
255-
.toHaveBeenCalledWith(jasmine.objectContaining({index: 1}));
255+
.toHaveBeenCalledWith(jasmine.objectContaining({index: 2}));
256256
});
257257

258258
it('should emit focusChange on arrow key navigation', () => {
@@ -266,8 +266,8 @@ describe('MatTabGroup', () => {
266266
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(0);
267267

268268
// In order to verify that the `focusChange` event also fires with the correct
269-
// index, we focus the second tab before testing the keyboard navigation.
270-
tabLabels[1].nativeElement.click();
269+
// index, we focus the third tab before testing the keyboard navigation.
270+
tabLabels[2].nativeElement.click();
271271
fixture.detectChanges();
272272

273273
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(1);
@@ -276,7 +276,7 @@ describe('MatTabGroup', () => {
276276

277277
expect(fixture.componentInstance.handleFocus).toHaveBeenCalledTimes(2);
278278
expect(fixture.componentInstance.handleFocus)
279-
.toHaveBeenCalledWith(jasmine.objectContaining({index: 0}));
279+
.toHaveBeenCalledWith(jasmine.objectContaining({index: 1}));
280280
});
281281

282282
it('should clean up the tabs QueryList on destroy', () => {

src/material/tabs/tab-header.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,15 @@ describe('MatTabHeader', () => {
6565
});
6666

6767
it('should initialize to the selected index', () => {
68+
// Recreate the fixture so we can test that it works with a non-default selected index
69+
fixture.destroy();
70+
fixture = TestBed.createComponent(SimpleTabHeaderApp);
71+
fixture.componentInstance.selectedIndex = 1;
6872
fixture.detectChanges();
69-
expect(appComponent.tabHeader.focusIndex).toBe(appComponent.selectedIndex);
73+
appComponent = fixture.componentInstance;
74+
tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement;
75+
76+
expect(appComponent.tabHeader.focusIndex).toBe(1);
7077
});
7178

7279
it('should send focus change event', () => {

0 commit comments

Comments
 (0)