Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Gina Contrino committed Oct 19, 2017
1 parent 154eae2 commit a9fc68e
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 323 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"redux": "3.7.2",
"redux-logger": "=3.0.6",
"redux-thunk": "^2.2.0",
"socket.io-client": "^2.0.3",
"webpack-merge": "^4.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/constants/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const actionTypes = {
metronomeBeat: 'METRONOME_BEAT',
relevantBlockAdded: 'RELEVANT_BLOCK_ADDED',
accountUpdated: 'ACCOUNT_UPDATED',
accountLoggedOut: 'ACCOUNT_LOGGED_OUT',
accountLoggedIn: 'ACCOUNT_LOGGED_IN',
Expand Down
2 changes: 1 addition & 1 deletion src/store/middlewares/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const passphraseUsed = (store, action) => {
const accountMiddleware = store => next => (action) => {
next(action);
switch (action.type) {
case actionTypes.metronomeBeat:
case actionTypes.relevantBlockAdded:
updateAccountData(store, action);
break;
case actionTypes.transactionsUpdated:
Expand Down
53 changes: 25 additions & 28 deletions src/store/middlewares/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SYNC_ACTIVE_INTERVAL, SYNC_INACTIVE_INTERVAL } from '../../constants/ap
import { accountUpdated } from '../../actions/account';
import { activePeerUpdate } from '../../actions/peers';
import * as votingActions from '../../actions/voting';
import * as forgingActions from '../../actions/forging';
import * as accountApi from '../../utils/api/account';
import actionTypes from '../../constants/actions';
import * as delegateApi from '../../utils/api/delegate';
Expand All @@ -29,15 +30,15 @@ describe('Account middleware', () => {
},
};

const activeBeatAction = {
type: actionTypes.metronomeBeat,
const relevantBlockAdded = {
type: actionTypes.relevantBlockAdded,
data: {
interval: SYNC_ACTIVE_INTERVAL,
},
};

const inactiveBeatAction = {
type: actionTypes.metronomeBeat,
const inactiveRelevantBlockAdded = {
type: actionTypes.relevantBlockAdded,
data: {
interval: SYNC_INACTIVE_INTERVAL,
},
Expand Down Expand Up @@ -82,77 +83,73 @@ describe('Account middleware', () => {
expect(next).to.have.been.calledWith(expectedAction);
});

it(`should call account API methods on ${actionTypes.metronomeBeat} action when online`, () => {
it(`should call account API methods on ${actionTypes.relevantBlockAdded} action when online`, () => {
// does this matter?
stubGetAccount.resolves({ balance: 0 });

middleware(store)(next)(activeBeatAction);
middleware(store)(next)(relevantBlockAdded);

expect(stubGetAccount).to.have.been.calledWith();
expect(store.dispatch).to.have.been.calledWith(activePeerUpdate({ online: true }));
});

it(`should call account API methods on ${actionTypes.metronomeBeat} action when offline`, () => {
it(`should call account API methods on ${actionTypes.relevantBlockAdded} action when offline`, () => {
const errorCode = 'EUNAVAILABLE';
stubGetAccount.rejects({ error: { code: errorCode } });

middleware(store)(next)(activeBeatAction);
middleware(store)(next)(relevantBlockAdded);

expect(store.dispatch).to.have.been.calledWith(activePeerUpdate(
{ online: false, code: errorCode }));
});

it(`should call transactions API methods on ${actionTypes.metronomeBeat} action if account.balance changes`, () => {
it(`should call transactions API methods on ${actionTypes.relevantBlockAdded} action if account.balance changes`, () => {
stubGetAccount.resolves({ balance: 10e8 });

middleware(store)(next)(activeBeatAction);
middleware(store)(next)(relevantBlockAdded);

expect(stubGetAccount).to.have.been.calledWith();
// TODO why next expect doesn't work despite it being called according to test coverage?
// expect(stubTransactions).to.have.been.calledWith();
expect(stubTransactions).to.have.been.calledWith();
});

it(`should call transactions API methods on ${actionTypes.metronomeBeat} action if account.balance changes and action.data.interval is SYNC_INACTIVE_INTERVAL`, () => {
it(`should call transactions API methods on ${actionTypes.relevantBlockAdded} action if account.balance changes and action.data.interval is SYNC_INACTIVE_INTERVAL`, () => {
stubGetAccount.resolves({ balance: 10e8 });

middleware(store)(next)(inactiveBeatAction);
middleware(store)(next)(inactiveRelevantBlockAdded);

expect(stubGetAccount).to.have.been.calledWith();
// TODO why next expect doesn't work despite it being called according to test coverage?
// expect(stubTransactions).to.have.been.calledWith();
expect(stubTransactions).to.have.been.calledWith();
});

it(`should call transactions API methods on ${actionTypes.metronomeBeat} action if action.data.interval is SYNC_ACTIVE_INTERVAL and there are recent transactions`, () => {
it(`should call transactions API methods on ${actionTypes.relevantBlockAdded} action if action.data.interval is SYNC_ACTIVE_INTERVAL and there are recent transactions`, () => {
stubGetAccount.resolves({ balance: 0 });

middleware(store)(next)(activeBeatAction);
middleware(store)(next)(relevantBlockAdded);

expect(stubGetAccount).to.have.been.calledWith();
// TODO why next expect doesn't work despite it being called according to test coverage?
// expect(stubTransactions).to.have.been.calledWith();
expect(stubTransactions).to.have.been.calledWith();
});

it(`should fetch delegate info on ${actionTypes.metronomeBeat} action if account.balance changes and account.isDelegate`, () => {
it(`should fetch delegate info on ${actionTypes.relevantBlockAdded} action if account.balance changes and account.isDelegate`, () => {
const delegateApiMock = stub(delegateApi, 'getDelegate').returnsPromise().resolves({ success: true, delegate: {} });
stubGetAccount.resolves({ balance: 10e8 });
state.account.isDelegate = true;
store.getState = () => (state);

middleware(store)(next)(activeBeatAction);
middleware(store)(next)(relevantBlockAdded);
expect(store.dispatch).to.have.been.calledWith();

delegateApiMock.restore();
});

it(`should call fetchAndUpdateForgedBlocks(...) on ${actionTypes.metronomeBeat} action if account.balance changes and account.isDelegate`, () => {
it(`should call fetchAndUpdateForgedBlocks(...) on ${actionTypes.relevantBlockAdded} action if account.balance changes and account.isDelegate`, () => {
state.account.isDelegate = true;
store.getState = () => (state);
stubGetAccount.resolves({ balance: 10e8 });
// const fetchAndUpdateForgedBlocksSpy = spy(forgingActions, 'fetchAndUpdateForgedBlocks');
const fetchAndUpdateForgedBlocksSpy = spy(forgingActions, 'fetchAndUpdateForgedBlocks');

middleware(store)(next)({ type: actionTypes.metronomeBeat });

// TODO why next expect doesn't work despite it being called according to test coverage?
// expect(fetchAndUpdateForgedBlocksSpy).to.have.been.calledWith();
middleware(store)(next)(relevantBlockAdded);
expect(fetchAndUpdateForgedBlocksSpy).to.have.been.calledWith();
});

it(`should fetch delegate info on ${actionTypes.transactionsUpdated} action if action.data.confirmed contains delegateRegistration transactions`, () => {
Expand Down
4 changes: 2 additions & 2 deletions src/store/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import thunk from 'redux-thunk';
import metronomeMiddleware from './metronome';
import accountMiddleware from './account';
import loginMiddleware from './login';
import transactionsMiddleware from './transactions';
Expand All @@ -8,12 +7,13 @@ import offlineMiddleware from './offline';
import notificationMiddleware from './notification';
import votingMiddleware from './voting';
import savedAccountsMiddleware from './savedAccounts';
import socketMiddleware from './socket';

export default [
thunk,
transactionsMiddleware,
loginMiddleware,
metronomeMiddleware,
socketMiddleware,
accountMiddleware,
loadingBarMiddleware,
offlineMiddleware,
Expand Down
20 changes: 0 additions & 20 deletions src/store/middlewares/metronome.js

This file was deleted.

45 changes: 0 additions & 45 deletions src/store/middlewares/metronome.test.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/store/middlewares/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import actionTypes from '../../constants/actions';
import { socketSetup } from '../../utils/socket';

const socketMiddleware = store => (
next => (action) => {
switch (action.type) {
case actionTypes.accountLoggedIn:
socketSetup(store, action);
break;
default: break;
}
next(action);
});

export default socketMiddleware;
33 changes: 33 additions & 0 deletions src/store/middlewares/socket.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from 'chai';
import { spy, stub } from 'sinon';
import middleware from './socket';
import * as socket from '../../utils/socket';
import actionTypes from '../../constants/actions';

describe('Socket middleware', () => {
let store;
let next;
let socketSetup;

beforeEach(() => {
next = spy();
store = stub();
});

it('should call socketSetup when metronome is initialized', () => {
socketSetup = stub(socket, 'socketSetup');

middleware(store)(next)({ type: actionTypes.accountLoggedIn });
expect(socketSetup).to.have.been.calledWith();
socketSetup.reset();
});

it('should passes the action to next middleware', () => {
const expectedAction = {
type: actionTypes.accountLoggedIn,
};
middleware(store)(next)(expectedAction);
expect(next).to.have.been.calledWith();
});
});

91 changes: 0 additions & 91 deletions src/utils/metronome.js

This file was deleted.

Loading

0 comments on commit a9fc68e

Please sign in to comment.