2022-08-01 17:44:10 +03:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2022 Google LLC
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-10-18 19:28:12 +03:00
|
|
|
import {Harness} from '../testing/harness.js';
|
2022-08-01 17:44:10 +03:00
|
|
|
|
2023-07-24 19:28:36 +03:00
|
|
|
import {List} from './internal/list.js';
|
|
|
|
import {ListItemHarness} from './internal/listitem/harness.js';
|
|
|
|
import {ListItemEl} from './internal/listitem/list-item.js';
|
2022-08-01 17:44:10 +03:00
|
|
|
|
2023-07-24 19:28:36 +03:00
|
|
|
export {ListItemHarness} from './internal/listitem/harness.js';
|
2023-03-30 09:11:02 +03:00
|
|
|
|
2023-06-30 01:17:52 +03:00
|
|
|
const NAVIGABLE_KEYS = {
|
|
|
|
'ArrowDown': 'ArrowDown',
|
|
|
|
'ArrowUp': 'ArrowUp',
|
|
|
|
'Home': 'Home',
|
|
|
|
'End': 'End',
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keys that are handled by MdList. Keys not included in this are not handled by
|
|
|
|
* MdList and should be dispatched by yourself.
|
|
|
|
*/
|
|
|
|
export type HandledListKeys =
|
2023-10-25 21:58:21 +03:00
|
|
|
(typeof NAVIGABLE_KEYS)[keyof typeof NAVIGABLE_KEYS];
|
2023-06-30 01:17:52 +03:00
|
|
|
|
2022-08-01 17:44:10 +03:00
|
|
|
/**
|
|
|
|
* Test harness for list.
|
|
|
|
*/
|
|
|
|
export class ListHarness extends Harness<List> {
|
2022-08-02 15:04:32 +03:00
|
|
|
/**
|
|
|
|
* Returns the first list item element.
|
|
|
|
*/
|
|
|
|
protected override async getInteractiveElement() {
|
|
|
|
await this.element.updateComplete;
|
2023-09-23 01:29:39 +03:00
|
|
|
return this.element as List;
|
2022-08-02 15:04:32 +03:00
|
|
|
}
|
|
|
|
|
2022-08-01 17:44:10 +03:00
|
|
|
/** @return List item harnesses. */
|
|
|
|
getItems() {
|
2023-06-30 01:17:52 +03:00
|
|
|
return this.element.items.map(
|
2023-10-25 21:58:21 +03:00
|
|
|
(item) => new ListItemHarness(item as ListItemEl),
|
|
|
|
);
|
2023-06-30 01:17:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs keyboard events that are handled by List and makes sure that
|
|
|
|
* they are constructed in a manner that List understands.
|
|
|
|
*
|
|
|
|
* @param key The key to dispatch on the list.
|
|
|
|
*/
|
|
|
|
async pressHandledKey<T extends string = HandledListKeys>(key: T) {
|
|
|
|
await this.keypress(key, {code: key});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches a keypress on the list. It may or may not be a supported event.
|
|
|
|
*
|
|
|
|
* @param key The key to dispatch on the list.
|
|
|
|
*/
|
|
|
|
override async keypress(key: string, init = {} as KeyboardEventInit) {
|
|
|
|
init = {code: key, ...init};
|
2023-09-23 01:29:39 +03:00
|
|
|
await super.keypress(key, init);
|
2022-08-01 17:44:10 +03:00
|
|
|
}
|
|
|
|
}
|