diff --git a/js/models/RateService.js b/js/models/RateService.js index aec56366d..de0072375 100644 --- a/js/models/RateService.js +++ b/js/models/RateService.js @@ -106,7 +106,10 @@ RateService.prototype.getHistoricRates = function(code, dates, cb) { }, function(err, res, body) { if (err || res.statusCode != 200 || !body) return cb(err || res); if (!_.isArray(body)) { - body = [{ ts: dates[0], rate: body.rate }]; + body = [{ + ts: dates[0], + rate: body.rate + }]; } return cb(null, body); }); diff --git a/test/RateService.js b/test/RateService.js index db2445976..278494bb9 100644 --- a/test/RateService.js +++ b/test/RateService.js @@ -1,5 +1,6 @@ 'use strict'; +var moment = require('moment'); var RateService = copay.RateService; describe('RateService model', function() { @@ -10,10 +11,10 @@ describe('RateService model', function() { describe('Fetching currencies', function() { var clock; - before(function () { + before(function() { clock = sinon.useFakeTimers(); }); - after(function () { + after(function() { clock.restore(); }); it('should retry fetching currencies on error', function() { @@ -174,6 +175,53 @@ describe('RateService model', function() { }); }); + describe('#getHistoricRate', function() { + it('should return historic rate', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 200 + }, { + ts: yesterday, + rate: 100 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + var params = [{ + code: 'USD', + date: yesterday, + expected: '100.00' + }]; + + _.each(params, function(p) { + rs.getHistoricRate('USD', yesterday, function(err, rate) { + rate.toFixed(2).should.equal(p.expected); + }); + }); + }); + it.only('should return error', function() { + var yesterday = moment().subtract(1, 'day'); + var reqStub = sinon.stub(); + reqStub.get = sinon.stub().yields(null, { + statusCode: 500 + }); + + var rs = new RateService({ + request: reqStub + }); + rs.isAvailable = sinon.stub().returns(true); + + rs.getHistoricRate('USD', yesterday, function(err, rate) { + err.statusCode.should.equal(500); + }); + + }); + }); + describe('#fromFiat', function() { it('should throw error when unavailable', function() { var rs = new RateService();