nifty-wallet/test/unit/currency-controller-test.js

83 lines
2.9 KiB
JavaScript
Raw Normal View History

// polyfill fetch
global.fetch = global.fetch || require('isomorphic-fetch')
const assert = require('assert')
const nock = require('nock')
2017-02-27 10:53:36 -08:00
const CurrencyController = require('../../app/scripts/controllers/currency')
2017-05-04 14:35:10 -07:00
describe('currency-controller', function () {
var currencyController
2017-05-04 14:35:10 -07:00
beforeEach(function () {
currencyController = new CurrencyController()
})
2017-05-04 14:35:10 -07:00
describe('currency conversions', function () {
describe('#setCurrentCurrency', function () {
it('should return USD as default', function () {
assert.equal(currencyController.getCurrentCurrency(), 'USD')
})
2017-05-04 14:35:10 -07:00
it('should be able to set to other currency', function () {
assert.equal(currencyController.getCurrentCurrency(), 'USD')
currencyController.setCurrentCurrency('JPY')
var result = currencyController.getCurrentCurrency()
assert.equal(result, 'JPY')
})
})
2017-05-04 14:35:10 -07:00
describe('#getConversionRate', function () {
it('should return undefined if non-existent', function () {
var result = currencyController.getConversionRate()
assert.ok(!result)
})
})
2017-05-04 14:35:10 -07:00
describe('#updateConversionRate', function () {
it('should retrieve an update for ETH to USD and set it in memory', function (done) {
this.timeout(15000)
2017-06-05 16:24:32 -07:00
nock('https://api.cryptonator.com')
.get('/api/ticker/eth-USD')
.reply(200, '{"ticker":{"base":"ETH","target":"USD","price":"11.02456145","volume":"44948.91745289","change":"-0.01472534"},"timestamp":1472072136,"success":true,"error":""}')
assert.equal(currencyController.getConversionRate(), 0)
currencyController.setCurrentCurrency('USD')
currencyController.updateConversionRate()
2017-05-04 14:35:10 -07:00
.then(function () {
var result = currencyController.getConversionRate()
console.log('currencyController.getConversionRate:', result)
assert.equal(typeof result, 'number')
done()
2017-05-04 14:35:10 -07:00
}).catch(function (err) {
done(err)
})
})
2017-05-04 14:35:10 -07:00
it('should work for JPY as well.', function () {
this.timeout(15000)
assert.equal(currencyController.getConversionRate(), 0)
2017-06-05 16:24:32 -07:00
nock('https://api.cryptonator.com')
.get('/api/ticker/eth-JPY')
.reply(200, '{"ticker":{"base":"ETH","target":"JPY","price":"11.02456145","volume":"44948.91745289","change":"-0.01472534"},"timestamp":1472072136,"success":true,"error":""}')
var promise = new Promise(
function (resolve, reject) {
currencyController.setCurrentCurrency('JPY')
2017-05-04 14:35:10 -07:00
currencyController.updateConversionRate().then(function () {
resolve()
})
2017-05-04 14:35:10 -07:00
})
2017-05-04 14:35:10 -07:00
promise.then(function () {
var result = currencyController.getConversionRate()
assert.equal(typeof result, 'number')
2017-05-04 14:35:10 -07:00
}).catch(function (done, err) {
done(err)
})
})
})
})
})