0

how to write unit tests for useUpdateTodo hook without mock useGetUserId

// Get the user
const useGetUserId = ()=>{
  const { data: user } = useQuery({
    queryKey: ['user', email],
    queryFn: getUserByEmail,
  })
  return user?.id
}



// Then update 
const useUpdateTodo = ()=>{
   const userId = useGetUserId()
   const mutation = useMutation({
    mutationFn: (newTodo) => {
      return axios.post('/todos', newTodo, userId)
    },
  })
  return mutation
}

I use react-hooks-testing-library,According to the normal usage of renderHook, when invoking the mutation through result.current, it does not wait for the result returned by useGetUserId.

2
  • Did you read the testing doc? Commented Mar 18 at 2:13
  • Yes, I have gone through the documentation. However, the scenario described therein is overly simplistic. In my situation, there exist dependencies among the requests. Commented Mar 18 at 3:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.