Skip to content

Commit

Permalink
Use Jest for tests (#72)
Browse files Browse the repository at this point in the history
* Use Jest for tests

* Fix return type for typescript

* Refactoring: use more functions

* eslint max len 120

* Add function for parsing page

* Add tests with jest

* Move test, add new test

* Add function to jest ignored files to run coverage

* Update version
  • Loading branch information
DavideViolante authored Sep 28, 2022
1 parent 39cf78b commit c2c05b7
Show file tree
Hide file tree
Showing 7 changed files with 5,548 additions and 2,267 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"commonjs": true,
"es2021": true,
"node": true,
"mocha": true
"jest": true
},
"extends": "google",
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
"max-len": [
"error", 100
"error", 120
],
"object-curly-spacing": [
"error", "always"
Expand Down
11 changes: 11 additions & 0 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ function mapResponse(array) {
}));
}

/**
* Get JSON response from Investing APIs
* @param {*} page puppeteer page
* @return {Object} JSON response from Investing
*/
async function getJsonContent(page) {
const content = await page.evaluate(() => document.querySelector('body').textContent);
return JSON.parse(content);
}

module.exports = {
mapResponse,
getJsonContent,
};
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ declare module "investing-com-api" {
period?: 'P1D' | 'P1W' | 'P1M' | 'P3M' | 'P6M' | 'P1Y' | 'P5Y' | 'MAX',
interval?: 'PT1M' | 'PT5M' | 'PT15M' | 'PT30M' | 'PT1H' | 'PT5H' | 'P1D' | 'P1W' | 'P1M',
pointscount?: 60 | 70 | 120
): { date: number, value: number }[];
): {
date: number,
value: number,
price_open: number,
price_high: number,
price_low: number,
price_close: number,
}[];
}
35 changes: 29 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
const puppeteer = require('puppeteer');
const { mapping } = require('./mapping');
const { mapResponse } = require('./functions');
const { getJsonContent, mapResponse } = require('./functions');

const validPeriod = ['P1D', 'P1W', 'P1M', 'P3M', 'P6M', 'P1Y', 'P5Y', 'MAX'];
const validInterval = ['PT1M', 'PT5M', 'PT15M', 'PT30M', 'PT1H', 'PT5H', 'P1D', 'P1W', 'P1M'];
const validPointscount = [60, 70, 120];

/**
* Check if params are valid
* @param {String} input input
* @param {String} period period
* @param {String} interval interval
* @param {Number} pointscount pointscount
*/
function checkParams(input, period, interval, pointscount) {
if (!input) {
throw Error('Parameter input is required');
}
if (!validPeriod.includes(period)) {
throw Error('Invalid period parameter. Valid values are: P1D, P1W, P1M, P3M, P6M, P1Y, P5Y, MAX');
}
if (!validInterval.includes(interval)) {
throw Error('Invalid interval parameter. Valid values are: PT1M, PT5M, PT15M, PT30M, PT1H, PT5H, P1D, P1W, P1M');
}
if (!validPointscount.includes(pointscount)) {
throw Error('Invalid pointscount parameter. Valid values are: 60, 70, 120');
}
}

/**
* Call Investing
Expand All @@ -19,9 +45,8 @@ async function callInvesting(pairId, period, interval, pointscount) {
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36');
// eslint-disable-next-line max-len
await page.goto(`https://api.investing.com/api/financialdata/${pairId}/historical/chart?period=${period}&interval=${interval}&pointscount=${pointscount}`);
const content = await page.evaluate(() => document.querySelector('body').textContent);
const jsonContent = await getJsonContent(page);
await browser.close();
const jsonContent = JSON.parse(content);
return jsonContent;
}

Expand All @@ -38,9 +63,7 @@ async function callInvesting(pairId, period, interval, pointscount) {
*/
async function investing(input, period = 'P1M', interval = 'P1D', pointscount = 120) {
try {
if (!input) {
throw Error('Parameter input is required');
}
checkParams(input, period, interval, pointscount);
const endpoint = mapping[input];
if (!endpoint) {
throw Error(`No mapping found for ${input}, check mapping.js`);
Expand Down
Loading

0 comments on commit c2c05b7

Please sign in to comment.