I am trying to test the useState functionality which is also updates when useEffect is called.
The thing I am trying to test is at the initial stage the useState value is false then it changes on a click event.
Please see the code I tried below:
it("should have initial state false", async () => {
const setStateMock = jest.fn();
const useSateMock = (useState) => [useState, setStateMock];
jest.spyOn(React, "useState").mockImplementation(useSateMock);
render(<Dropdown placeholder="placeholder name" options={testOptions} />, { wrapper: BrowserRouter });
const dropdwonBtn = screen.getByTestId("dropdownBtn");
fireEvent.click(dropdwonBtn);
expect(setStateMock).toBeFalsy();
});
The code of my dropdown component;
const [showDropdown, setShowDropdown] = useState(false);
useEffect(() => {
const handler = () => setShowDropdown(false);
window.addEventListener("click", handler);
return () => {
window.removeEventListener("click", handler);
};
}, [""]);
const handleDropdownClick = (e) => {
e.stopPropagation();
setShowDropdown(!showDropdown);
};
the error I am getting