forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-dom-event.js
59 lines (51 loc) · 1.2 KB
/
create-dom-event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import eventTypes from 'dom-event-types'
const defaultEventType = {
eventInterface: 'Event',
cancelable: true,
bubbles: true
}
const modifiers = {
enter: 13,
tab: 9,
delete: 46,
esc: 27,
space: 32,
up: 38,
down: 40,
left: 37,
right: 39,
end: 35,
home: 36,
backspace: 8,
insert: 45,
pageup: 33,
pagedown: 34
}
export default function createDOMEvent (type, options) {
const [eventType, modifier] = type.split('.')
const {
eventInterface,
bubbles,
cancelable
} = eventTypes[eventType] || defaultEventType
if (typeof window.Event === 'function') {
const SupportedEventInterface =
typeof window[eventInterface] === 'function'
? window[eventInterface]
: window.Event
return new SupportedEventInterface(eventType, {
bubbles,
cancelable,
...options,
keyCode: modifiers[modifier]
})
}
// Fallback for IE10,11 - https://stackoverflow.com/questions/26596123
const eventObject = document.createEvent('Event')
eventObject.initEvent(eventType, bubbles, cancelable)
Object.keys(options || {}).forEach(key => {
eventObject[key] = options[key]
})
eventObject.keyCode = modifiers[modifier]
return eventObject
}