addon-test-support/@ember/test-helpers/dom/trigger-key-event.js
/**
  Triggers a keyboard event of given type in the target element.
  It also requires the developer to provide either a string with the [`key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values)
  or the numeric [`keyCode`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode) of the pressed key.
  Optionally the user can also provide a POJO with extra modifiers for the event.

  @public
  @param {string|Element} target the element or selector to trigger the event on
  @param {'keydown' | 'keyup' | 'keypress'} eventType the type of event to trigger
  @param {number|string} key the `keyCode`(number) or `key`(string) of the event being triggered
  @param {Object} [modifiers] the state of various modifier keys
  @param {boolean} [modifiers.ctrlKey=false] if true the generated event will indicate the control key was pressed during the key event
  @param {boolean} [modifiers.altKey=false] if true the generated event will indicate the alt key was pressed during the key event
  @param {boolean} [modifiers.shiftKey=false] if true the generated event will indicate the shift key was pressed during the key event
  @param {boolean} [modifiers.metaKey=false] if true the generated event will indicate the meta key was pressed during the key event
  @return {Promise<void>} resolves when the application is settled
*/
Triggers a keyboard event of given type in the target element.
It also requires the developer to provide either a string with the [`key`]
or the numeric [`keyCode`] of the pressed key.
Optionally the user can also provide a POJO with extra modifiers for the event.
Triggers a keyboard event of given type in the target element . It also requires the developer to provide either a string with the [ ` key ` ] or the numeric [ ` keyCode ` ] of the pressed key . Optionally the user can also provide a POJO with extra modifiers for the event .
function triggerKeyEvent(target, eventType, key, modifiers = DEFAULT_MODIFIERS) {
  return nextTickPromise().then(() => {
    if (!target) {
      throw new Error('Must pass an element or selector to `triggerKeyEvent`.');
    }
    let element = getElement(target);
    if (!element) {
      throw new Error(`Element not found when calling \`triggerKeyEvent('${target}', ...)\`.`);
    }
    if (!eventType) {
      throw new Error(`Must provide an \`eventType\` to \`triggerKeyEvent\``);
    }
    if (KEYBOARD_EVENT_TYPES.indexOf(eventType) === -1) {
      let validEventTypes = KEYBOARD_EVENT_TYPES.join(', ');
      throw new Error(
        `Must provide an \`eventType\` of ${validEventTypes} to \`triggerKeyEvent\` but you passed \`${eventType}\`.`
      );
    }
    let props;
    if (typeof key === 'number') {
      props = { keyCode: key, which: key, key: keyFromKeyCodeAndModifiers(key, modifiers) };
    } else if (typeof key === 'string' && key.length !== 0) {
      let firstCharacter = key[0];
      if (firstCharacter !== firstCharacter.toUpperCase()) {
        throw new Error(
          `Must provide a \`key\` to \`triggerKeyEvent\` that starts with an uppercase character but you passed \`${key}\`.`
        );
      }
      if (isNumeric(key) && key.length > 1) {
        throw new Error(
          `Must provide a numeric \`keyCode\` to \`triggerKeyEvent\` but you passed \`${key}\` as a string.`
        );
      }
      let keyCode = keyCodeFromKey(key);
      props = { keyCode, which: keyCode, key };
    } else {
      throw new Error(`Must provide a \`key\` or \`keyCode\` to \`triggerKeyEvent\``);
    }
    let options = assign(props, modifiers);
    fireEvent(element, eventType, options);
    return settled();
  });
}
function triggerKeyEvent ( target , eventType , key , modifiers = DEFAULT_MODIFIERS ) { return nextTickPromise ( ) . then ( ( ) => { if ( ! target ) { throw new Error ( 'Must pass an element or selector to `triggerKeyEvent`.' ) ; } let element = getElement ( target ) ; if ( ! element ) { throw new Error ( ` \` ${ target } \` ` ) ; } if ( ! eventType ) { throw new Error ( ` \` \` \` \` ` ) ; } if ( KEYBOARD_EVENT_TYPES . indexOf ( eventType ) === - 1 ) { let validEventTypes = KEYBOARD_EVENT_TYPES . join ( ', ' ) ; throw new Error ( ` \` \` ${ validEventTypes } \` \` \` ${ eventType } \` ` ) ; } let props ; if ( typeof key === 'number' ) { props = { keyCode : key , which : key , key : keyFromKeyCodeAndModifiers ( key , modifiers ) } ; } else if ( typeof key === 'string' && key . length !== 0 ) { let firstCharacter = key [ 0 ] ; if ( firstCharacter !== firstCharacter . toUpperCase ( ) ) { throw new Error ( ` \` \` \` \` \` ${ key } \` ` ) ; } if ( isNumeric ( key ) && key . length > 1 ) { throw new Error ( ` \` \` \` \` \` ${ key } \` ` ) ; } let keyCode = keyCodeFromKey ( key ) ; props = { keyCode , which : keyCode , key } ; } else { throw new Error ( ` \` \` \` \` \` \` ` ) ; } let options = assign ( props , modifiers ) ; fireEvent ( element , eventType , options ) ; return settled ( ) ; } ) ; }
Triggers a keyboard event of given type in the target element.
Triggers a keyboard event of given type in the target element .