2018-11-21 08:38:11 +00:00
|
|
|
import {
|
|
|
|
isString,
|
|
|
|
assign
|
|
|
|
} from 'min-dash';
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2018-11-21 08:38:11 +00:00
|
|
|
/**
|
|
|
|
* Create a fake key event for testing purposes.
|
|
|
|
*
|
2020-04-06 08:25:03 +00:00
|
|
|
* @param {string|number} key the key or keyCode/charCode
|
2018-11-21 08:38:11 +00:00
|
|
|
* @param {Object} [attrs]
|
|
|
|
*
|
|
|
|
* @return {Event}
|
|
|
|
*/
|
|
|
|
export function createKeyEvent(key, attrs) {
|
|
|
|
var event = document.createEvent('Events') || new document.defaultView.CustomEvent('keyEvent');
|
2017-10-11 13:19:36 +00:00
|
|
|
|
2018-11-21 08:38:11 +00:00
|
|
|
// init and mark as bubbles / cancelable
|
|
|
|
event.initEvent('keydown', false, true);
|
|
|
|
|
|
|
|
var keyAttrs = isString(key) ? { key: key } : { keyCode: key, which: key };
|
|
|
|
|
|
|
|
return assign(event, keyAttrs, attrs || {});
|
|
|
|
}
|