Skip to content

Commit 2d16345

Browse files
crisbetotinayuangao
authored andcommitted
fix(list-key-manager): remove handling for home and end keys (#4544)
* Removes the handling for the home and end keys from the ListKeyManager since their usage isn't as generic as the arrow keys. * Adds the home and end key handling to the select specifically. * Reworks the Autocomplete, ListKeyManager and ChipList tests to use the `createKeyboardEvent`, instead of making their own fake keyboard events. * Adds a `target` parameter to the `createKeyboardEvent` function, allowing us to define the event target. Fixes #3496.
1 parent c53bbba commit 2d16345

File tree

8 files changed

+149
-185
lines changed

8 files changed

+149
-185
lines changed

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import {MdInputModule} from '../input/index';
1616
import {Dir, LayoutDirection} from '../core/rtl/dir';
1717
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
1818
import {Subscription} from 'rxjs/Subscription';
19-
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW, HOME, END} from '../core/keyboard/keycodes';
19+
import {ENTER, DOWN_ARROW, SPACE, UP_ARROW} from '../core/keyboard/keycodes';
2020
import {MdOption} from '../core/option/option';
2121
import {MdAutocomplete} from './autocomplete';
2222
import {MdInputContainer} from '../input/input-container';
2323
import {Observable} from 'rxjs/Observable';
2424
import {Subject} from 'rxjs/Subject';
2525
import {dispatchFakeEvent} from '../core/testing/dispatch-events';
26+
import {createKeyboardEvent} from '../core/testing/event-objects';
2627
import {typeInElement} from '../core/testing/type-in-element';
2728
import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher';
2829

@@ -535,8 +536,8 @@ describe('MdAutocomplete', () => {
535536
fixture.detectChanges();
536537

537538
input = fixture.debugElement.query(By.css('input')).nativeElement;
538-
DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
539-
ENTER_EVENT = new MockKeyboardEvent(ENTER) as KeyboardEvent;
539+
DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
540+
ENTER_EVENT = createKeyboardEvent('keydown', ENTER);
540541

541542
fixture.componentInstance.trigger.openPanel();
542543
fixture.detectChanges();
@@ -594,7 +595,7 @@ describe('MdAutocomplete', () => {
594595
const optionEls =
595596
overlayContainerElement.querySelectorAll('md-option') as NodeListOf<HTMLElement>;
596597

597-
const UP_ARROW_EVENT = new MockKeyboardEvent(UP_ARROW) as KeyboardEvent;
598+
const UP_ARROW_EVENT = createKeyboardEvent('keydown', UP_ARROW);
598599
fixture.componentInstance.trigger._handleKeydown(UP_ARROW_EVENT);
599600
tick();
600601
fixture.detectChanges();
@@ -668,7 +669,7 @@ describe('MdAutocomplete', () => {
668669
typeInElement('New', input);
669670
fixture.detectChanges();
670671

671-
const SPACE_EVENT = new MockKeyboardEvent(SPACE) as KeyboardEvent;
672+
const SPACE_EVENT = createKeyboardEvent('keydown', SPACE);
672673
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
673674

674675
fixture.whenStable().then(() => {
@@ -748,7 +749,7 @@ describe('MdAutocomplete', () => {
748749
const scrollContainer =
749750
document.querySelector('.cdk-overlay-pane .mat-autocomplete-panel');
750751

751-
const UP_ARROW_EVENT = new MockKeyboardEvent(UP_ARROW) as KeyboardEvent;
752+
const UP_ARROW_EVENT = createKeyboardEvent('keydown', UP_ARROW);
752753
fixture.componentInstance.trigger._handleKeydown(UP_ARROW_EVENT);
753754
tick();
754755
fixture.detectChanges();
@@ -757,36 +758,6 @@ describe('MdAutocomplete', () => {
757758
expect(scrollContainer.scrollTop).toEqual(272, `Expected panel to reveal last option.`);
758759
}));
759760

760-
it('should scroll the active option into view when pressing END', fakeAsync(() => {
761-
tick();
762-
const scrollContainer =
763-
document.querySelector('.cdk-overlay-pane .mat-autocomplete-panel');
764-
765-
const END_EVENT = new MockKeyboardEvent(END) as KeyboardEvent;
766-
fixture.componentInstance.trigger._handleKeydown(END_EVENT);
767-
tick();
768-
fixture.detectChanges();
769-
770-
// Expect option bottom minus the panel height (528 - 256 = 272)
771-
expect(scrollContainer.scrollTop).toEqual(272, 'Expected panel to reveal the last option.');
772-
}));
773-
774-
it('should scroll the active option into view when pressing HOME', fakeAsync(() => {
775-
tick();
776-
const scrollContainer =
777-
document.querySelector('.cdk-overlay-pane .mat-autocomplete-panel');
778-
779-
scrollContainer.scrollTop = 100;
780-
fixture.detectChanges();
781-
782-
const HOME_EVENT = new MockKeyboardEvent(HOME) as KeyboardEvent;
783-
fixture.componentInstance.trigger._handleKeydown(HOME_EVENT);
784-
tick();
785-
fixture.detectChanges();
786-
787-
expect(scrollContainer.scrollTop).toEqual(0, 'Expected panel to reveal the first option.');
788-
}));
789-
790761
});
791762

792763
describe('aria', () => {
@@ -832,7 +803,7 @@ describe('MdAutocomplete', () => {
832803
expect(input.hasAttribute('aria-activedescendant'))
833804
.toBe(false, 'Expected aria-activedescendant to be absent if no active item.');
834805

835-
const DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
806+
const DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
836807
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
837808

838809
fixture.whenStable().then(() => {
@@ -1140,7 +1111,7 @@ describe('MdAutocomplete', () => {
11401111
tick();
11411112
fixture.detectChanges();
11421113

1143-
const DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
1114+
const DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
11441115
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
11451116
tick();
11461117
fixture.detectChanges();
@@ -1418,10 +1389,3 @@ class AutocompleteWithNativeInput {
14181389
});
14191390
}
14201391
}
1421-
1422-
1423-
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
1424-
class MockKeyboardEvent {
1425-
constructor(public keyCode: number) {}
1426-
preventDefault() {}
1427-
}

src/lib/chips/chip-list.spec.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@ import {Component, DebugElement, QueryList} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {MdChip, MdChipList, MdChipsModule} from './index';
55
import {FocusKeyManager} from '../core/a11y/focus-key-manager';
6-
import {FakeEvent} from '../core/a11y/list-key-manager.spec';
76
import {SPACE, LEFT_ARROW, RIGHT_ARROW, TAB} from '../core/keyboard/keycodes';
87
import {createKeyboardEvent} from '../core/testing/event-objects';
98

109

11-
class FakeKeyboardEvent extends FakeEvent {
12-
constructor(keyCode: number, protected target: HTMLElement) {
13-
super(keyCode);
14-
15-
this.target = target;
16-
}
17-
}
18-
1910
describe('MdChipList', () => {
2011
let fixture: ComponentFixture<any>;
2112
let chipListDebugElement: DebugElement;
@@ -117,7 +108,7 @@ describe('MdChipList', () => {
117108
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
118109
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
119110

120-
let LEFT_EVENT = new FakeKeyboardEvent(LEFT_ARROW, lastNativeChip) as any;
111+
let LEFT_EVENT = createKeyboardEvent('keydown', LEFT_ARROW, lastNativeChip);
121112
let array = chips.toArray();
122113
let lastIndex = array.length - 1;
123114
let lastItem = array[lastIndex];
@@ -138,7 +129,7 @@ describe('MdChipList', () => {
138129
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
139130
let firstNativeChip = nativeChips[0] as HTMLElement;
140131

141-
let RIGHT_EVENT: KeyboardEvent = new FakeKeyboardEvent(RIGHT_ARROW, firstNativeChip) as any;
132+
let RIGHT_EVENT = createKeyboardEvent('keydown', RIGHT_ARROW, firstNativeChip);
142133
let array = chips.toArray();
143134
let firstItem = array[0];
144135

@@ -164,7 +155,7 @@ describe('MdChipList', () => {
164155
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
165156
let firstNativeChip = nativeChips[0] as HTMLElement;
166157

167-
let SPACE_EVENT: KeyboardEvent = new FakeKeyboardEvent(SPACE, firstNativeChip) as any;
158+
let SPACE_EVENT = createKeyboardEvent('keydown', SPACE, firstNativeChip);
168159
let firstChip: MdChip = chips.toArray()[0];
169160

170161
spyOn(testComponent, 'chipSelect');
@@ -209,7 +200,7 @@ describe('MdChipList', () => {
209200
});
210201

211202
it('SPACE ignores selection', () => {
212-
let SPACE_EVENT: KeyboardEvent = new FakeEvent(SPACE) as KeyboardEvent;
203+
let SPACE_EVENT = createKeyboardEvent('keydown', SPACE);
213204
let firstChip: MdChip = chips.toArray()[0];
214205

215206
spyOn(testComponent, 'chipSelect');

0 commit comments

Comments
 (0)