Skip to content

Commit 0083b9c

Browse files
committed
fix(list-key-manager): remove handling for home and end keys
* 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 6121fa1 commit 0083b9c

File tree

8 files changed

+150
-184
lines changed

8 files changed

+150
-184
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

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

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

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

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

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

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

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

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

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

834-
const DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
805+
const DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
835806
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
836807

837808
fixture.whenStable().then(() => {
@@ -1121,7 +1092,7 @@ describe('MdAutocomplete', () => {
11211092
tick();
11221093
fixture.detectChanges();
11231094

1124-
const DOWN_ARROW_EVENT = new MockKeyboardEvent(DOWN_ARROW) as KeyboardEvent;
1095+
const DOWN_ARROW_EVENT = createKeyboardEvent('keydown', DOWN_ARROW);
11251096
fixture.componentInstance.trigger._handleKeydown(DOWN_ARROW_EVENT);
11261097
tick();
11271098
fixture.detectChanges();
@@ -1372,10 +1343,3 @@ class AutocompleteWithOnPushDelay implements OnInit {
13721343
}, 1000);
13731344
}
13741345
}
1375-
1376-
1377-
/** This is a mock keyboard event to test keyboard events in the autocomplete. */
1378-
class MockKeyboardEvent {
1379-
constructor(public keyCode: number) {}
1380-
preventDefault() {}
1381-
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@ 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} from '../core/keyboard/keycodes';
7+
import {createKeyboardEvent} from '../core/testing/event-objects';
88

9-
class FakeKeyboardEvent extends FakeEvent {
10-
constructor(keyCode: number, protected target: HTMLElement) {
11-
super(keyCode);
12-
13-
this.target = target;
14-
}
15-
}
169

1710
describe('MdChipList', () => {
1811
let fixture: ComponentFixture<any>;
@@ -117,7 +110,7 @@ describe('MdChipList', () => {
117110
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
118111
let lastNativeChip = nativeChips[nativeChips.length - 1] as HTMLElement;
119112

120-
let LEFT_EVENT = new FakeKeyboardEvent(LEFT_ARROW, lastNativeChip) as any;
113+
let LEFT_EVENT = createKeyboardEvent('keydown', LEFT_ARROW, lastNativeChip);
121114
let array = chips.toArray();
122115
let lastIndex = array.length - 1;
123116
let lastItem = array[lastIndex];
@@ -138,7 +131,7 @@ describe('MdChipList', () => {
138131
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
139132
let firstNativeChip = nativeChips[0] as HTMLElement;
140133

141-
let RIGHT_EVENT: KeyboardEvent = new FakeKeyboardEvent(RIGHT_ARROW, firstNativeChip) as any;
134+
let RIGHT_EVENT = createKeyboardEvent('keydown', RIGHT_ARROW, firstNativeChip);
142135
let array = chips.toArray();
143136
let firstItem = array[0];
144137

@@ -164,7 +157,7 @@ describe('MdChipList', () => {
164157
let nativeChips = chipListNativeElement.querySelectorAll('md-chip');
165158
let firstNativeChip = nativeChips[0] as HTMLElement;
166159

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

170163
spyOn(testComponent, 'chipSelect');
@@ -198,7 +191,7 @@ describe('MdChipList', () => {
198191
});
199192

200193
it('SPACE ignores selection', () => {
201-
let SPACE_EVENT: KeyboardEvent = new FakeEvent(SPACE) as KeyboardEvent;
194+
let SPACE_EVENT = createKeyboardEvent('keydown', SPACE);
202195
let firstChip: MdChip = chips.toArray()[0];
203196

204197
spyOn(testComponent, 'chipSelect');

0 commit comments

Comments
 (0)