Merge branch 'master' into develop
This commit is contained in:
commit
5d0af43b6f
6 changed files with 253 additions and 3 deletions
90
tests/imageHelper.test.ts
Normal file
90
tests/imageHelper.test.ts
Normal file
|
@ -0,0 +1,90 @@
|
|||
import ImageHelper from "../src/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: "<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("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,
|
||||
});
|
||||
|
||||
const result = await ImageHelper.FetchImageFromRedditGallery("https://redd.it/gallery/image");
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
|
@ -1,5 +1,6 @@
|
|||
import { ErrorCode } from "../src/constants/ErrorCode";
|
||||
import ErrorMessages from "../src/constants/ErrorMessages";
|
||||
import ImageHelper from "../src/imageHelper";
|
||||
import randomBunny from "../src/index";
|
||||
import fetch from "got-cjs";
|
||||
|
||||
|
@ -189,5 +190,74 @@ describe('randomBunny', () => {
|
|||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
});
|
||||
|
||||
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")
|
||||
|
||||
const result = await randomBunny('rabbits', 'new');
|
||||
|
||||
expect(result.IsSuccess).toBeTruthy();
|
||||
expect(result.Result).toBeDefined();
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue