2023-09-15 15:18:57 +01:00
|
|
|
|
import { ErrorCode } from "../src/constants/ErrorCode";
|
|
|
|
|
import ErrorMessages from "../src/constants/ErrorMessages";
|
2024-06-22 10:09:55 +01:00
|
|
|
|
import ImageHelper from "../src/helpers/imageHelper";
|
2023-04-22 13:20:30 +01:00
|
|
|
|
import randomBunny from "../src/index";
|
2023-10-27 15:53:12 +01:00
|
|
|
|
import fetch from "got-cjs";
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
|
|
|
|
jest.mock('got-cjs');
|
|
|
|
|
const fetchMock = jest.mocked(fetch);
|
|
|
|
|
|
|
|
|
|
describe('randomBunny', () => {
|
|
|
|
|
test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', 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/cr8xudsnkgua1.jpg',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await randomBunny('rabbits', 'new');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeTruthy();
|
|
|
|
|
expect(result.Result).toBeDefined();
|
2023-09-15 15:18:57 +01:00
|
|
|
|
expect(result.Error).toBeUndefined();
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
2024-06-22 10:09:55 +01:00
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GIVEN sortBy is NOT supplied, expect it to default to hot', 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/cr8xudsnkgua1.jpg',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await randomBunny('rabbits');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeTruthy();
|
|
|
|
|
expect(result.Result).toBeDefined();
|
2023-09-15 15:18:57 +01:00
|
|
|
|
expect(result.Error).toBeUndefined();
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
2024-06-22 10:09:55 +01:00
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GIVEN the fetch fails, EXPECT failure result', async () => {
|
2023-10-20 17:31:38 +01:00
|
|
|
|
fetchMock.mockRejectedValue('Test Reason')
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
|
|
|
|
const result = await randomBunny('rabbits', 'new');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeFalsy();
|
|
|
|
|
expect(result.Result).toBeUndefined();
|
2023-09-15 15:18:57 +01:00
|
|
|
|
expect(result.Error).toBeDefined();
|
|
|
|
|
|
|
|
|
|
expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit);
|
|
|
|
|
expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit);
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
2024-06-22 10:09:55 +01:00
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GIVEN the result is NOT valid JSON, EXPECT failure result', async () => {
|
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
|
body: JSON.stringify(null),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await randomBunny('rabbits', 'new');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeFalsy();
|
|
|
|
|
expect(result.Result).toBeUndefined();
|
2023-09-15 15:18:57 +01:00
|
|
|
|
expect(result.Error).toBeDefined();
|
|
|
|
|
|
|
|
|
|
expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON);
|
|
|
|
|
expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON);
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
2024-06-22 10:09:55 +01:00
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GIVEN randomSelect does NOT find a response, EXPECT failure result', async () => {
|
|
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
data: {
|
|
|
|
|
children: [],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await randomBunny('rabbits', 'new');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeFalsy();
|
|
|
|
|
expect(result.Result).toBeUndefined();
|
2023-09-15 15:18:57 +01:00
|
|
|
|
expect(result.Error).toBeDefined();
|
|
|
|
|
|
|
|
|
|
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
|
|
|
|
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
2024-01-12 17:48:37 +00:00
|
|
|
|
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GIVEN randomSelect does NOT find a valid response, EXPECT failure result', 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/cr8xudsnkgua1.webp',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await randomBunny('rabbits', 'new');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeFalsy();
|
|
|
|
|
expect(result.Result).toBeUndefined();
|
2023-09-15 15:18:57 +01:00
|
|
|
|
expect(result.Error).toBeDefined();
|
|
|
|
|
|
|
|
|
|
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
|
|
|
|
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
2023-04-22 13:20:30 +01:00
|
|
|
|
|
2024-06-22 10:09:55 +01:00
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
2024-04-07 11:03:20 +01:00
|
|
|
|
|
2024-04-12 17:54:12 +01:00
|
|
|
|
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")
|
2024-04-07 11:03:20 +01:00
|
|
|
|
|
2024-04-12 17:54:12 +01:00
|
|
|
|
const result = await randomBunny('rabbits', 'new');
|
|
|
|
|
|
|
|
|
|
expect(result.IsSuccess).toBeTruthy();
|
|
|
|
|
expect(result.Result).toBeDefined();
|
2024-04-07 11:03:20 +01:00
|
|
|
|
|
2024-06-22 10:09:55 +01:00
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
2024-04-12 17:54:12 +01:00
|
|
|
|
|
|
|
|
|
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledTimes(1);
|
2024-04-12 18:13:04 +01:00
|
|
|
|
expect(ImageHelper.FetchImageFromRedditGallery).toHaveBeenCalledWith("https://i.redd.it/gallery/cr8xudsnkgua1");
|
2024-04-12 17:54:12 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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);
|
2024-04-12 18:13:04 +01:00
|
|
|
|
expect(result.Error).toBeDefined();
|
|
|
|
|
expect(result.Error?.Code).toBe(ErrorCode.NoImageResultsFound);
|
|
|
|
|
expect(result.Error?.Message).toBe(ErrorMessages.NoImageResultsFound);
|
2023-04-22 13:20:30 +01:00
|
|
|
|
});
|
|
|
|
|
});
|