diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index 0aa5dcc..79f386f 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -25,5 +25,6 @@ Title = This is my Ms Bear! Upvotes = 17 Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d Query.Subreddit = rabbits -Query.Sort By = hot" +Query.Sort By = hot +Query.Limit = 100" `; diff --git a/tests/helpers/outputHelper.test.ts b/tests/helpers/outputHelper.test.ts index 8c9893d..a0f1959 100644 --- a/tests/helpers/outputHelper.test.ts +++ b/tests/helpers/outputHelper.test.ts @@ -10,6 +10,7 @@ describe("GenerateOutput", () => { Query: { subreddit: "rabbits", sortBy: "hot", + limit: 100, }, Result: { Archived: false, @@ -40,6 +41,7 @@ describe("GenerateOutput", () => { Query: { subreddit: "rabbits", sortBy: "hot", + limit: 100, }, Result: { Archived: false, @@ -72,6 +74,7 @@ describe("GenerateOutput", () => { Query: { subreddit: "rabbits", sortBy: "hot", + limit: 100, }, Result: { Archived: false, diff --git a/tests/index.test.ts b/tests/index.test.ts index 874f129..f70d411 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -7,6 +7,10 @@ import fetch from "got-cjs"; jest.mock('got-cjs'); const fetchMock = jest.mocked(fetch); +beforeEach(() => { + fetchMock.mockReset(); +}); + describe('randomBunny', () => { test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', async() => { fetchMock.mockResolvedValue({ @@ -231,4 +235,106 @@ describe('randomBunny', () => { expect(result.Error?.Code).toBe(ErrorCode.NoImageResultsFound); expect(result.Error?.Message).toBe(ErrorMessages.NoImageResultsFound); }); + + test("GIVEN limit is supplied, EXPECT limit sent to the API", 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', 50); + + expect(result.IsSuccess).toBeTruthy(); + expect(result.Result).toBeDefined(); + expect(result.Error).toBeUndefined(); + + expect(fetchMock).toHaveBeenCalledWith('https://reddit.com/r/rabbits/new.json?limit=50'); + }); + + test("GIVEN limit is less than 1, EXPECT error to be returned", 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', 0); + + expect(result.IsSuccess).toBeFalsy(); + expect(result.Result).toBeUndefined(); + expect(result.Error).toBeDefined(); + + expect(result.Error!.Code).toBe(ErrorCode.LimitOutOfRange); + expect(result.Error!.Message).toBe(ErrorMessages.LimitOutOfRange); + + expect(fetchMock).not.toHaveBeenCalled(); + }); + + test("GIVEN limit is greater than 100, EXPECT error to be returned", 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', 101); + + expect(result.IsSuccess).toBeFalsy(); + expect(result.Result).toBeUndefined(); + expect(result.Error).toBeDefined(); + + expect(result.Error!.Code).toBe(ErrorCode.LimitOutOfRange); + expect(result.Error!.Message).toBe(ErrorMessages.LimitOutOfRange); + + expect(fetchMock).not.toHaveBeenCalled(); + }); }); \ No newline at end of file