Skip to content

Instantly share code, notes, and snippets.

@cristofersousa
Created September 28, 2019 22:18
Show Gist options
  • Save cristofersousa/add9873c2c7997547e50f3c5f3a1eacf to your computer and use it in GitHub Desktop.
Save cristofersousa/add9873c2c7997547e50f3c5f3a1eacf to your computer and use it in GitHub Desktop.
const axios = require('axios');
const getURL = data => `https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)?@dataCotacao='${data}'&$top=100&$format=json&$select=cotacaoCompra,cotacaoVenda,dataHoraCotacao`;
const getCotacaoAPI = url => axios.get(url);
const extractCotacao = res => res.data.value[0].cotacaoVenda;
const getToday = () => {
const today = new Date();
return (today.getMonth()+1)+'-'+today.getDate()+ '-'+today.getFullYear();
}
const getCotacao = async () => {
try {
const today = getToday();
const url = getURL(today);
const res = await getCotacaoAPI(url);
// const res = await getCotacaoAPI('4-12-2019');
const cotacao = extractCotacao(res);
return cotacao;
} catch(err) {
console.log(err);
return '';
}
}
module.exports = {
getCotacaoAPI,
getCotacao,
extractCotacao
}
Arquivo de Teste
const api = require('../OlindaAPI');
const axios = require('axios');
jest.mock('axios');
describe('OlindaAPI', () => {
it('should get cotacao API', () => {
const response = {
data: {
value: [
{ cotacaoVenda: 3.90 }
],
}
};
axios.get.mockResolvedValue(response);
api.getCotacaoAPI('url').then( resp => {
expect(resp).toEqual(response);
console.log(axios.get.mock);
expect(axios.get.mock.calls[0][0]).toBe('url');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment