0
0
0Bannon2021-02-11 04:39:02
Software testing
0Bannon, 2021-02-11 04:39:02

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);
  });
}

test
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();
  });
});

Throws an error and showHideButton is read-only. How to do it right?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question