diff --git a/src/actions/account.js b/src/actions/account.js index 66572099d7..1b95a9fe2c 100644 --- a/src/actions/account.js +++ b/src/actions/account.js @@ -113,6 +113,18 @@ export const secondPassphraseRegistered = ({ dispatch(passphraseUsed(passphrase)); }; + +export const updateDelegateAccount = ({ activePeer, publicKey }) => + (dispatch) => { + getDelegate(activePeer, { publicKey }) + .then((delegateData) => { + dispatch(accountUpdated(Object.assign( + {}, + { delegate: delegateData.delegate, isDelegate: true }, + ))); + }); + }; + /** * */ diff --git a/src/actions/account.test.js b/src/actions/account.test.js index 1e784dbd34..46a22b5988 100644 --- a/src/actions/account.test.js +++ b/src/actions/account.test.js @@ -12,6 +12,7 @@ import { loadAccount, accountDataUpdated, updateTransactionsIfNeeded, + updateDelegateAccount, } from './account'; import { errorAlertDialogDisplayed } from './dialog'; import { delegateRegisteredFailure } from './delegate'; @@ -377,4 +378,29 @@ describe('actions: account', () => { expect(transactionsActionsStub).to.have.been.calledWith(); }); }); + + describe('updateDelegateAccount', () => { + const dispatch = spy(); + + beforeEach(() => { + stub(delegateApi, 'getDelegate').returnsPromise(); + }); + + afterEach(() => { + delegateApi.getDelegate.restore(); + }); + + it('should fetch delegate and update account', () => { + delegateApi.getDelegate.resolves({ delegate: 'delegate data' }); + const data = { + activePeer: {}, + publicKey: accounts.genesis.publicKey, + }; + + updateDelegateAccount(data)(dispatch); + + const accountUpdatedAction = accountUpdated(Object.assign({}, { delegate: 'delegate data', isDelegate: true })); + expect(dispatch).to.have.been.calledWith(accountUpdatedAction); + }); + }); }); diff --git a/src/store/middlewares/account.js b/src/store/middlewares/account.js index 68d89c01a3..fe9e59c8f4 100644 --- a/src/store/middlewares/account.js +++ b/src/store/middlewares/account.js @@ -1,8 +1,11 @@ -import { accountUpdated, accountDataUpdated, updateTransactionsIfNeeded } from '../../actions/account'; +import { accountUpdated, + accountDataUpdated, + updateTransactionsIfNeeded, + updateDelegateAccount, +} from '../../actions/account'; import { votesFetched } from '../../actions/voting'; import actionTypes from '../../constants/actions'; import accountConfig from '../../constants/account'; -import { getDelegate } from '../../utils/api/delegate'; import transactionTypes from '../../constants/transactionTypes'; const { lockDuration } = accountConfig; @@ -34,13 +37,10 @@ const delegateRegistration = (store, action) => { const state = store.getState(); if (delegateRegistrationTx) { - getDelegate(state.peers.data, { publicKey: state.account.publicKey }) - .then((delegateData) => { - store.dispatch(accountUpdated(Object.assign( - {}, - { delegate: delegateData.delegate, isDelegate: true }, - ))); - }); + store.dispatch(updateDelegateAccount({ + activePeer: state.peers.data, + publicKey: state.account.publicKey, + })); } }; diff --git a/src/store/middlewares/account.test.js b/src/store/middlewares/account.test.js index 78ec6734fe..8b2574876a 100644 --- a/src/store/middlewares/account.test.js +++ b/src/store/middlewares/account.test.js @@ -8,7 +8,6 @@ import * as accountApi from '../../utils/api/account'; import * as transactionsApi from '../../utils/api/transactions'; import accounts from '../../../test/constants/accounts'; import actionTypes from '../../constants/actions'; -import * as delegateApi from '../../utils/api/delegate'; import middleware from './account'; import transactionTypes from '../../constants/transactionTypes'; @@ -68,12 +67,14 @@ describe('Account middleware', () => { next = spy(); spy(accountActions, 'updateTransactionsIfNeeded'); + spy(accountActions, 'updateDelegateAccount'); stubGetAccount = stub(accountApi, 'getAccount').returnsPromise(); transactionsActionsStub = spy(transactionsActions, 'transactionsUpdated'); stubTransactions = stub(transactionsApi, 'getTransactions').returnsPromise().resolves(true); }); afterEach(() => { + accountActions.updateDelegateAccount.restore(); accountActions.updateTransactionsIfNeeded.restore(); transactionsActionsStub.restore(); stubGetAccount.restore(); @@ -138,22 +139,15 @@ describe('Account middleware', () => { }); it(`should fetch delegate info on ${actionTypes.transactionsUpdated} action if action.data.confirmed contains delegateRegistration transactions`, () => { - const delegateApiMock = stub(delegateApi, 'getDelegate').returnsPromise().resolves({ success: true, delegate: {} }); - middleware(store)(next)(transactionsUpdatedAction); - expect(delegateApiMock).to.have.been.calledWith(); - - delegateApiMock.restore(); + expect(accountActions.updateDelegateAccount).to.have.been.calledWith(); }); it(`should not fetch delegate info on ${actionTypes.transactionsUpdated} action if action.data.confirmed does not contain delegateRegistration transactions`, () => { - const delegateApiMock = stub(delegateApi, 'getDelegate').returnsPromise().resolves({ success: true, delegate: {} }); transactionsUpdatedAction.data.confirmed[0].type = transactionTypes.send; middleware(store)(next)(transactionsUpdatedAction); expect(store.dispatch).to.not.have.been.calledWith(); - - delegateApiMock.restore(); }); it(`should dispatch ${actionTypes.votesFetched} action on ${actionTypes.transactionsUpdated} action if action.data.confirmed contains delegateRegistration transactions`, () => {