Skip to content

Commit bbf1c1e

Browse files
author
Jess
committed
(fix) use custom keyCodes or codes when provided to trigger options
1 parent a05b499 commit bbf1c1e

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

Diff for: packages/test-utils/src/create-dom-event.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import eventTypes from 'dom-event-types'
2+
import { throwError } from 'shared/util'
23

34
const defaultEventType = {
45
eventInterface: 'Event',
@@ -24,6 +25,19 @@ const modifiers = {
2425
pagedown: 34
2526
}
2627

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+
2741
function createEvent(
2842
type,
2943
modifier,
@@ -35,14 +49,12 @@ function createEvent(
3549
? window[eventInterface]
3650
: window.Event
3751

38-
const event = new SupportedEventInterface(type, {
52+
const event = new SupportedEventInterface(
53+
type,
3954
// event properties can only be added when the event is instantiated
4055
// 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+
)
4658

4759
return event
4860
}

Diff for: test/specs/wrapper/trigger.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ describeWithShallowAndMount('trigger', mountingMethod => {
3737
expect(keydownHandler.calledOnce).to.equal(true)
3838
})
3939

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+
4056
it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', () => {
4157
const keydownHandler = sandbox.stub()
4258
const wrapper = mountingMethod(ComponentWithEvents, {

0 commit comments

Comments
 (0)