Add string literal types to function
This commit is contained in:
parent
a20bdacbe5
commit
b1c7de7e8c
3 changed files with 2 additions and 42 deletions
|
@ -1,6 +1,6 @@
|
|||
export default interface ICliOptions {
|
||||
subreddit: string,
|
||||
json?: boolean,
|
||||
sort: string,
|
||||
sort: "new" | "hot" | "top",
|
||||
queryMetadata?: boolean,
|
||||
}
|
10
src/index.ts
10
src/index.ts
|
@ -7,15 +7,7 @@ import { ErrorCode } from "./constants/ErrorCode";
|
|||
import ErrorMessages from "./constants/ErrorMessages";
|
||||
import ImageHelper from "./imageHelper";
|
||||
|
||||
const sortable = [
|
||||
'new',
|
||||
'hot',
|
||||
'top'
|
||||
];
|
||||
|
||||
export default async function randomBunny(subreddit: string, sortBy: string = 'hot'): Promise<IReturnResult> {
|
||||
if (!sortable.includes(sortBy)) sortBy = 'hot';
|
||||
|
||||
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> {
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)
|
||||
.then((res) => {
|
||||
return res;
|
||||
|
|
|
@ -72,38 +72,6 @@ describe('randomBunny', () => {
|
|||
expect(fetchMock).toHaveBeenCalledWith('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).toHaveBeenCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
||||
});
|
||||
|
||||
test('GIVEN the fetch fails, EXPECT failure result', async () => {
|
||||
fetchMock.mockRejectedValue('Test Reason')
|
||||
|
||||
|
|
Loading…
Reference in a new issue