I mocked my login component using react testing library, did the necessary "getBy's" and used fireEvent to submit or click on the submit button. I rendered the component with an onSubmit function but it still doesn't get called.
it("Calls onSubmit with username and Password when submitted", () => {
const onSubmit = jest.fn();
const { getByTestId, getByText } = render(
<MemoryRouter>
<Login onSubmit = {onSubmit} />
</MemoryRouter>
);
const button = getByText("Login");
const email = getByTestId("email");
const password = getByTestId("password");
const form = getByTestId("form-element");
fireEvent.change(email, { target: { value: "Tolulope@gmail.com" } });
fireEvent.change(password, { target: { value: "YummyPizza" } });
fireEvent.click(button);
expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit).toHaveBeenCalledWith({
email: email.value,
password: password.value,
});
});
I tried but fireEvent.submit and fireEvent.click on the form and button respectively but both seems not to trigger the function.
Aucun commentaire:
Enregistrer un commentaire