Skip to content

Instantly share code, notes, and snippets.

@jgwhite
Created February 23, 2018 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgwhite/a36da6217bde4d23a9434bfda3172b8c to your computer and use it in GitHub Desktop.
Save jgwhite/a36da6217bde4d23a9434bfda3172b8c to your computer and use it in GitHub Desktop.
import { focus, triggerKeyEvent } from '@ember/test-helpers';
import findButton from './find-button';
import { calculateTabsTo } from './tabbability';
const KEY_RETURN = 13;
export default async function keyboardClick(text) {
let element = findButton(text);
if (!element) {
throw new Error(`Could not find a button containing "${text}"`);
}
let tabs = calculateTabsTo(element);
if (tabs < 0) {
throw new Error(`The user would have to tab backwards to reach the button containing "${text}"`);
}
await focus(element);
await triggerKeyEvent(element, 'keydown', KEY_RETURN);
await triggerKeyEvent(element, 'keypress', KEY_RETURN);
await triggerKeyEvent(element, 'keyup', KEY_RETURN);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment