import ImageHelper from "../../src/helpers/imageHelper"; import fetch from "got-cjs"; jest.mock('got-cjs'); const fetchMock = jest.mocked(fetch); describe("FetchImageFromRedditGallery", () => { test("EXPECT image url to be returned", async () => { fetchMock.mockResolvedValue({ body: "
", errored: undefined, statusCode: 200, }); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(fetchMock).toHaveBeenCalledTimes(1); expect(fetchMock).toHaveBeenCalledWith("https://redd.it/gallery/image"); expect(result.length).toBe(1); expect(result[0]).toBe("https://preview.redd.it/image.png"); }); test("GIVEN fetch is unable to return data, EXPECT empty array returned", async () => { fetchMock.mockResolvedValue(null); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(result.length).toBe(0); }); test("GIVEN fetch is an error, EXPECT empty array returned", async () => { fetchMock.mockResolvedValue({ body: "", errored: "Error", statusCode: 200, }); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(result.length).toBe(0); }); test("GIVEN fetch is not status code of 200, EXPECT empty array returned", async () => { fetchMock.mockResolvedValue({ body: "", errored: undefined, statusCode: 500, }); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(result.length).toBe(0); }); test("GIVEN image tag is not found, EXPECT empty array returned", async () => { fetchMock.mockResolvedValue({ body: "", errored: undefined, statusCode: 200, }); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(result.length).toBe(0); }); test("GIVEN image source attribute is not found, EXPECT empty array returned", async () => { fetchMock.mockResolvedValue({ body: "", errored: undefined, statusCode: 200, }); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(result.length).toBe(0); }); test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT empty array returned", async () => { fetchMock.mockResolvedValue({ body: "", errored: undefined, statusCode: 200, }); const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image"); expect(result.length).toBe(0); }); });