From 866a9e0cbc9157cd7d57684edf51ae10a859449e Mon Sep 17 00:00:00 2001 From: Kevin Kee Date: Sat, 6 Mar 2021 20:42:28 +0900 Subject: [PATCH] random: Add tests resolving #4 --- test/random.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/random.test.ts diff --git a/test/random.test.ts b/test/random.test.ts new file mode 100644 index 0000000..4d1f6af --- /dev/null +++ b/test/random.test.ts @@ -0,0 +1,27 @@ +import * as request from '../source/request'; +import wiki from "../source/index"; +import { summaryJson } from './samples'; +const requestMock = jest.spyOn(request, "makeRestRequest"); + +afterAll(() => { + requestMock.mockRestore(); +}) + +test('Throws error if response is error', async () => { + requestMock.mockImplementation(async () => { throw new Error("This is an error") }); + const err = async () => { await wiki.random("some_arg") }; + expect(err).rejects.toThrowError(Error); +}); + +test('Returns summary of random article', async () => { + requestMock.mockImplementation(async () => { return summaryJson }); + const result = await wiki.random("summary"); + expect(result).toStrictEqual(summaryJson); +}); + +test('Request includes arg provided to method', async () => { + requestMock.mockImplementation(async () => { return summaryJson }); + const result = await wiki.random("summary"); + expect(result).toStrictEqual(summaryJson); + expect(requestMock).toBeCalledWith(`page/random/summary`); +});