KeyEvents.js 493 B

123456789101112131415161718192021222324
  1. import {
  2. assign
  3. } from 'min-dash';
  4. /**
  5. * Create a fake key event for testing purposes.
  6. *
  7. * @param {String} key the key or code
  8. * @param {Object} [attrs]
  9. * @param {string} [attrs.type]
  10. *
  11. * @return {Event}
  12. */
  13. export function createKeyEvent(key, attrs) {
  14. if (!attrs) {
  15. attrs = {};
  16. }
  17. var event = new Event(attrs.type || 'keydown', { bubbles: true, cancelable: true });
  18. var keyAttrs = { key: key, code: key };
  19. delete attrs.type;
  20. return assign(event, keyAttrs, attrs);
  21. }