Ethan Lane
5ea8b72e54
All checks were successful
continuous-integration/drone/push Build is passing
# Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Add higher limit on the json result - This will fix the tests not always fetching an image from r/rabbits, as not every post in the top is a single image #125 ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/139 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
193 lines
No EOL
7.4 KiB
TypeScript
193 lines
No EOL
7.4 KiB
TypeScript
import { ErrorCode } from "../src/constants/ErrorCode";
|
||
import ErrorMessages from "../src/constants/ErrorMessages";
|
||
import randomBunny from "../src/index";
|
||
import fetch from "got-cjs";
|
||
|
||
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();
|
||
expect(result.Error).toBeUndefined();
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||
});
|
||
|
||
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();
|
||
expect(result.Error).toBeUndefined();
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
||
});
|
||
|
||
test('GIVEN sortBy is NOT valid, 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', 'invalid');
|
||
|
||
expect(result.IsSuccess).toBeTruthy();
|
||
expect(result.Result).toBeDefined();
|
||
expect(result.Error).toBeUndefined();
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
||
});
|
||
|
||
test('GIVEN the fetch fails, EXPECT failure result', async () => {
|
||
fetchMock.mockRejectedValue('Test Reason')
|
||
|
||
const result = await randomBunny('rabbits', 'new');
|
||
|
||
expect(result.IsSuccess).toBeFalsy();
|
||
expect(result.Result).toBeUndefined();
|
||
expect(result.Error).toBeDefined();
|
||
|
||
expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit);
|
||
expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit);
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||
});
|
||
|
||
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();
|
||
expect(result.Error).toBeDefined();
|
||
|
||
expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON);
|
||
expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON);
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||
});
|
||
|
||
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();
|
||
expect(result.Error).toBeDefined();
|
||
|
||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||
});
|
||
|
||
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();
|
||
expect(result.Error).toBeDefined();
|
||
|
||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||
|
||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||
});
|
||
}); |