Skip to content

Commit

Permalink
Move transaction related functions to their own action and api files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gina Contrino committed Jun 8, 2018
1 parent 1c7d11b commit ea6dca2
Show file tree
Hide file tree
Showing 16 changed files with 271 additions and 207 deletions.
33 changes: 1 addition & 32 deletions src/actions/account.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import i18next from 'i18next';
import actionTypes from '../constants/actions';
import { setSecondPassphrase, send, getAccount } from '../utils/api/account';
import { setSecondPassphrase, getAccount } from '../utils/api/account';
import { registerDelegate, getDelegate, getVotes, getVoters } from '../utils/api/delegate';
import { loadTransactionsFinish } from './transactions';
import { delegateRegisteredFailure } from './delegate';
import { errorAlertDialogDisplayed } from './dialog';
import Fees from '../constants/fees';
import { toRawLsk } from '../utils/lsk';
import transactionTypes from '../constants/transactionTypes';

/**
Expand Down Expand Up @@ -142,36 +141,6 @@ export const delegateRegistered = ({
dispatch(passphraseUsed(passphrase));
};

/**
*
*/
export const sent = ({
activePeer, account, recipientId, amount, passphrase, secondPassphrase,
}) =>
(dispatch) => {
send(activePeer, recipientId, toRawLsk(amount), passphrase, secondPassphrase)
.then((data) => {
dispatch({
data: {
id: data.transactionId,
senderPublicKey: account.publicKey,
senderId: account.address,
recipientId,
amount: toRawLsk(amount),
fee: Fees.send,
type: transactionTypes.send,
},
type: actionTypes.transactionAdded,
});
})
.catch((error) => {
const errorMessage = error && error.message ? `${error.message}.` : i18next.t('An error occurred while creating the transaction.');
dispatch({ data: { errorMessage }, type: actionTypes.transactionFailed });
});
dispatch(passphraseUsed(passphrase));
};


export const loadDelegate = ({ activePeer, publicKey }) =>
(dispatch) => {
getDelegate(activePeer, { publicKey }).then((response) => {
Expand Down
85 changes: 9 additions & 76 deletions src/actions/account.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { expect } from 'chai';
import sinon from 'sinon';
import actionTypes from '../constants/actions';
import { accountUpdated, accountLoggedOut,
secondPassphraseRegistered, delegateRegistered, sent, removePassphrase, passphraseUsed, loadDelegate } from './account';
import {
accountUpdated,
accountLoggedOut,
secondPassphraseRegistered,
delegateRegistered,
removePassphrase,
passphraseUsed,
loadDelegate,
} from './account';
import { errorAlertDialogDisplayed } from './dialog';
import { delegateRegisteredFailure } from './delegate';
import * as accountApi from '../utils/api/account';
import * as delegateApi from '../utils/api/delegate';
import Fees from '../constants/fees';
import { toRawLsk } from '../utils/lsk';
import transactionTypes from '../constants/transactionTypes';
import networks from '../constants/networks';
import accounts from '../../test/constants/accounts';
Expand Down Expand Up @@ -190,79 +196,6 @@ describe('actions: account', () => {
});
});

describe('sent', () => {
let accountApiMock;
const data = {
activePeer: {},
recipientId: '15833198055097037957L',
amount: 100,
passphrase: 'sample passphrase',
secondPassphrase: null,
account: {
publicKey: 'test_public-key',
address: 'test_address',
},
};
const actionFunction = sent(data);
let dispatch;

beforeEach(() => {
accountApiMock = sinon.stub(accountApi, 'send');
dispatch = sinon.spy();
});

afterEach(() => {
accountApiMock.restore();
});

it('should create an action function', () => {
expect(typeof actionFunction).to.be.deep.equal('function');
});

it('should dispatch transactionAdded action if resolved', () => {
accountApiMock.returnsPromise().resolves({ transactionId: '15626650747375562521' });
const expectedAction = {
id: '15626650747375562521',
senderPublicKey: 'test_public-key',
senderId: 'test_address',
recipientId: data.recipientId,
amount: toRawLsk(data.amount),
fee: Fees.send,
type: transactionTypes.send,
};

actionFunction(dispatch);
expect(dispatch).to.have.been
.calledWith({ data: expectedAction, type: actionTypes.transactionAdded });
});

it('should dispatch transactionFailed action if caught', () => {
accountApiMock.returnsPromise().rejects({ message: 'sample message' });

actionFunction(dispatch);
const expectedAction = {
data: {
errorMessage: 'sample message.',
},
type: actionTypes.transactionFailed,
};
expect(dispatch).to.have.been.calledWith(expectedAction);
});

it('should dispatch transactionFailed action if caught but no message returned', () => {
accountApiMock.returnsPromise().rejects({});

actionFunction(dispatch);
const expectedAction = {
data: {
errorMessage: 'An error occurred while creating the transaction.',
},
type: actionTypes.transactionFailed,
};
expect(dispatch).to.have.been.calledWith(expectedAction);
});
});

describe('removePassphrase', () => {
it('should create an action to remove passphrase', () => {
const data = {
Expand Down
11 changes: 4 additions & 7 deletions src/actions/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import actionTypes from '../constants/actions';
import { loadingStarted, loadingFinished } from '../utils/loading';
import { transactions, getAccount } from '../utils/api/account';
import { getAccount } from '../utils/api/account';
import { getTransactions } from '../utils/api/transactions';
import { getDelegate, getVoters, getVotes } from '../utils/api/delegate';
import { searchAll } from '../utils/api/search';

Expand Down Expand Up @@ -62,9 +63,7 @@ export const searchTransactions = ({
}) =>
(dispatch) => {
if (showLoading) loadingStarted(actionTypes.searchTransactions);
transactions({
activePeer, address, limit, filter,
})
getTransactions({ activePeer, address, limit, filter })
.then((transactionsResponse) => {
dispatch({
data: {
Expand All @@ -83,9 +82,7 @@ export const searchMoreTransactions = ({
activePeer, address, limit, offset, filter,
}) =>
(dispatch) => {
transactions({
activePeer, address, limit, offset, filter,
})
getTransactions({ activePeer, address, limit, offset, filter })
.then((transactionsResponse) => {
dispatch({
data: {
Expand Down
71 changes: 56 additions & 15 deletions src/actions/transactions.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import i18next from 'i18next';
import actionTypes from '../constants/actions';
import { loadingStarted, loadingFinished } from '../utils/loading';
import { transactions, transaction, unconfirmedTransactions } from '../utils/api/account';
import { send, getTransactions, getSingleTransaction, unconfirmedTransactions } from '../utils/api/transactions';
import { getDelegate } from '../utils/api/delegate';
import { loadDelegateCache } from '../utils/delegates';
import { extractAddress } from '../utils/account';
import { loadAccount } from './account';
import { loadAccount, passphraseUsed } from './account';
import Fees from '../constants/fees';
import { toRawLsk } from '../utils/lsk';
import transactionTypes from '../constants/transactionTypes';

export const transactionsFilterSet = ({
activePeer, address, limit, filter,
}) =>
(dispatch) => {
transactions({
getTransactions({
activePeer,
address,
limit,
Expand Down Expand Up @@ -50,7 +54,7 @@ export const loadTransactions = ({ activePeer, publicKey, address }) =>
const lastActiveAddress = publicKey && extractAddress(publicKey);
const isSameAccount = lastActiveAddress === address;
loadingStarted(actionTypes.transactionsLoad);
transactions({ activePeer, address, limit: 25 })
getTransactions({ activePeer, address, limit: 25 })
.then((transactionsResponse) => {
dispatch(loadAccount({
activePeer,
Expand All @@ -68,17 +72,9 @@ export const loadTransactions = ({ activePeer, publicKey, address }) =>
});
};

/**
*
*
*/
export const transactionsRequested = ({
activePeer, address, limit, offset, filter,
}) =>
export const transactionsRequested = ({ activePeer, address, limit, offset, filter }) =>
(dispatch) => {
transactions({
activePeer, address, limit, offset, filter,
})
getTransactions({ activePeer, address, limit, offset, filter })
.then((response) => {
dispatch({
data: {
Expand All @@ -95,7 +91,7 @@ export const transactionsRequested = ({
export const loadTransaction = ({ activePeer, id }) =>
(dispatch) => {
dispatch({ type: actionTypes.transactionCleared });
transaction({ activePeer, id })
getSingleTransaction({ activePeer, id })
.then((response) => {
const added = (response.transaction.votes && response.transaction.votes.added) || [];
const deleted = (response.transaction.votes && response.transaction.votes.deleted) || [];
Expand Down Expand Up @@ -147,3 +143,48 @@ export const loadTransaction = ({ activePeer, id }) =>
dispatch({ data: error, type: actionTypes.transactionLoadFailed });
});
};

export const transactionsUpdated = ({ activePeer, address, limit, filter, pendingTransactions }) =>
(dispatch) => {
getTransactions({ activePeer, address, limit, filter })
.then((response) => {
dispatch({
data: {
confirmed: response.transactions,
count: parseInt(response.count, 10),
},
type: actionTypes.transactionsUpdated,
});
if (pendingTransactions.length) {
dispatch(transactionsUpdateUnconfirmed({
activePeer,
address,
pendingTransactions,
}));
}
});
};

export const sent = ({ activePeer, account, recipientId, amount, passphrase, secondPassphrase }) =>
(dispatch) => {
send(activePeer, recipientId, toRawLsk(amount), passphrase, secondPassphrase)
.then((data) => {
dispatch({
data: {
id: data.transactionId,
senderPublicKey: account.publicKey,
senderId: account.address,
recipientId,
amount: toRawLsk(amount),
fee: Fees.send,
type: transactionTypes.send,
},
type: actionTypes.transactionAdded,
});
})
.catch((error) => {
const errorMessage = error && error.message ? `${error.message}.` : i18next.t('An error occurred while creating the transaction.');
dispatch({ data: { errorMessage }, type: actionTypes.transactionFailed });
});
dispatch(passphraseUsed(passphrase));
};
Loading

0 comments on commit ea6dca2

Please sign in to comment.