1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-22 17:33:23 +01:00

Tested against code to play nice with unit tests.

This commit is contained in:
Kevin Serrano 2017-02-23 13:56:58 -08:00
parent 3be6ee5f6c
commit 62854398f1
No known key found for this signature in database
GPG Key ID: 7CC862A58D2889B4
6 changed files with 54 additions and 14 deletions

View File

@ -25,6 +25,14 @@ class PreferencesController {
return this.store.getState().selectedAddress
}
updateFrequentRpcList (_url) {
return this.addToFrequentRpcList(_url)
.then((rpcList) => {
this.store.updateState({ frequentRpcList: rpcList })
return rpcList
})
}
addToFrequentRpcList (_url) {
let rpcList = this.getFrequentRpcList()
let index = rpcList.findIndex((element) => { return element === _url })
@ -37,8 +45,7 @@ class PreferencesController {
if (rpcList.length > 2) {
rpcList.shift()
}
this.store.updateState({ frequentRpcList: rpcList })
return Promise.resolve()
return Promise.resolve(rpcList)
}
getFrequentRpcList () {
@ -49,6 +56,8 @@ class PreferencesController {
// PRIVATE METHODS
//
}
module.exports = PreferencesController

View File

@ -259,7 +259,7 @@ module.exports = class MetamaskController extends EventEmitter {
// PreferencesController
setSelectedAddress: nodeify(preferencesController.setSelectedAddress).bind(preferencesController),
addToFrequentRpcList: nodeify(preferencesController.addToFrequentRpcList).bind(preferencesController),
updateFrequentRpcList: nodeify(preferencesController.updateFrequentRpcList).bind(preferencesController),
// KeyringController
setLocked: nodeify(keyringController.setLocked).bind(keyringController),

View File

@ -11,6 +11,7 @@ describe ('config view actions', function() {
var initialState = {
metamask: {
rpcTarget: 'foo',
frequentRpcList: []
},
appState: {
currentView: {
@ -30,15 +31,36 @@ describe ('config view actions', function() {
describe('SET_RPC_TARGET', function() {
it('sets the state.metamask.rpcTarget property of the state to the action.value', function() {
const value = {
rpcTarget: 'foo',
frequentRpcList: ['foo']
}
const action = {
type: actions.SET_RPC_TARGET,
value: 'bar',
value,
}
var result = reducers(initialState, action)
assert.equal(result.metamask.provider.type, 'rpc')
assert.equal(result.metamask.provider.rpcTarget, action.value)
assert.equal(result.metamask.provider.rpcTarget, value.rpcTarget)
assert.equal(result.metamask.frequentRpcList[0], value.frequentRpcList[0])
})
})
})
it('should handle multiple requests to change the rpc gracefully', function() {
const value = {
rpcTarget: 'foo',
frequentRpcList: ['foo']
}
const action = {
type: actions.SET_RPC_TARGET,
value,
}
var result = reducers(initialState, action)
var secondResult = reducers(result, action)
assert.equal(secondResult.metamask.frequentRpcList.length, 1)
})
})
})

View File

@ -626,13 +626,19 @@ function markAccountsFound() {
//
function setRpcTarget (newRpc) {
return (dispatch) => {
if (global.METAMASK_DEBUG) console.log(`background.setRpcTarget`)
background.addToFrequentRpcList(newRpc, () => {
background.setRpcTarget(newRpc)
})
return {
background.updateFrequentRpcList(newRpc, (frequentRpcList) => {
const value = {
rpcTarget: newRpc,
frequentRpcList,
}
dispatch({
type: actions.SET_RPC_TARGET,
value: newRpc,
value,
})
})
}
}

View File

@ -257,7 +257,9 @@ App.prototype.renderNetworkDropdown = function () {
h(DropMenuItem, {
label: 'Localhost 8545',
closeMenu: () => this.setState({ isNetworkMenuOpen: false }),
action: () => props.dispatch(actions.setRpcTarget('http://localhost:8545')),
action: () => {
props.dispatch(actions.setRpcTarget('http://localhost:8545'))
},
icon: h('i.fa.fa-question-circle.fa-lg'),
activeNetworkRender: props.provider.rpcTarget,
}),

View File

@ -55,9 +55,10 @@ function reduceMetamask (state, action) {
case actions.SET_RPC_TARGET:
return extend(metamaskState, {
frequentRpcList: action.value.frequentRpcList,
provider: {
type: 'rpc',
rpcTarget: action.value,
rpcTarget: action.value.rpcTarget,
},
})