Skip to content

Trigger options key code fix (fix #1285) #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/test-utils/src/create-dom-event.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import eventTypes from 'dom-event-types'
import { warn } from 'shared/util'

const defaultEventType = {
eventInterface: 'Event',
Expand Down Expand Up @@ -35,13 +36,20 @@ function createEvent(
? window[eventInterface]
: window.Event

if (options && options.keyCode && modifier) {
warn(
`event modifier ".${modifier}" will be overridden ` +
`by specified key code "${options.keyCode}"`
)
}

const event = new SupportedEventInterface(type, {
// event properties can only be added when the event is instantiated
// custom properties must be added after the event has been instantiated
keyCode: modifiers[modifier],
...options,
bubbles,
cancelable,
keyCode: modifiers[modifier]
cancelable
})

return event
Expand All @@ -50,11 +58,13 @@ function createEvent(
function createOldEvent(
type,
modifier,
{ eventInterface, bubbles, cancelable }
{ eventInterface, bubbles, cancelable },
options
) {
const event = document.createEvent('Event')
event.initEvent(type, bubbles, cancelable)
event.keyCode = modifiers[modifier]
event.keyCode =
options && options.keyCode ? options.keyCode : modifiers[modifier]
return event
}

Expand All @@ -66,7 +76,7 @@ export default function createDOMEvent(type, options) {
const event =
typeof window.Event === 'function'
? createEvent(eventType, modifier, meta, options)
: createOldEvent(eventType, modifier, meta)
: createOldEvent(eventType, modifier, meta, options)

const eventPrototype = Object.getPrototypeOf(event)
Object.keys(options || {}).forEach(key => {
Expand Down
26 changes: 26 additions & 0 deletions test/specs/wrapper/trigger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { itDoNotRunIf } from 'conditional-specs'
describeWithShallowAndMount('trigger', mountingMethod => {
const sandbox = sinon.createSandbox()

beforeEach(() => {
sandbox.stub(console, 'error').callThrough()
})

afterEach(() => {
sandbox.reset()
sandbox.restore()
Expand Down Expand Up @@ -76,6 +80,20 @@ describeWithShallowAndMount('trigger', mountingMethod => {
}
})

it('adds key code to event', () => {
const keyupHandler = sandbox.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keyupHandler }
})
// All printable keys
for (let keyCode = 49; keyCode <= 90; keyCode++) {
wrapper.find('.keydown').trigger(`keyup`, {
keyCode: keyCode
})
expect(keyupHandler.lastCall.args[0].keyCode).to.equal(keyCode)
}
})

it('causes DOM to update after clickHandler method that changes components data is called', async () => {
const wrapper = mountingMethod(ComponentWithEvents)
const toggle = wrapper.find('.toggle')
Expand Down Expand Up @@ -163,6 +181,14 @@ describeWithShallowAndMount('trigger', mountingMethod => {
}
)

it('throws Vue warning when both modifier and keyCode are specified in event', () => {
const wrapper = mountingMethod(ComponentWithEvents)
wrapper.find('.keydown').trigger(`keyup.enter`, {
keyCode: 49
})
expect(console.error).calledWith(sandbox.match('[vue-test-utils]'))
})

it('throws error if options contains a target value', () => {
const wrapper = mountingMethod({ render: h => h('div') })
const div = wrapper.find('div')
Expand Down