I am trying to write a test suit for this custom hook.
export const useInitialMount = () => {
const isFirstRender = useRef(true);
// in the very first render the ref is true, but we immediately change it to false.
// so the every renders after will be false.
if (isFirstRender.current) {
isFirstRender.current = false;
return true;
}
return false;
};
Very simple returns true
or false
.
As I saw, I should use @testing-library/react-hooks
and here is my try:
test("should return true in the very first render and false in the next renders", () => {
const { result } = renderHook(() => {
useInitialMount();
});
expect(result.current).toBe(true);
});
but I got undefined
which doesn't make sense it should be either true
or false
.
PS: the code works as expected on the project.