Skip to content

Commit

Permalink
fixup! Convert optCurrency test to esnext
Browse files Browse the repository at this point in the history
  • Loading branch information
nipher committed Jun 3, 2017
1 parent 41adc67 commit aab6398
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/options/optCurrency.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function formatRates(rates) {

async function optCurrency({ base = 'USD', symbols } = {}) {
const apiURL = 'http://api.fixer.io/latest';
const params = { params: { base } };
const config = { params: { base } };

if (symbols) {
params.params.symbols = symbols;
config.params.symbols = symbols;
}

try {
const response = await axios.get(apiURL, params);
const response = await axios.get(apiURL, config);
const { data, status: statusCode } = response;

if (statusCode !== 200 || typeof data !== 'object') {
Expand Down
55 changes: 19 additions & 36 deletions src/options/optCurrency.test.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,83 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-underscore-dangle */

import nock from 'nock';
import moxios from 'moxios';
import chalk from 'chalk';
import chai, { expect } from 'chai';
import rewire from 'rewire';
import sinonChai from 'sinon-chai';
import sinon from 'sinon';

import { response } from '../../stubs/currency';

const optCurrency = rewire('./optCurrency');
chai.use(sinonChai);

const stubs = require('../../stubs/currency');

const wait = t => new Promise(r => setTimeout(() => r(), t));

let responseMock;
let commanderMock;
let consoleStub;

describe('optCurrency', () => {
beforeEach(() => {
responseMock = stubs.response;
moxios.install();
consoleStub = sinon.stub(console, 'log');
});

afterEach(() => {
moxios.uninstall();
console.log.restore();
});

it('should console log USD base currency against all currencies', async () => {
nock('http://api.fixer.io')
.get('/latest')
.query({ base: 'USD' })
.reply(200, responseMock);
moxios.stubRequest('http://api.fixer.io/latest?base=USD', { status: 200, response });

commanderMock = {};
optCurrency(commanderMock);
const responseMockParsed = JSON.parse(responseMock);

await wait(300);
await new Promise(resolve => moxios.wait(resolve));

expect(consoleStub).to.have.been.calledWith(`\n${chalk.yellow('Base currency')} ${optCurrency.__get__('getCountryIcon')(responseMockParsed.base)} ${chalk.cyan(responseMockParsed.base)}`);
expect(consoleStub).to.have.been.calledWith(`\nCurrency Rates\n\n${optCurrency.__get__('formatRates')(responseMockParsed.rates)}`);
expect(consoleStub).to.have.been.calledWith(`\n${chalk.yellow('Base currency')} ${optCurrency.__get__('getCountryIcon')(response.base)} ${chalk.cyan(response.base)}`);
expect(consoleStub).to.have.been.calledWith(`\nCurrency Rates\n\n${optCurrency.__get__('formatRates')(response.rates)}`);
});

it('should console log USD base currency against currencies defined in symbols parameter', async () => {
const responseMockSymbols = JSON.stringify({
const response2 = {
base: 'USD',
date: '2017-01-04',
rates: {
BRL: 3.2369,
EUR: 0.95813,
},
});

const responseMockParsed = JSON.parse(responseMockSymbols);
};

nock('http://api.fixer.io')
.get('/latest')
.query({ base: 'USD', symbols: 'BRL,EUR' })
.reply(200, responseMockSymbols);
moxios.stubRequest('http://api.fixer.io/latest?base=USD&symbols=BRL,EUR', { status: 200, response: response2 });

commanderMock = { symbols: 'BRL,EUR' };
optCurrency(commanderMock);

await wait(300);
await new Promise(resolve => moxios.wait(resolve));

expect(consoleStub).to.have.been.calledWith(`\n${chalk.yellow('Base currency')} ${optCurrency.__get__('getCountryIcon')(responseMockParsed.base)} ${chalk.cyan(responseMockParsed.base)}`);
expect(consoleStub).to.have.been.calledWith(`\nCurrency Rates\n\n${optCurrency.__get__('formatRates')(responseMockParsed.rates)}`);
expect(consoleStub).to.have.been.calledWith(`\n${chalk.yellow('Base currency')} ${optCurrency.__get__('getCountryIcon')(response2.base)} ${chalk.cyan(response2.base)}`);
expect(consoleStub).to.have.been.calledWith(`\nCurrency Rates\n\n${optCurrency.__get__('formatRates')(response2.rates)}`);
});

it('should message user when api reply with error', async () => {
nock('http://api.fixer.io')
.get('/latest')
.query({ base: 'USD' })
.replyWithError('Error');
moxios.stubRequest('http://api.fixer.io/latest?base=USD', { status: 500 });

commanderMock = {};
optCurrency(commanderMock);

await wait(300);
await new Promise(resolve => moxios.wait(resolve));

expect(consoleStub).to.have.been.calledWith(`${chalk.red('Something went wrong in the API. Try in a few minutes')}`);
});

it('should message user when request is made with unusual get parameters', async () => {
nock('http://api.fixer.io')
.get('/latest')
.query({ base: 'UWERSD' })
.reply(200, 'invalid_json');
moxios.stubRequest('http://api.fixer.io/latest?base=UWERSD', { status: 200, response: 'invalid_json' });

commanderMock = { base: 'UWERSD' };
optCurrency(commanderMock);

await wait(300);
await new Promise(resolve => moxios.wait(resolve));

expect(consoleStub).to.have.been.calledWith(`${chalk.red('It was not possible to retrieve what you want')}`);
});
Expand Down
6 changes: 3 additions & 3 deletions stubs/currency.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.response = JSON.stringify({
export const response = {
base: 'USD',
date: '2017-01-04',
rates: {
Expand Down Expand Up @@ -33,5 +33,5 @@ exports.response = JSON.stringify({
TRY: 3.5822,
ZAR: 13.645,
EUR: 0.95813,
},
});
}
};

0 comments on commit aab6398

Please sign in to comment.