element-plus/packages/test-utils/trigger-event.ts
Herrington Darkholme e2e3ace5db
WIP: refactor(table): refactor the test cases (#2232)
* fix(table): move common test util out of table

* refactor(table): move column test out of table

* refactor(table): remove unnecessary toArray

* refactor(table): use triggerEvent from test-util

* fix(utils): add types to triggerEvent

* fix(table): add commented test back

* refactor(table): remove done from async test

* refactor(table): remove unused expectArray

* fix(table): fix type error on triggerEvent

* refactor(table): use conciser array methods
2021-06-12 19:38:39 +08:00

30 lines
689 B
TypeScript

/**
* Trigger event
* mouseenter, mouseleave, mouseover, keyup, change, click
*/
const triggerEvent = (elm: Element, name: string, ...opts: any[]) => {
let eventName: string
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(evt)
return elm
}
export default triggerEvent