Skip to content

Commit

Permalink
fix(cdk/a11y): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BobobUnicorn committed Nov 22, 2023
1 parent abfa4a3 commit e9cfe0e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/cdk/a11y/key-manager/list-key-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ describe('Key managers', () => {

keyManager.setActiveItem(0);
itemList.reset([new FakeFocusable('zero'), ...itemList.toArray()]);
itemList.notifyOnChanges();
keyManager.setActiveItem(0);

expect(spy).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -342,6 +343,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[1].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

// Next event should skip past disabled item from 0 to 2
keyManager.onKeydown(this.nextKeyEvent);
Expand All @@ -367,6 +369,7 @@ describe('Key managers', () => {
items[1].disabled = undefined;
items[2].disabled = undefined;
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.onKeydown(this.nextKeyEvent);
expect(keyManager.activeItemIndex)
Expand Down Expand Up @@ -416,6 +419,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[2].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.onKeydown(this.nextKeyEvent);
expect(keyManager.activeItemIndex)
Expand Down Expand Up @@ -558,6 +562,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[0].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.setFirstItemActive();
expect(keyManager.activeItemIndex)
Expand All @@ -580,6 +585,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[2].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.setLastItemActive();
expect(keyManager.activeItemIndex)
Expand All @@ -602,6 +608,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[1].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

expect(keyManager.activeItemIndex)
.withContext(`Expected first item of the list to be active.`)
Expand Down Expand Up @@ -629,6 +636,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[1].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.onKeydown(fakeKeyEvents.downArrow);
keyManager.onKeydown(fakeKeyEvents.downArrow);
Expand Down Expand Up @@ -706,6 +714,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items.forEach(item => (item.disabled = true));
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.onKeydown(fakeKeyEvents.downArrow);
});
Expand All @@ -730,6 +739,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[1].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

expect(keyManager.activeItemIndex).toBe(0);

Expand All @@ -744,6 +754,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[1].skipItem = true;
itemList.reset(items);
itemList.notifyOnChanges();

expect(keyManager.activeItemIndex).toBe(0);

Expand Down Expand Up @@ -839,6 +850,7 @@ describe('Key managers', () => {
new FakeFocusable('две'),
new FakeFocusable('три'),
]);
itemList.notifyOnChanges();

const keyboardEvent = createKeyboardEvent('keydown', 68, 'д');

Expand All @@ -854,6 +866,7 @@ describe('Key managers', () => {
new FakeFocusable('321'),
new FakeFocusable('`!?'),
]);
itemList.notifyOnChanges();

keyManager.onKeydown(createKeyboardEvent('keydown', 192, '`')); // types "`"
tick(debounceInterval);
Expand All @@ -874,6 +887,7 @@ describe('Key managers', () => {
const items = itemList.toArray();
items[0].disabled = true;
itemList.reset(items);
itemList.notifyOnChanges();

keyManager.onKeydown(createKeyboardEvent('keydown', 79, 'o')); // types "o"
tick(debounceInterval);
Expand All @@ -889,6 +903,7 @@ describe('Key managers', () => {
new FakeFocusable('Boromir'),
new FakeFocusable('Aragorn'),
]);
itemList.notifyOnChanges();

keyManager.setActiveItem(1);
keyManager.onKeydown(createKeyboardEvent('keydown', 66, 'b'));
Expand All @@ -905,6 +920,7 @@ describe('Key managers', () => {
new FakeFocusable('Boromir'),
new FakeFocusable('Aragorn'),
]);
itemList.notifyOnChanges();

keyManager.setActiveItem(3);
keyManager.onKeydown(createKeyboardEvent('keydown', 66, 'b'));
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/a11y/key-manager/list-key-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export class ListKeyManager<T extends ListKeyManagerOption> {
// items aren't being collected via `ViewChildren` or `ContentChildren`).
if (_items instanceof QueryList) {
this._itemChangesSubscription = _items.changes.subscribe((newItems: QueryList<T>) => {
const itemArray = newItems.toArray();
this._typeahead?.setItems(itemArray);
if (this._activeItem) {
const itemArray = newItems.toArray();
const newIndex = itemArray.indexOf(this._activeItem);
this._typeahead?.setItems(itemArray);

if (newIndex > -1 && newIndex !== this._activeItemIndex) {
this._activeItemIndex = newIndex;
Expand Down
20 changes: 10 additions & 10 deletions src/cdk/a11y/key-manager/typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export class Typeahead<T extends TypeaheadItem> {
this._skipPredicateFn = config.skipPredicate;
}

if (
(typeof ngDevMode === 'undefined' || ngDevMode) &&
initialItems.length &&
initialItems.some(item => typeof item.getLabel !== 'function')
) {
console.error('failed', initialItems);
throw new Error('KeyManager items in typeahead mode must implement the `getLabel` method.');
}

this.setItems(initialItems);
this._setupKeyHandler(typeAheadInterval);
}
Expand All @@ -51,14 +60,6 @@ export class Typeahead<T extends TypeaheadItem> {
}

setItems(items: T[]) {
if (
(typeof ngDevMode === 'undefined' || ngDevMode) &&
items.length &&
items.some(item => typeof item.getLabel !== 'function')
) {
throw new Error('KeyManager items in typeahead mode must implement the `getLabel` method.');
}

this._items = items;
}

Expand All @@ -79,13 +80,12 @@ export class Typeahead<T extends TypeaheadItem> {
return this._pressedLetters.length > 0;
}

// TODO: find a better name?
/** Resets the currently stored sequence of typed letters. */
reset(): void {
this._pressedLetters = [];
}

private _setupKeyHandler(typeAheadInterval: number) {
// TODO: handle unsubscription
// Debounce the presses of non-navigational keys, collect the ones that correspond to letters
// and convert those letters back into a string. Afterwards find the first item that starts
// with that string and select it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export class VimTreeKeyManager<T extends TreeKeyManagerItem> implements TreeKeyM
}
}

destroy() {
this.change.complete();
}

/** Stream that emits any time the focused item changes. */
readonly change = new Subject<T | null>();

Expand Down

0 comments on commit e9cfe0e

Please sign in to comment.