1
1
import eventTypes from 'dom-event-types'
2
+ import { throwError } from 'shared/util'
2
3
3
4
const defaultEventType = {
4
5
eventInterface : 'Event' ,
@@ -24,6 +25,37 @@ const modifiers = {
24
25
pagedown : 34
25
26
}
26
27
28
+ /**
29
+ * Fixing a bug where we listen to what keyCode the user passes in.
30
+ * Previously the modifier's keyCode would win, or we would return "0"
31
+ * This puts us at risk to break users' tests, so we should detect and notify them.
32
+ * */
33
+ function onModifierAndKeyCodeMismatch ( type , modifier , finalKeyCode ) {
34
+ if ( ! modifier || ! finalKeyCode ) { return } // can't mismatch if nothing is passed in
35
+ const fromModifiers = modifiers [ modifier ]
36
+ const keysAreDifferent = fromModifiers != finalKeyCode // this will be true if the user is using modifiers and mismatched keyCodes. Coerce.
37
+ if ( fromModifiers && finalKeyCode && keysAreDifferent ) {
38
+ throwError ( `wrapper.trigger('${ type } .${ modifier } ') was called ` +
39
+ `with options.code (or keyCode) of ${ finalKeyCode } . ` +
40
+ `This does not match the modifier's code (${ fromModifiers } ). ` +
41
+ `Unable to decide which code to trigger. Please disambiguate.` )
42
+ }
43
+ }
44
+
45
+ function sanitizeOptions ( type , modifier , { bubbles, cancelable } , options ) {
46
+ const keyCode = options . code || options . keyCode || modifiers [ modifier ]
47
+ onModifierAndKeyCodeMismatch ( type , modifier , keyCode )
48
+
49
+ const derivedOptions = { keyCode, code : keyCode }
50
+
51
+ return {
52
+ bubbles,
53
+ cancelable,
54
+ ...options , // What the user passed in as the second argument to #trigger
55
+ ...derivedOptions // Computed values, not necessarily the raw values that the user passed in
56
+ }
57
+ }
58
+
27
59
function createEvent (
28
60
type ,
29
61
modifier ,
@@ -35,14 +67,11 @@ function createEvent(
35
67
? window [ eventInterface ]
36
68
: window . Event
37
69
38
- const event = new SupportedEventInterface ( type , {
70
+ const event = new SupportedEventInterface ( type ,
39
71
// event properties can only be added when the event is instantiated
40
72
// custom properties must be added after the event has been instantiated
41
- ...options ,
42
- bubbles,
43
- cancelable,
44
- keyCode : modifiers [ modifier ]
45
- } )
73
+ sanitizeOptions ( type , modifier , { bubbles, cancelable } , options )
74
+ )
46
75
47
76
return event
48
77
}
0 commit comments