Skip to content

Commit

Permalink
random: Add tests
Browse files Browse the repository at this point in the history
resolving #4
  • Loading branch information
friendofdog committed Mar 27, 2021
1 parent fdb2da5 commit 866a9e0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/random.test.ts
Original file line number Diff line number Diff line change
@@ -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`);
});

0 comments on commit 866a9e0

Please sign in to comment.