Skip to content

fix(trigger): set spec-compliant event.code on keydown/keyup#2850

Merged
cexbrayat merged 1 commit into
vuejs:mainfrom
AhmadHannan037:fix/keydown-event-code
May 11, 2026
Merged

fix(trigger): set spec-compliant event.code on keydown/keyup#2850
cexbrayat merged 1 commit into
vuejs:mainfrom
AhmadHannan037:fix/keydown-event-code

Conversation

@AhmadHannan037
Copy link
Copy Markdown
Contributor

Fixes #2544.

trigger('keydown.enter') (and other keydown/keyup modifiers) was setting event.code to the numeric keyCode (e.g. 13) instead of the W3C UI Events string identifier ("Enter"). Tests checking event.code for accessibility/keyboard-navigation handlers couldn't pass because of this.

Change
Added codesByKeyName next to keyCodesByKeyName in src/constants/dom-events.ts, mapping every supported key modifier to its W3C code string.
createDomEvent.ts now picks event.code from codesByKeyName when a key modifier is present. Explicit options.code still takes priority. The numeric keyCode fallback is kept for legacy callers that pass { keyCode } without a modifier, so existing behavior is unchanged.
Coverage
Added a sibling test in tests/trigger.spec.ts (next to the existing keyCode-mapping test) that iterates codesByKeyName and asserts the handler receives the correct event.code string for every modifier.

Affected modifiers: backspace, tab, enter, esc, space, pageup, pagedown, end, home, left, up, right, down, insert, delete.

…ey modifiers

`trigger('keydown.enter')` previously set `event.code` to the numeric
keyCode (e.g. 13) instead of the W3C string identifier (`Enter`). Add a
`codesByKeyName` map alongside `keyCodesByKeyName` and use it for `code`
when a key modifier is present. The numeric `keyCode` fallback is kept
for callers that pass `{ keyCode }` without a modifier, so existing
behavior is unchanged.

Closes vuejs#2544
@netlify
Copy link
Copy Markdown

netlify Bot commented May 6, 2026

Deploy Preview for vue-test-utils-docs ready!

Name Link
🔨 Latest commit caef20a
🔍 Latest deploy log https://app.netlify.com/projects/vue-test-utils-docs/deploys/69fb78ca867d7e0008c6f069
😎 Deploy Preview https://deploy-preview-2850--vue-test-utils-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@vue/test-utils@2850

commit: caef20a

Copy link
Copy Markdown
Member

@cexbrayat cexbrayat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cexbrayat cexbrayat merged commit d224b09 into vuejs:main May 11, 2026
9 checks passed
@AhmadHannan037
Copy link
Copy Markdown
Contributor Author

Thanks @AhmadHannan037

Thanks for the quick review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants