我正在尝试为这个自定义钩子编写一个测试套装。
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;
};
非常简单的返回真或假。
正如我所见,我应该使用@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 这没有意义它应该是真还是假。
PS:代码在项目上按预期工作。