如何在打字稿中使用 jest 模拟 `useSelector` 和 `useQuery`

时间:2021-07-24 04:29:04

标签: reactjs unit-testing react-redux jestjs

我有一个函数/钩子,其中包括两个来自“react”的 useSelect 和一个 useApiCall,它是 useQuery 函数来获取 api 响应。我如何使用 jest 来模拟 2 useSelector 和 useApiCall 函数 所以我可以测试 if 块中的逻辑来做类似 expect(store.action(recordsAction)).not.toHaveBeenCalled()

export function useApp(): {
  const dispatch = useDispatch();
  const records = useSelector((state) => state.records) || {};
  const options = useSelector((state) => state.options) || {};

  const response = useApiCall(options);

  const onApp = useCallback(async (): Promise<void> => {
      const isChange = records?.options == options
    if (isChange) {
      dispatch(recordsAction(options));
    }
  }, [dispatch, options]);
  return {onApp};
}

0 个答案:

没有答案