Test for getHistoricRate

This commit is contained in:
Matias Pando 2014-12-15 18:15:20 -03:00
parent 82a26f76ea
commit f41d795921
2 changed files with 54 additions and 3 deletions

View File

@ -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);
});

View File

@ -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();