File tree 2 files changed +34
-6
lines changed
2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 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,19 @@ const modifiers = {
24
25
pagedown : 34
25
26
}
26
27
28
+ function getOptions ( modifier , { bubbles, cancelable } , options ) {
29
+ const keyCode = modifiers [ modifier ] || options . keyCode || options . code
30
+
31
+ const derivedOptions = { keyCode, code : keyCode }
32
+
33
+ return {
34
+ ...options , // What the user passed in as the second argument to #trigger
35
+ bubbles,
36
+ cancelable,
37
+ ...derivedOptions // Computed values, not necessarily the raw values that the user passed in
38
+ }
39
+ }
40
+
27
41
function createEvent (
28
42
type ,
29
43
modifier ,
@@ -35,14 +49,12 @@ function createEvent(
35
49
? window [ eventInterface ]
36
50
: window . Event
37
51
38
- const event = new SupportedEventInterface ( type , {
52
+ const event = new SupportedEventInterface (
53
+ type ,
39
54
// event properties can only be added when the event is instantiated
40
55
// custom properties must be added after the event has been instantiated
41
- ...options ,
42
- bubbles,
43
- cancelable,
44
- keyCode : modifiers [ modifier ]
45
- } )
56
+ getOptions ( modifier , { bubbles, cancelable } , options )
57
+ )
46
58
47
59
return event
48
60
}
Original file line number Diff line number Diff line change @@ -37,6 +37,22 @@ describeWithShallowAndMount('trigger', mountingMethod => {
37
37
expect ( keydownHandler . calledOnce ) . to . equal ( true )
38
38
} )
39
39
40
+ it ( 'causes keydown handler to fire with the appropriate keyCode when wrapper.trigger("keydown", { keyCode: 65 }) is fired on a Component' , ( ) => {
41
+ const keydownHandler = sandbox . stub ( )
42
+ const wrapper = mountingMethod ( ComponentWithEvents , {
43
+ propsData : { keydownHandler }
44
+ } )
45
+ wrapper . find ( '.keydown' ) . trigger ( 'keydown' , { keyCode : 65 } )
46
+
47
+ const keyboardEvent = keydownHandler . getCall ( 0 ) . args [ 0 ]
48
+
49
+ // Unfortunately, JSDom will give different types than PhantomJS for the keyCodes.
50
+ // parseInt to normalize. I can't find a matcher here https://www.chaijs.com/api/bdd/
51
+ // that relies on (`==`) instead of strict equals (`===`)
52
+ expect ( parseInt ( keyboardEvent . keyCode , 10 ) ) . to . equal ( 65 )
53
+ expect ( parseInt ( keyboardEvent . code , 10 ) ) . to . equal ( 65 )
54
+ } )
55
+
40
56
it ( 'causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component' , ( ) => {
41
57
const keydownHandler = sandbox . stub ( )
42
58
const wrapper = mountingMethod ( ComponentWithEvents , {
You can’t perform that action at this time.
0 commit comments