2024-06-22 10:09:55 +01:00
|
|
|
import ImageHelper from "../../src/helpers/imageHelper";
|
2024-04-12 17:54:12 +01:00
|
|
|
import fetch from "got-cjs";
|
|
|
|
|
|
|
|
jest.mock('got-cjs');
|
|
|
|
const fetchMock = jest.mocked(fetch);
|
|
|
|
|
2024-04-07 11:03:20 +01:00
|
|
|
describe("FetchImageFromRedditGallery", () => {
|
2024-04-12 17:54:12 +01:00
|
|
|
test("EXPECT image url to be returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
|
|
|
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).toBe("https://preview.redd.it/image.png");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("GIVEN fetch is unable to return data, EXPECT undefined returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue(null);
|
|
|
|
|
|
|
|
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
|
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("GIVEN fetch is an error, EXPECT undefined returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
|
|
|
errored: "Error",
|
|
|
|
statusCode: 200,
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
|
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("GIVEN fetch is not status code of 200, EXPECT undefined returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
body: "<html><body><img src='https://preview.redd.it/image.png' /></body></html>",
|
|
|
|
errored: undefined,
|
|
|
|
statusCode: 500,
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
|
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("GIVEN image tag is not found, EXPECT undefined returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
body: "<html><body></body></html>",
|
|
|
|
errored: undefined,
|
|
|
|
statusCode: 200,
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
|
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("GIVEN image source attribute is not found, EXPECT undefined returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
body: "<html><body><img /></body></html>",
|
|
|
|
errored: undefined,
|
|
|
|
statusCode: 200,
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
|
|
|
|
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
2024-04-07 11:03:20 +01:00
|
|
|
|
2024-04-12 17:54:12 +01:00
|
|
|
test("GIVEN image source attribute is not found that is a preview.redd.it url, EXPECT undefined returned", async () => {
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
body: "<html><body><img src='main.png' /></body></html>",
|
|
|
|
errored: undefined,
|
|
|
|
statusCode: 200,
|
|
|
|
});
|
2024-04-07 11:03:20 +01:00
|
|
|
|
2024-04-12 17:54:12 +01:00
|
|
|
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
2024-04-07 11:03:20 +01:00
|
|
|
|
2024-04-12 17:54:12 +01:00
|
|
|
expect(result).toBeUndefined();
|
|
|
|
});
|
2024-04-07 11:03:20 +01:00
|
|
|
});
|