2017-02-03 08:32:24 +01:00
|
|
|
// polyfill fetch
|
|
|
|
global.fetch = global.fetch || require('isomorphic-fetch')
|
|
|
|
|
|
|
|
const assert = require('assert')
|
|
|
|
const nock = require('nock')
|
2017-02-27 19:53:36 +01:00
|
|
|
const CurrencyController = require('../../app/scripts/controllers/currency')
|
2017-02-03 08:32:24 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('currency-controller', function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
var currencyController
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
beforeEach(function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
currencyController = new CurrencyController()
|
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('currency conversions', function () {
|
|
|
|
describe('#setCurrentCurrency', function () {
|
|
|
|
it('should return USD as default', function () {
|
2017-09-26 19:02:49 +02:00
|
|
|
assert.equal(currencyController.getCurrentCurrency(), 'usd')
|
2017-02-03 08:32:24 +01:00
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
it('should be able to set to other currency', function () {
|
2017-09-26 19:02:49 +02:00
|
|
|
assert.equal(currencyController.getCurrentCurrency(), 'usd')
|
2017-02-03 08:32:24 +01:00
|
|
|
currencyController.setCurrentCurrency('JPY')
|
|
|
|
var result = currencyController.getCurrentCurrency()
|
|
|
|
assert.equal(result, 'JPY')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#getConversionRate', function () {
|
|
|
|
it('should return undefined if non-existent', function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
var result = currencyController.getConversionRate()
|
|
|
|
assert.ok(!result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('#updateConversionRate', function () {
|
|
|
|
it('should retrieve an update for ETH to USD and set it in memory', function (done) {
|
2017-02-03 08:32:24 +01:00
|
|
|
this.timeout(15000)
|
2017-09-26 19:02:49 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/v1/ticker/ethusd')
|
|
|
|
.reply(200, '{"base": "ETH", "quote": "USD", "bid": 288.45, "ask": 288.46, "volume": 112888.17569277, "exchange": "bitfinex", "total_volume": 272175.00106721005, "num_exchanges": 8, "timestamp": 1506444677}')
|
2017-02-03 08:32:24 +01:00
|
|
|
|
|
|
|
assert.equal(currencyController.getConversionRate(), 0)
|
2017-09-26 19:02:49 +02:00
|
|
|
currencyController.setCurrentCurrency('usd')
|
2017-02-03 08:32:24 +01:00
|
|
|
currencyController.updateConversionRate()
|
2017-05-04 23:35:10 +02:00
|
|
|
.then(function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
var result = currencyController.getConversionRate()
|
|
|
|
console.log('currencyController.getConversionRate:', result)
|
|
|
|
assert.equal(typeof result, 'number')
|
|
|
|
done()
|
2017-05-04 23:35:10 +02:00
|
|
|
}).catch(function (err) {
|
2017-02-03 08:32:24 +01:00
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
it('should work for JPY as well.', function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
this.timeout(15000)
|
|
|
|
assert.equal(currencyController.getConversionRate(), 0)
|
|
|
|
|
2017-09-26 19:02:49 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/v1/ticker/ethjpy')
|
|
|
|
.reply(200, '{"base": "ETH", "quote": "JPY", "bid": 32300.0, "ask": 32400.0, "volume": 247.4616071, "exchange": "kraken", "total_volume": 247.4616071, "num_exchanges": 1, "timestamp": 1506444676}')
|
2017-02-03 08:32:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
var promise = new Promise(
|
|
|
|
function (resolve, reject) {
|
2017-09-26 19:02:49 +02:00
|
|
|
currencyController.setCurrentCurrency('jpy')
|
2017-05-04 23:35:10 +02:00
|
|
|
currencyController.updateConversionRate().then(function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
resolve()
|
|
|
|
})
|
2017-05-04 23:35:10 +02:00
|
|
|
})
|
2017-02-03 08:32:24 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
promise.then(function () {
|
2017-02-03 08:32:24 +01:00
|
|
|
var result = currencyController.getConversionRate()
|
|
|
|
assert.equal(typeof result, 'number')
|
2017-05-04 23:35:10 +02:00
|
|
|
}).catch(function (done, err) {
|
2017-02-03 08:32:24 +01:00
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|