Update tests

This commit is contained in:
Ethan Lane 2023-09-13 11:09:38 +01:00
parent a56e448b88
commit 8b4efab7ce
2 changed files with 27 additions and 6 deletions

View file

@ -1,10 +1,10 @@
import IReturnResult from "./contracts/IReturnResult.js"; import IReturnResult from "./contracts/IReturnResult";
import IRedditResult from "./contracts/IRedditResult.js"; import IRedditResult from "./contracts/IRedditResult";
import fetch from "got-cjs"; import fetch from "got-cjs";
import { List } from 'linqts'; import { List } from 'linqts';
import IFetchResult from "./contracts/IFetchResult.js"; import IFetchResult from "./contracts/IFetchResult";
import { ErrorCode } from "./constants/ErrorCode.js"; import { ErrorCode } from "./constants/ErrorCode";
import ErrorMessages from "./constants/ErrorMessages.js"; import ErrorMessages from "./constants/ErrorMessages";
const sortable = [ const sortable = [
'new', 'new',
@ -34,7 +34,7 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
IsSuccess: false, IsSuccess: false,
Error: { Error: {
Code: ErrorCode.UnableToParseJSON, Code: ErrorCode.UnableToParseJSON,
Message: ErrorMessages.FailedToFetchReddit, Message: ErrorMessages.UnableToParseJSON,
}, },
} }
} }

View file

@ -1,3 +1,5 @@
import { ErrorCode } from "../src/constants/ErrorCode";
import ErrorMessages from "../src/constants/ErrorMessages";
import randomBunny from "../src/index"; import randomBunny from "../src/index";
import fetch from "got-cjs"; import fetch from "got-cjs";
@ -32,6 +34,7 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeTruthy(); expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined(); expect(result.Result).toBeDefined();
expect(result.Error).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -63,6 +66,7 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeTruthy(); expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined(); expect(result.Result).toBeDefined();
expect(result.Error).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
}); });
@ -94,6 +98,7 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeTruthy(); expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined(); expect(result.Result).toBeDefined();
expect(result.Error).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
}); });
@ -105,6 +110,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -118,6 +127,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -135,6 +148,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -166,6 +183,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });