In my current React project, I'm testing a component which includes, as child component, the Dialog component from Material UI.
It's supposed it has to run the onClose()
function when the user presses the Esc
key. I tested it manually and it works perfectly well. Now I'm doing the same test, this time using Jest + React Testing Library. You can see the code below:
fireEvent.keyDown(container, {
key: "Escape",
code: "Escape",
keyCode: 27,
charCode: 27
});
And the test doesn't pass. After some debugs, I've realized that the event is actually being triggered, but for some reason is not having an impact on the <Dialog/>
component.
So I did run the component on Google Chrome, and triggered the same event from the Dev Tools console with the following code:
document.dispatchEvent(
new KeyboardEvent(
"keydown", {
key: "Escape",
code: "Escape",
keyCode: 27,
charCode: 27
}
)
);
...and it doesn't work either.
What can I do to make this test pass?
container
? In the test you are firing the event on thecontainer
, but this may not be the case when using the app in the browser. – Latoshalatouche