element-plus/packages/test-utils/trigger-event.ts
zazzaz f15d61ddbc
fix: fix timepicker keyevent bug (#1151)
* fix: fix timepicker keyevent bug

fix #904

* chore: update

* chore: update

* chore: update
2020-12-31 11:12:52 +08:00

34 lines
788 B
TypeScript

/**
* Trigger event
* mouseenter, mouseleave, mouseover, keyup, change, click
* @param {Element} elm
* @param {String} name
* @param {*} opts
*/
const triggerEvent = (elm, name, ...opts) => {
let eventName
if (/^mouse|click/.test(name)) {
eventName = 'MouseEvents'
} else if (/^key/.test(name)) {
eventName = 'KeyboardEvent'
} else {
eventName = 'HTMLEvents'
}
const evt = document.createEvent(eventName)
evt.initEvent(name, ...opts)
if (name === 'keydown' && opts[0]) {
// trigger event with keycode
// triggerEvent(ele, 'keydown', 'ArrowDown')
Object.defineProperty(evt, 'code', { value: opts[0] })
}
elm.dispatchEvent
? elm.dispatchEvent(evt)
: elm.fireEvent('on' + name, evt)
return elm
}
export default triggerEvent