import { waitForUpdatesAsync } from '../../testing/async-helpers';
export async function clickElement(element) {
    element.click();
    await waitForUpdatesAsync();
}
export async function sendKeyDownEvent(target, key, init) {
    return (await sendKeyDownEvents(target, [key], init ? [init] : undefined))[0];
}
export async function sendKeyDownEvents(target, keySequence, init) {
    if (keySequence.length === 0) {
        throw new Error('The key sequence must have at least one key.');
    }
    if (init && init.length !== keySequence.length) {
        throw new Error('The length of the key sequence and the init array must match.');
    }
    const events = [];
    for (const [index, key] of keySequence.entries()) {
        const event = new KeyboardEvent('keydown', {
            key,
            cancelable: true,
            bubbles: true,
            ...init?.[index]
        });
        target.dispatchEvent(event);
        events.push(event);
    }
    await waitForUpdatesAsync();
    return events;
}
/** A helper function to abstract turning waiting for an event to fire into a promise.
 * The returned promise should be resolved prior to completing a test.
 */
export async function waitForEvent(element, eventName, callback) {
    await new Promise(resolve => {
        const handler = ((evt) => {
            callback?.(evt);
            resolve();
        });
        element.addEventListener(eventName, handler, {
            once: true
        });
    });
}
/**
 * Waits for a requested animation frame to occur.
 */
export async function waitAnimationFrame() {
    await new Promise(resolve => {
        requestAnimationFrame(() => resolve());
    });
}
/**
 * Timeout for use in async tets.
 */
export async function waitTimeout(ms = 0) {
    await new Promise(resolve => {
        window.setTimeout(() => resolve(undefined), ms);
    });
}
//# sourceMappingURL=component.js.map