Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vylpes committed Apr 12, 2024
1 parent 00b5158 commit 9e25e5a
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/imageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default class ImageHelper {
public static async FetchImageFromRedditGallery(url: string): Promise<string | undefined> {
const fetched = await fetch(url);

if (!fetched) {
if (!fetched || fetched.errored || fetched.statusCode != 200) {
return undefined;
}

Expand Down
89 changes: 85 additions & 4 deletions tests/imageHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,90 @@
import ImageHelper from "../src/imageHelper";
import fetch from "got-cjs";

jest.mock('got-cjs');
const fetchMock = jest.mocked(fetch);

describe("FetchImageFromRedditGallery", () => {
test.todo("EXPECT image url to be returned");
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();
});

test.todo("GIVEN fetch is unable to return data, EXPECT undefined returned");
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,
});

test.todo("GIVEN image tag is not found, EXPECT undefined returned");
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");

test.todo("GIVEN image source attribute is not found, EXPECT undefined returned");
expect(result).toBeUndefined();
});
});
69 changes: 66 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ImageHelper from "../src/imageHelper";
import randomBunny from "../src/index";
import fetch from "got-cjs";

Expand Down Expand Up @@ -170,9 +171,71 @@ describe('randomBunny', () => {
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
});

test.todo("GIVEN data fetched is a gallery AND an image is returned from the helper, EXPECT this to be used");
test("GIVEN data fetched is a gallery AND an image is returned from the helper, EXPECT this to be used", async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles it’s Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/gallery/cr8xudsnkgua1',
},
},
],
}
}),
});

ImageHelper.FetchImageFromRedditGallery = jest.fn().mockResolvedValue("https://i.redd.it/cr8xudsnkgua1.jpg")

test.todo("GIVEN data fetched is a gallery AND an image is not returned from the helper, EXPECT error");
const result = await randomBunny('rabbits', 'new');

expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();

test.todo("GIVEN data fetched is not a gallery, EXPECT url to be used directly");
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');

expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1")
});

test("GIVEN data fetched is a gallery AND an image is not returned from the helper, EXPECT error", async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles it’s Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/gallery/cr8xudsnkgua1',
},
},
],
}
}),
});

ImageHelper.FetchImageFromRedditGallery = jest.fn().mockResolvedValue(undefined)

const result = await randomBunny('rabbits', 'new');

expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);

expect(result.IsSuccess).toBe(false);
});
});

0 comments on commit 9e25e5a

Please sign in to comment.