Skip to content

Commit

Permalink
move delegate api call to account actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gina Contrino committed Jun 11, 2018
1 parent 9b0f375 commit 41319a9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
12 changes: 12 additions & 0 deletions src/actions/account.js
Expand Up @@ -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 },
)));
});
};

/**
*
*/
Expand Down
26 changes: 26 additions & 0 deletions src/actions/account.test.js
Expand Up @@ -12,6 +12,7 @@ import {
loadAccount,
accountDataUpdated,
updateTransactionsIfNeeded,
updateDelegateAccount,
} from './account';
import { errorAlertDialogDisplayed } from './dialog';
import { delegateRegisteredFailure } from './delegate';
Expand Down Expand Up @@ -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);
});
});
});
18 changes: 9 additions & 9 deletions 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;
Expand Down Expand Up @@ -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,
}));
}
};

Expand Down
12 changes: 3 additions & 9 deletions src/store/middlewares/account.test.js
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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`, () => {
Expand Down

0 comments on commit 41319a9

Please sign in to comment.