Answer the question
In order to leave comments, you need to log in
Jest how to test function call from eventListener'a?
I have a button, it has a click handler, an arrow function fires, another showHideButton function is called in it. Everything is simple. But how can I check that this function has been called? I've already tried everything I could. Just learning Jest.
export function initListeners(app) {
const button = app.querySelector("button");
const input = app.querySelector("input");
button.addEventListener("click", (ev) => {
ev.preventDefault();
showHideButton(button, input.value);
});
}
import { showHideButton } from "./initListeners";
const app = document.querySelector("#app");
const button = document.body.querySelector("button");
initListeners(app);
describe("this suit tests event listener", () => {
it("checks event listener", () => {
showHideButton = jest.fn();
button.dispatchEvent(new Event("click"));
expect(showHideButton).toHaveBeenCalled();
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question