|
1 | 1 | import {
|
2 |
| - async, ComponentFixture, TestBed, fakeAsync, tick, discardPeriodicTasks |
| 2 | + async, |
| 3 | + ComponentFixture, |
| 4 | + TestBed, |
| 5 | + fakeAsync, |
| 6 | + tick, |
| 7 | + discardPeriodicTasks, |
3 | 8 | } from '@angular/core/testing';
|
4 | 9 | import {Component, ViewChild} from '@angular/core';
|
5 | 10 | import {CommonModule} from '@angular/common';
|
6 | 11 | import {By} from '@angular/platform-browser';
|
7 |
| -import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes'; |
| 12 | +import {ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE, HOME, END} from '@angular/cdk/keycodes'; |
8 | 13 | import {PortalModule} from '@angular/cdk/portal';
|
9 | 14 | import {Direction, Directionality} from '@angular/cdk/bidi';
|
10 | 15 | import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
|
@@ -136,6 +141,60 @@ describe('MatTabHeader', () => {
|
136 | 141 | expect(appComponent.selectedIndex).toBe(0);
|
137 | 142 | expect(spaceEvent.defaultPrevented).toBe(true);
|
138 | 143 | });
|
| 144 | + |
| 145 | + it('should move focus to the first tab when pressing HOME', () => { |
| 146 | + appComponent.tabHeader.focusIndex = 3; |
| 147 | + fixture.detectChanges(); |
| 148 | + expect(appComponent.tabHeader.focusIndex).toBe(3); |
| 149 | + |
| 150 | + const tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement; |
| 151 | + const event = dispatchKeyboardEvent(tabListContainer, 'keydown', HOME); |
| 152 | + fixture.detectChanges(); |
| 153 | + |
| 154 | + expect(appComponent.tabHeader.focusIndex).toBe(0); |
| 155 | + expect(event.defaultPrevented).toBe(true); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should skip disabled items when moving focus using HOME', () => { |
| 159 | + appComponent.tabHeader.focusIndex = 3; |
| 160 | + appComponent.tabs[0].disabled = true; |
| 161 | + fixture.detectChanges(); |
| 162 | + expect(appComponent.tabHeader.focusIndex).toBe(3); |
| 163 | + |
| 164 | + const tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement; |
| 165 | + dispatchKeyboardEvent(tabListContainer, 'keydown', HOME); |
| 166 | + fixture.detectChanges(); |
| 167 | + |
| 168 | + // Note that the second tab is disabled by default already. |
| 169 | + expect(appComponent.tabHeader.focusIndex).toBe(2); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should move focus to the last tab when pressing END', () => { |
| 173 | + appComponent.tabHeader.focusIndex = 0; |
| 174 | + fixture.detectChanges(); |
| 175 | + expect(appComponent.tabHeader.focusIndex).toBe(0); |
| 176 | + |
| 177 | + const tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement; |
| 178 | + const event = dispatchKeyboardEvent(tabListContainer, 'keydown', END); |
| 179 | + fixture.detectChanges(); |
| 180 | + |
| 181 | + expect(appComponent.tabHeader.focusIndex).toBe(3); |
| 182 | + expect(event.defaultPrevented).toBe(true); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should skip disabled items when moving focus using END', () => { |
| 186 | + appComponent.tabHeader.focusIndex = 0; |
| 187 | + appComponent.tabs[3].disabled = true; |
| 188 | + fixture.detectChanges(); |
| 189 | + expect(appComponent.tabHeader.focusIndex).toBe(0); |
| 190 | + |
| 191 | + const tabListContainer = appComponent.tabHeader._tabListContainer.nativeElement; |
| 192 | + dispatchKeyboardEvent(tabListContainer, 'keydown', END); |
| 193 | + fixture.detectChanges(); |
| 194 | + |
| 195 | + expect(appComponent.tabHeader.focusIndex).toBe(2); |
| 196 | + }); |
| 197 | + |
139 | 198 | });
|
140 | 199 |
|
141 | 200 | describe('pagination', () => {
|
|
0 commit comments