From b1c7de7e8ca4cf400cb050934948be9d3387aaad Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 19 Jun 2024 13:45:07 +0100 Subject: [PATCH] Add string literal types to function --- src/contracts/ICliOptions.ts | 2 +- src/index.ts | 10 +--------- tests/index.test.ts | 32 -------------------------------- 3 files changed, 2 insertions(+), 42 deletions(-) diff --git a/src/contracts/ICliOptions.ts b/src/contracts/ICliOptions.ts index cc3af03..783f6af 100644 --- a/src/contracts/ICliOptions.ts +++ b/src/contracts/ICliOptions.ts @@ -1,6 +1,6 @@ export default interface ICliOptions { subreddit: string, json?: boolean, - sort: string, + sort: "new" | "hot" | "top", queryMetadata?: boolean, } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 75ad628..694a274 100644 --- a/src/index.ts +++ b/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 { - if (!sortable.includes(sortBy)) sortBy = 'hot'; - +export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise { const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`) .then((res) => { return res; diff --git a/tests/index.test.ts b/tests/index.test.ts index e1caf92..f7f7193 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -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')