我正在尝试为这个自定义钩子编写一个测试服。
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;
};
非常简单的返回true或false。
正如我所看到的,我应该使用@testing-library/react-hooks
,这是我的尝试:
test("should return true in the very first render and false in the next renders", () => {
const { result } = renderHook(() => {
useInitialMount();
});
expect(result.current).toBe(true);
});
但我得到了undefined这没有意义,它应该是trueor false。
PS:代码在项目中按预期工作。