|
1 | 1 | import {FocusKeyManager} from '@angular/cdk/a11y';
|
2 | 2 | import {Directionality, Direction} from '@angular/cdk/bidi';
|
3 |
| -import {BACKSPACE, DELETE, ENTER, LEFT_ARROW, RIGHT_ARROW, SPACE, TAB} from '@angular/cdk/keycodes'; |
| 3 | +import { |
| 4 | + BACKSPACE, |
| 5 | + DELETE, |
| 6 | + ENTER, |
| 7 | + LEFT_ARROW, |
| 8 | + RIGHT_ARROW, |
| 9 | + SPACE, |
| 10 | + TAB, |
| 11 | + HOME, |
| 12 | + END, |
| 13 | +} from '@angular/cdk/keycodes'; |
4 | 14 | import {createKeyboardEvent, dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
|
5 | 15 | import {
|
6 | 16 | Component,
|
@@ -226,6 +236,36 @@ describe('MatChipList', () => {
|
226 | 236 | .toBe(initialActiveIndex, 'Expected focused item not to have changed.');
|
227 | 237 | });
|
228 | 238 |
|
| 239 | + it('should focus the first item when pressing HOME', () => { |
| 240 | + const nativeChips = chipListNativeElement.querySelectorAll('mat-chip'); |
| 241 | + const lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement; |
| 242 | + const HOME_EVENT = createKeyboardEvent('keydown', HOME, lastNativeChip); |
| 243 | + const array = chips.toArray(); |
| 244 | + const lastItem = array[array.length - 1]; |
| 245 | + |
| 246 | + lastItem.focus(); |
| 247 | + expect(manager.activeItemIndex).toBe(array.length - 1); |
| 248 | + |
| 249 | + chipListInstance._keydown(HOME_EVENT); |
| 250 | + fixture.detectChanges(); |
| 251 | + |
| 252 | + expect(manager.activeItemIndex).toBe(0); |
| 253 | + expect(HOME_EVENT.defaultPrevented).toBe(true); |
| 254 | + }); |
| 255 | + |
| 256 | + it('should focus the last item when pressing END', () => { |
| 257 | + const nativeChips = chipListNativeElement.querySelectorAll('mat-chip'); |
| 258 | + const END_EVENT = createKeyboardEvent('keydown', END, nativeChips[0]); |
| 259 | + |
| 260 | + expect(manager.activeItemIndex).toBe(-1); |
| 261 | + |
| 262 | + chipListInstance._keydown(END_EVENT); |
| 263 | + fixture.detectChanges(); |
| 264 | + |
| 265 | + expect(manager.activeItemIndex).toBe(chips.length - 1); |
| 266 | + expect(END_EVENT.defaultPrevented).toBe(true); |
| 267 | + }); |
| 268 | + |
229 | 269 | });
|
230 | 270 |
|
231 | 271 | describe('RTL', () => {
|
|
0 commit comments