mirror of
https://github.com/element-plus/element-plus.git
synced 2024-12-21 02:50:11 +08:00
e2e3ace5db
* 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
30 lines
689 B
TypeScript
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
|