test: eBird.js first test passed using jest promise handling

This commit is contained in:
Samuel Ortion 2022-08-26 11:58:41 +02:00
parent da431878dd
commit 0e177a9d01
2 changed files with 18 additions and 30 deletions

View File

@ -1,31 +1,28 @@
const axios = require('axios'); const axios = require('axios');
const debug = require('debug')('ebird:api');
module.exports = function (EBIRD_API_KEY) { module.exports = function (EBIRD_API_KEY) {
const BASE_URL = "https://api.ebird.org/v2" const BASE_URL = "https://api.ebird.org/v2"
let key = EBIRD_API_KEY; let key = EBIRD_API_KEY;
function query(endpoint, params) { function query(endpoint, params) {
let data = undefined; debug(`querying to ${BASE_URL}/${endpoint}`);
axios.get(`${BASE_URL}/${endpoint}`, { if (params === undefined) {
params = {};
}
return axios.get(`${BASE_URL}/${endpoint}`, {
headers: { headers: {
'X-eBirdApiToken': key 'X-eBirdApiToken': key
}, },
params: { params: {
...params ...params
} }
}).then((response) => { }).then((response) => {return response.data})
console.log('GET response'); .catch((error) => {throw error})
data = response.data;
}).catch((error) => {
console.error(error);
});
return data;
} }
function get(endpoint) { function get(endpoint) {
return params => { return (params) => query(endpoint, params)
query(endpoint, params);
}
} }
const api = { const api = {
@ -124,4 +121,5 @@ module.exports = function (EBIRD_API_KEY) {
}, },
}, },
} }
return api;
}; };

View File

@ -1,20 +1,10 @@
const ebird = require('../src/index'); require('dotenv').config();
require('dotenv').config; const ebird = require('../src/index')(process.env.EBIRD_API_KEY);
test('/data/obs/${regionCode}/recent for location France', () => { test('/data/obs/${regionCode}/recent for location France', () => {
let data = ebird.data.obs.recent.in('FR')(); const data = ebird.data.obs.recent.in('FR')();
console.log(data); return data.then(data=>{
expect(data.length).toBeGreaterThan(0);
})
}); });
// test('get subregions from region code and type', () => {
// let regionType = "country";
// let regionCode = "world";
// let regions = ebird.get_regions(regionType, regionCode);
// expect(regions).toContain({ code: 'FR', name: 'France' });
// });
// test('get region code from coordinates', () => {
// let regionCode = ebird.coordinates2region(47.87842, 0.21826);
// console.log(regionCode);
// });