All files / src/hooks useFetchImage.ts

37.5% Statements 6/16
25% Branches 1/4
42.85% Functions 3/7
41.66% Lines 5/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29      62x 398x 15x                     398x   15x                    
import { useQuery } from "react-query";
import { isTestEnvironment } from "@constants/constants";
 
const useFetchImage = (baseImageUrl: string) => {
  const fetchImage = async (url: string): Promise<string> => {
    Eif (isTestEnvironment) return url;
    if (!url) return "";
 
    return new Promise((resolve, reject) => {
      const img = new Image();
      img.onload = () => resolve(url);
      img.onerror = () => reject(new Error("Failed to load image"));
      img.src = url;
    });
  };
 
  return useQuery(
    ["media-library-image", baseImageUrl],
    () => fetchImage(baseImageUrl),
    {
      enabled: !!baseImageUrl,
      retry: 8,
      retryDelay: () => 3000,
    },
  );
};
 
export default useFetchImage;