@applicaster/zapp-react-native-utils
Version: 
Applicaster Zapp React Native utilities package
73 lines (60 loc) • 1.88 kB
JavaScript
import { ImageCache } from "../index";
const imageUrl = "http://lorempixel/400/200";
describe("ImageCache", () => {
  beforeEach(() => {
    ImageCache.reset();
    ImageCache.cache(imageUrl);
  });
  describe("cacheImage", () => {
    it("returns the existing image if it is already set", () => {
      const image = ImageCache.cache(imageUrl);
      expect(image).not.toBe(null);
      expect(image.url).toEqual(imageUrl);
    });
    it("it sets the cache and returns the image if it is not set", () => {
      ImageCache.reset();
      const image = ImageCache.cache(imageUrl);
      expect(image).not.toBe(null);
      expect(image.url).toEqual(imageUrl);
    });
  });
  describe("get", () => {
    it("returns the Image object of the given URL", () => {
      const image = ImageCache.get(imageUrl);
      expect(image).not.toBe(null);
      expect(image.url).toEqual(imageUrl);
    });
    it("returns null if the image is not cached", () => {
      expect(ImageCache.get("not_cached_url")).toBe(null);
    });
  });
  describe("removeImage", () => {
    it("removes the image from the cache", () => {
      ImageCache.remove(imageUrl);
      expect(ImageCache.get(imageUrl)).toBe(null);
    });
  });
  describe("reset", () => {
    it("clears the entire image cache", () => {
      ImageCache.reset();
      expect(ImageCache.get(imageUrl)).toBe(null);
    });
    describe("if not on test env", () => {
      let env;
      beforeEach(() => {
        env = process.env.NODE_ENV;
        process.env.NODE_ENV = "not_test";
      });
      afterEach(() => {
        process.env.NODE_ENV = env;
      });
      it("does nothing", () => {
        ImageCache.cache(imageUrl);
        ImageCache.reset();
        const image = ImageCache.get(imageUrl);
        expect(image).not.toBe(null);
        expect(image.url).toEqual(imageUrl);
      });
    });
  });
});