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.
|
|
|
|
*
|
|
|
|
* @param {String|Number} key the key or keyCode/charCode
|
|
|
|
* @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 || {});
|
|
|
|
}
|