handle Submit and PreventDefault using react testing library

Viewed 23

I am trying to write test case which has submit type and has prevent default inside submit button. It looks like event is not firing in test case.

form.js


click me

  function submit(e) {
    e.preventDefault();
  }

my test case

  it("Click with correct credentials", async () => {
    await act(() => {
      render(<XYZ />, container);
    });

    const submitLogin = screen.getByTestId("submit-login-btn");

    act(() => {
      submitLogin.dispatchEvent(new MouseEvent("click", { bubbles: true }));
    });

    await expect(global.window.location.pathname).toEqual("/expectedPath");
 });

Onclick on "click me", it should redirect to /expectedPath. But test case failing, I am guessing its due to submit button and prevent default.

0 Answers
Related