import React from "react"; import Image from "./image"; import { render, act } from "@testing-library/react-native"; import { create } from "react-test-renderer"; import { ThemeProvider } from "../../../theme/src/theme-context"; import CacheManager from "./cache-manager"; import Icon from "../../atoms/icon/icon"; jest.useFakeTimers(); describe("Molecules/Image", () => { it("passes the snapshot test for basic setup", async () => { const tree = await render( ).toJSON(); expect(tree).toMatchSnapshot(); }); it("loads uses the local url for local images", async () => { let comp; act(() => { comp = create( ); }); const tree = (comp as any).toJSON(); expect(tree).toMatchSnapshot(); }); it("passes the snapshot test for the various progressive loading indicators", async () => { const tree = await render( ).toJSON(); expect(tree).toMatchSnapshot(); }); it("passes the snapshot test for the fallback methods if there is an error while fetching the image", async () => { const tree = await render( } source={{ uri: "https://4kwallpapers.com/imas/wallpapers/macos-big-sur-apple-layers-fluidic-colorful-wwdc-stock-2560x1440-1455.jpg", }} previewSource={{ uri: "https://www.wpbeginner.com/wp-content/uploads/2020/03/ultimate-small-business-resource-coronavirus.png", }} /> ).toJSON(); expect(tree).toMatchSnapshot(); }); it("loads uses the target url for remote images without caching", async () => { let comp; act(() => { comp = create( ); }); const tree = (comp as any).toJSON(); expect(tree).toMatchSnapshot(); }); it("triggers onError to be triggered and final image to be hidden in when an error occurs while fetching a remote image", () => { const onError = jest.fn(); const main = render( ); const tree = main.toJSON(); expect(tree).toMatchSnapshot(); expect(() => main.getByTestId("mainImage")).toThrowError(); // TODO Mock Filesystem download to invoke the error correctly // expect(onError).toHaveBeenCalledTimes(1); }); it("makes sure that the CacheManager works as expected", async () => { await act(async () => { await CacheManager.clearCache(); const finalPath = await CacheManager.get( "https://wallpaperaccess.com/full/1713248.jpg", {} ).getPath(); expect(finalPath?.includes(".jpg")).toBeTruthy(); }); }); it("loads uses the filesystem url for remote images with caching", () => { // TODO: Add cache tests const tree = render( ).toJSON(); expect(tree).toMatchSnapshot(); }); });