11import eventTypes from 'dom-event-types'
2+ import { throwError } from 'shared/util'
23
34const defaultEventType = {
45 eventInterface : 'Event' ,
@@ -24,6 +25,37 @@ const modifiers = {
2425 pagedown : 34
2526}
2627
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+
2759function createEvent (
2860 type ,
2961 modifier ,
@@ -35,14 +67,11 @@ function createEvent(
3567 ? window [ eventInterface ]
3668 : window . Event
3769
38- const event = new SupportedEventInterface ( type , {
70+ const event = new SupportedEventInterface ( type ,
3971 // event properties can only be added when the event is instantiated
4072 // 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+ )
4675
4776 return event
4877}
0 commit comments