mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
Progress on config manager. Plus tests.
This commit is contained in:
parent
5b895c2a22
commit
aac810b1eb
@ -1,6 +1,7 @@
|
||||
const Migrator = require('pojo-migrator')
|
||||
const MetamaskConfig = require('../config.js')
|
||||
const migrations = require('./migrations')
|
||||
const rp = require('request-promise')
|
||||
|
||||
const TESTNET_RPC = MetamaskConfig.network.testnet
|
||||
const MAINNET_RPC = MetamaskConfig.network.mainnet
|
||||
@ -270,3 +271,29 @@ ConfigManager.prototype.getConfirmed = function () {
|
||||
return ('isConfirmed' in data) && data.isConfirmed
|
||||
}
|
||||
|
||||
ConfigManager.prototype.setCurrentFiat = function (currency) {
|
||||
var data = this.getData()
|
||||
data.fiatCurrency = currency
|
||||
this.setData(data)
|
||||
}
|
||||
|
||||
ConfigManager.prototype.getCurrentFiat = function () {
|
||||
var data = this.getData()
|
||||
return ('fiatCurrency' in data) && data.fiatCurrency
|
||||
}
|
||||
|
||||
ConfigManager.prototype.setConversionRate = function () {
|
||||
var data = this.getData()
|
||||
return rp(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`)
|
||||
.then(function (response) {
|
||||
data.conversionRate = Number(JSON.parse(response).ticker.price)
|
||||
this.setData(data)
|
||||
}.bind(this)).catch(function (err) {
|
||||
console.log('Error in conversion.', err)
|
||||
})
|
||||
}
|
||||
|
||||
ConfigManager.prototype.getConversionRate = function () {
|
||||
var data = this.getData()
|
||||
return ('conversionRate' in data) && data.conversionRate
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ const extend = require('xtend')
|
||||
const STORAGE_KEY = 'metamask-persistance-key'
|
||||
var configManagerGen = require('../lib/mock-config-manager')
|
||||
var configManager
|
||||
const rp = require('request-promise')
|
||||
|
||||
describe('config-manager', function() {
|
||||
|
||||
@ -11,6 +12,86 @@ describe('config-manager', function() {
|
||||
configManager = configManagerGen()
|
||||
})
|
||||
|
||||
describe('currency conversions', function() {
|
||||
|
||||
describe('#getCurrentFiat', function() {
|
||||
it('should return false if no previous key exists', function() {
|
||||
var result = configManager.getCurrentFiat()
|
||||
assert.ok(!result)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#setCurrentFiat', function() {
|
||||
it('should make getCurrentFiat return true once set', function() {
|
||||
assert.equal(configManager.getCurrentFiat(), false)
|
||||
configManager.setCurrentFiat('usd')
|
||||
var result = configManager.getCurrentFiat()
|
||||
assert.equal(result, 'usd')
|
||||
})
|
||||
|
||||
it('should work with other currencies as well', function() {
|
||||
assert.equal(configManager.getCurrentFiat(), false)
|
||||
configManager.setCurrentFiat('jpy')
|
||||
var result = configManager.getCurrentFiat()
|
||||
assert.equal(result, 'jpy')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getConversionRate', function() {
|
||||
it('should return false if non-existent', function() {
|
||||
var result = configManager.getConversionRate()
|
||||
assert.ok(!result)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#setConversionRate', function() {
|
||||
it('should retrieve an update for ETH to USD and set it in memory', function(done) {
|
||||
this.timeout(15000)
|
||||
assert.equal(configManager.getConversionRate(), false)
|
||||
var promise = new Promise(
|
||||
function (resolve, reject) {
|
||||
configManager.setCurrentFiat('usd')
|
||||
configManager.setConversionRate().then(function() {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
promise.then(function() {
|
||||
var result = configManager.getConversionRate()
|
||||
assert.equal(typeof result, 'number')
|
||||
done()
|
||||
}).catch(function(err) {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
it('should work for JPY as well.', function() {
|
||||
this.timeout(15000)
|
||||
assert.equal(configManager.getConversionRate(), false)
|
||||
var promise = new Promise(
|
||||
function (resolve, reject) {
|
||||
configManager.setCurrentFiat('jpy')
|
||||
configManager.setConversionRate().then(function() {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
promise.then(function() {
|
||||
var result = configManager.getConversionRate()
|
||||
assert.equal(typeof result, 'number')
|
||||
done()
|
||||
}).catch(function(err) {
|
||||
console.log(err)
|
||||
})
|
||||
})
|
||||
|
||||
xit('should activate every time the currency is changed.', function() {
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('confirmation', function() {
|
||||
|
||||
describe('#getConfirmed', function() {
|
||||
@ -215,4 +296,3 @@ describe('config-manager', function() {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user