added more unit tests

feature/default_network_editable
brunobar79 6 years ago
parent e5512c306d
commit de4265c629
  1. 7
      app/scripts/metamask-controller.js
  2. 57
      test/unit/app/controllers/metamask-controller-test.js

@ -628,17 +628,16 @@ module.exports = class MetamaskController extends EventEmitter {
const oldAccounts = await keyringController.getAccounts() const oldAccounts = await keyringController.getAccounts()
const keyState = await keyringController.addNewAccount(keyring) const keyState = await keyringController.addNewAccount(keyring)
const newAccounts = await keyringController.getAccounts() const newAccounts = await keyringController.getAccounts()
// Assuming the trezor account is the last one
const trezorAccount = newAccounts[newAccounts.length - 1]
this.preferencesController.setAddresses(newAccounts) this.preferencesController.setAddresses(newAccounts)
console.log('new vs old', newAccounts, oldAccounts)
newAccounts.forEach(address => { newAccounts.forEach(address => {
if (!oldAccounts.includes(address)) { if (!oldAccounts.includes(address)) {
console.log('new address found', address)
this.preferencesController.setAccountLabel(address, `TREZOR #${parseInt(index, 10) + 1}`)
this.preferencesController.setSelectedAddress(address) this.preferencesController.setSelectedAddress(address)
} }
}) })
this.preferencesController.setAccountLabel(trezorAccount, `TREZOR #${parseInt(index, 10) + 1}`)
this.preferencesController.setSelectedAddress(trezorAccount)
const { identities } = this.preferencesController.store.getState() const { identities } = this.preferencesController.store.getState()
return { ...keyState, identities } return { ...keyState, identities }
} }

@ -233,10 +233,12 @@ describe('MetaMaskController', function () {
}) })
it('should add the Trezor Hardware keyring', async function () { it('should add the Trezor Hardware keyring', async function () {
sinon.spy(metamaskController.keyringController, 'addNewKeyring')
await metamaskController.connectHardware('trezor', 0).catch((e) => null) await metamaskController.connectHardware('trezor', 0).catch((e) => null)
const keyrings = await metamaskController.keyringController.getKeyringsByType( const keyrings = await metamaskController.keyringController.getKeyringsByType(
'Trezor Hardware' 'Trezor Hardware'
) )
assert.equal(metamaskController.keyringController.addNewKeyring.getCall(0).args, 'Trezor Hardware')
assert.equal(keyrings.length, 1) assert.equal(keyrings.length, 1)
}) })
@ -281,15 +283,66 @@ describe('MetaMaskController', function () {
}) })
describe('unlockTrezorAccount', function () { describe('unlockTrezorAccount', function () {
it('should set accountToUnlock in the keyring', async function () { let accountToUnlock
let windowOpenStub
let addNewAccountStub
let getAccountsStub
beforeEach(async function () {
accountToUnlock = 10
windowOpenStub = sinon.stub(window, 'open')
windowOpenStub.returns(noop)
addNewAccountStub = sinon.stub(metamaskController.keyringController, 'addNewAccount')
addNewAccountStub.returns({})
getAccountsStub = sinon.stub(metamaskController.keyringController, 'getAccounts')
// Need to return different address to mock the behavior of
// adding a new account from the keyring
getAccountsStub.onCall(0).returns(Promise.resolve(['0x1']))
getAccountsStub.onCall(1).returns(Promise.resolve(['0x2']))
getAccountsStub.onCall(2).returns(Promise.resolve(['0x3']))
getAccountsStub.onCall(3).returns(Promise.resolve(['0x4']))
sinon.spy(metamaskController.preferencesController, 'setAddresses')
sinon.spy(metamaskController.preferencesController, 'setSelectedAddress')
sinon.spy(metamaskController.preferencesController, 'setAccountLabel')
await metamaskController.connectHardware('trezor', 0).catch((e) => null) await metamaskController.connectHardware('trezor', 0).catch((e) => null)
const accountToUnlock = 10
await metamaskController.unlockTrezorAccount(accountToUnlock).catch((e) => null) await metamaskController.unlockTrezorAccount(accountToUnlock).catch((e) => null)
})
afterEach(function () {
metamaskController.keyringController.addNewAccount.restore()
window.open.restore()
})
it('should set accountToUnlock in the keyring', async function () {
const keyrings = await metamaskController.keyringController.getKeyringsByType( const keyrings = await metamaskController.keyringController.getKeyringsByType(
'Trezor Hardware' 'Trezor Hardware'
) )
assert.equal(keyrings[0].unlockedAccount, accountToUnlock) assert.equal(keyrings[0].unlockedAccount, accountToUnlock)
}) })
it('should call keyringController.addNewAccount', async function () {
assert(metamaskController.keyringController.addNewAccount.calledOnce)
})
it('should call keyringController.getAccounts ', async function () {
assert(metamaskController.keyringController.getAccounts.called)
})
it('should call preferencesController.setAddresses', async function () {
assert(metamaskController.preferencesController.setAddresses.calledOnce)
})
it('should call preferencesController.setSelectedAddress', async function () {
assert(metamaskController.preferencesController.setSelectedAddress.calledOnce)
})
it('should call preferencesController.setAccountLabel', async function () {
assert(metamaskController.preferencesController.setAccountLabel.calledOnce)
})
}) })
describe('#setCustomRpc', function () { describe('#setCustomRpc', function () {

Loading…
Cancel
Save