You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.8 KiB
79 lines
2.8 KiB
8 years ago
|
const assert = require('assert')
|
||
|
const nock = require('nock')
|
||
7 years ago
|
const CurrencyController = require('../../../../app/scripts/controllers/currency')
|
||
8 years ago
|
|
||
8 years ago
|
describe('currency-controller', function () {
|
||
8 years ago
|
var currencyController
|
||
|
|
||
8 years ago
|
beforeEach(function () {
|
||
8 years ago
|
currencyController = new CurrencyController()
|
||
|
})
|
||
|
|
||
8 years ago
|
describe('currency conversions', function () {
|
||
|
describe('#setCurrentCurrency', function () {
|
||
|
it('should return USD as default', function () {
|
||
7 years ago
|
assert.equal(currencyController.getCurrentCurrency(), 'usd')
|
||
8 years ago
|
})
|
||
|
|
||
8 years ago
|
it('should be able to set to other currency', function () {
|
||
7 years ago
|
assert.equal(currencyController.getCurrentCurrency(), 'usd')
|
||
8 years ago
|
currencyController.setCurrentCurrency('JPY')
|
||
|
var result = currencyController.getCurrentCurrency()
|
||
|
assert.equal(result, 'JPY')
|
||
|
})
|
||
|
})
|
||
|
|
||
8 years ago
|
describe('#getConversionRate', function () {
|
||
|
it('should return undefined if non-existent', function () {
|
||
8 years ago
|
var result = currencyController.getConversionRate()
|
||
|
assert.ok(!result)
|
||
|
})
|
||
|
})
|
||
|
|
||
8 years ago
|
describe('#updateConversionRate', function () {
|
||
|
it('should retrieve an update for ETH to USD and set it in memory', function (done) {
|
||
8 years ago
|
this.timeout(15000)
|
||
7 years ago
|
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}')
|
||
8 years ago
|
|
||
|
assert.equal(currencyController.getConversionRate(), 0)
|
||
7 years ago
|
currencyController.setCurrentCurrency('usd')
|
||
8 years ago
|
currencyController.updateConversionRate()
|
||
8 years ago
|
.then(function () {
|
||
8 years ago
|
var result = currencyController.getConversionRate()
|
||
|
assert.equal(typeof result, 'number')
|
||
|
done()
|
||
8 years ago
|
}).catch(function (err) {
|
||
8 years ago
|
done(err)
|
||
|
})
|
||
|
})
|
||
|
|
||
8 years ago
|
it('should work for JPY as well.', function () {
|
||
8 years ago
|
this.timeout(15000)
|
||
|
assert.equal(currencyController.getConversionRate(), 0)
|
||
|
|
||
7 years ago
|
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}')
|
||
8 years ago
|
|
||
|
|
||
|
var promise = new Promise(
|
||
|
function (resolve, reject) {
|
||
7 years ago
|
currencyController.setCurrentCurrency('jpy')
|
||
8 years ago
|
currencyController.updateConversionRate().then(function () {
|
||
8 years ago
|
resolve()
|
||
|
})
|
||
8 years ago
|
})
|
||
8 years ago
|
|
||
8 years ago
|
promise.then(function () {
|
||
8 years ago
|
var result = currencyController.getConversionRate()
|
||
|
assert.equal(typeof result, 'number')
|
||
8 years ago
|
}).catch(function (done, err) {
|
||
8 years ago
|
done(err)
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
})
|