I am trying to test a React component. It is an input element that on button click the value is set to "".
My test keeps on failing but I am confused because I can see that it does in fact work on button click.
Heres the test
const mockedSetSearch = jest.fn();
const mockedSearch = "Hello world";
it("Input should be cleared on 'clear' button click", () => {
render(
<PostSearch setUserSearch={mockedSetSearch} userSearch={mockedSearch} />
);
const inputElement = screen.getByPlaceholderText(
/Search posts/i
) as HTMLInputElement;
fireEvent.change(inputElement, { target: { value: "Hello World" } });
const buttonElement = screen.getByRole("button", { name: /clear/i });
fireEvent.click(buttonElement);
// LINE BELOW FAILS THE TEST
expect(inputElement.value).toBe("");
});
P.S I am using TypeScript + Nextjs