Skip to content

Commit

Permalink
added tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dopecodez committed Aug 20, 2021
1 parent 22f5530 commit cdbb132
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
coverage
test.js
test.js
.DS_Store
14 changes: 10 additions & 4 deletions source/page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { categoriesError, contentError, coordinatesError, htmlError, imageError, wikiError, pdfError,
infoboxError, introError, linksError, mediaError, preloadError, relatedError, summaryError } from './errors';
import request, { makeRestRequest } from './request';
import request, { makeRestRequest, returnRestUrl } from './request';
import { coordinatesResult, imageResult, langLinksResult, notFound, pageResult, relatedResult, wikiMediaResult, wikiSummary } from './resultTypes';
import { setPageId, setPageIdOrTitleParam } from './utils';
import { listOptions, pageOptions, pdfOptions } from './optionTypes';
Expand Down Expand Up @@ -394,7 +394,13 @@ export class Page {
}
}

public pdf = async (pdfOptions: pdfOptions): Promise<any> => {
/**
* Returns pdf of a given page
*
* @param pdfOptions - {@link pdfOptions | pdfOptions }
* @returns Returns path string
*/
public pdf = async (pdfOptions?: pdfOptions): Promise<string> => {
try {
const result = await pdf(this.title, pdfOptions)

Expand Down Expand Up @@ -840,13 +846,13 @@ export const mobileHtml = async (title: string, redirect = true): Promise<notFou
* @param pdfOptions - {@link pdfOptions | pdfOptions }
* @returns Returns pdf format
*/
export const pdf = async (title: string, pdfOptions?: pdfOptions): Promise<any> => {
export const pdf = async (title: string, pdfOptions?: pdfOptions): Promise<string> => {
try {
let path = `page/pdf/${title}`;
pdfOptions?.format ? path += `/${pdfOptions.format}` : null;
pdfOptions?.type ? path += `/${pdfOptions.type}` : null;

const result = await makeRestRequest(path);
const result = returnRestUrl(path);
return result;
} catch (error) {
throw new pdfError(error);
Expand Down
6 changes: 6 additions & 0 deletions source/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export async function makeRestRequest(path: string, redirect = true): Promise<an
}
}

//return rest uri
export function returnRestUrl(path: string): string {
const url = encodeURI(REST_API_URL + path);
return url;
}

//change language of both urls
export function setAPIUrl(prefix: string) : string {
API_URL = 'http://' + prefix.toLowerCase() + '.wikipedia.org/w/api.php?';
Expand Down
68 changes: 68 additions & 0 deletions test/pdf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as request from '../source/request';
import wiki from "../source/index";
import { htmlString, notFoundJson } from './samples';
import { pdfError } from '../source/errors';
import Page, { pdf } from '../source/page';
import { pageJson } from './samples';
import * as utils from '../source/utils'
const requestMock = jest.spyOn(request, "returnRestUrl");
const setTitleMock = jest.spyOn(utils, "setTitleForPage");

const pdfResult = "link/pdf"

afterAll(() => {
requestMock.mockRestore();
setTitleMock.mockRestore();
});

test('returns link of pdf on page', async () => {
requestMock.mockImplementation(() => { return pdfResult });
const page = new Page(pageJson);
const result = await page.pdf();
expect(requestMock).toHaveBeenCalledTimes(1);
expect(result).toStrictEqual(pdfResult);
});

test('throws pdf error on page', async () => {
requestMock.mockImplementation(() => { throw Error("this is an error") });
const page = new Page(pageJson);
const t = async () => {
await page.pdf();
};
expect(t).rejects.toThrowError(pdfError);
});

test('Throws pdf error', async () => {
requestMock.mockImplementation(() => { throw new Error("This is an error") });
const t = async () => {
await pdf("Test")
};
expect(t).rejects.toThrowError(pdfError);
});

test('Returns link of pdf', async () => {
requestMock.mockImplementation(() => { return pdfResult });
const result = await pdf("Test");
expect(result).toStrictEqual(pdfResult);
});

test('Returns link of pdf directly', async () => {
requestMock.mockImplementation(() => { return pdfResult });
const result = await wiki.pdf("ACID");
expect(result).toStrictEqual(pdfResult);
});

test('throws pdf error directly', async () => {
requestMock.mockImplementation(() => { throw new Error("This is an error") });
const t = async () => {
await wiki.pdf("ACID");
};
expect(t).rejects.toThrowError(pdfError);
});

test('Returns link of pdf directly', async () => {
let pdfResultWithAllFormats = pdfResult + '/legal/mobile';
requestMock.mockImplementation(() => { return pdfResultWithAllFormats });
const result = await wiki.pdf("ACID", { autoSuggest: true, type: 'mobile', format: 'legal' });
expect(result).toStrictEqual(pdfResultWithAllFormats);
});
11 changes: 8 additions & 3 deletions test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import request, {makeRestRequest, setAPIUrl} from '../source/request';
import request, {makeRestRequest, setAPIUrl, returnRestUrl} from '../source/request';
import * as fetch from 'node-fetch';
import { Response } from 'node-fetch';
import { wikiError } from '../source/errors';
Expand Down Expand Up @@ -58,7 +58,7 @@ test('makeRestRequest method calls and returns with expected params', async () =
test('makeRestRequest throws wiki error if error is raised', async () => {
fetchMock.mockImplementation(async () => { throw new Error("Error") });
const t = async () => {
await makeRestRequest("", true)
await makeRestRequest("")
};
expect(t).rejects.toThrowError(wikiError);
});
Expand All @@ -72,7 +72,12 @@ test('makeRestRequest throws wiki error if error is raised', async () => {
// );
// });

test('Return rest url', () => {
const result = returnRestUrl("path/pdf");
expect(result).toStrictEqual("http://en.wikipedia.org/api/rest_v1/path/pdf");
});

test('Set language returns api url with language set', () => {
const result = setAPIUrl("mal");
expect(result).toStrictEqual("http://mal.wikipedia.org/w/api.php?");
});
});

0 comments on commit cdbb132

Please sign in to comment.