question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't dispatch KeyboardEvent in Jsdom

See original GitHub issue

Hi, I’m using jsdom in my unit test, and I need test mouse event and keyboard event. Code like this will trigger click event I bind to “svg” element.

var event = new window.MouseEvent("click");
document.querySelector('svg').dispatchEvent(event);

But code like this will not trigger keydown event I bind to document or body, why?

var event = new window.KeyboardEvent("keydown", {key: "z", char: "z", keyCode: 90});
document.dispatchEvent(event);

It will work in browser, but not work in jsdom. where I was wrong?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
spencommented, Jan 25, 2017

A re-jig of @i8-pi’s example worked for me! I thought I’d post for anybody else who finds their way to this issue 😁

const onEscapeSpy = spy();
const wrapper = mount( <CloseOnEscape onEscape={ onEscapeSpy } /> );
const event = new window.KeyboardEvent( 'keydown', { keyCode: 27 } );
document.dispatchEvent( event );

expect( onEscapeSpy ).to.have.been.calledOnce;

Basically: create a new window.KeyboardEvent > dispatch that event on document > 👌

4reactions
ronencommented, Nov 7, 2018

In case anybody (like me) finds this old issue because they’re trying to dispatch a keyboard event from a specific node but intending to handle it in the document with event.target set appropriately (which is how KeyboardEventHandler works ), there are two things to note:

  • You need to specify bubbles: true in the event so that it will bubble to the document
  • If you’re coming from enzyme, note that mount() does not put the component in the document, you need to specify attachTo: with a node that’s in the document (React will complain if you try to directly attach to document.body)

A re-jig of @spen’s code:

const onEscapeSpy = spy();
document.addEventListener( 'keydown', onEscapeSpy )

const root = document.createElement( 'div' );
document.body.appendChild( root );
const wrapper = mount( <MyComponent />, { attachTo: root } );

const event = new window.KeyboardEvent( 'keydown', { keyCode: 27, bubbles: true } );
const targetNode = wrapper.getDOMNode();
targetNode.dispatchEvent( event );

expect( onEscapeSpy ).to.have.been.calledOnce;
expect( spy.lastCall.lastArg.target ).to.equal( targetNode ); 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Can't dispatch KeyboardEvent in Jsdom -
Hi, I'm using jsdom in my unit test, and I need test mouse event and keyboard event. Code like this will trigger click...
Read more >
dispatching keyboard event does nothing in Jest test ...
So the problem was that I needed to programatically focus on the input first!
Read more >
Simulate Keypress Event - Discuss - ProseMirror
I'm trying to use Jest (JSDOM) to test some keypress events, and your method does work for a registered shortcut. But if the...
Read more >
Changelog.md - jest-environment-jsdom-fourteen
JSDOM.fragment() now creates fragments whose document has no browsing context ... Added getModifierState() to MouseEvent and KeyboardEvent .
Read more >
The KeyboardEvent
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found