1

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3335796
  • 1,227
  • 1
  • 17
  • 24

0 Answers0