mirror of https://github.com/BTCPrivate/copay.git
Test for getHistoricRate
This commit is contained in:
parent
82a26f76ea
commit
f41d795921
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue