This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into 531-unit-test-coverage
- Loading branch information
Showing
12 changed files
with
170 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { getAccountStatus, getAccount, transactions } from '../../utils/api/account'; | ||
import { accountUpdated } from '../../actions/account'; | ||
import { transactionsUpdated } from '../../actions/transactions'; | ||
import { activePeerUpdate } from '../../actions/peers'; | ||
import actionsType from '../../constants/actions'; | ||
|
||
const updateAccountData = next => (store) => { // eslint-disable-line | ||
const { peers, account } = store.getState(); | ||
// TODO remove if statement when the beat event wiil be launched after loged in | ||
if (peers.data && account) { | ||
getAccount(peers.data, account.address).then((result) => { | ||
if (result.balance !== account.balance) { | ||
const maxBlockSize = 25; | ||
transactions(peers.data, account.address, maxBlockSize) | ||
.then(res => next(transactionsUpdated(res.transactions))); | ||
} | ||
next(accountUpdated(result)); | ||
}); | ||
return getAccountStatus(peers.data).then(() => { | ||
next(activePeerUpdate({ online: true })); | ||
}).catch(() => { | ||
next(activePeerUpdate({ online: false })); | ||
}); | ||
} | ||
}; | ||
|
||
const accountMiddleware = store => next => (action) => { | ||
next(action); | ||
const update = updateAccountData(next); | ||
switch (action.type) { | ||
case actionsType.metronomeBeat: | ||
update(store); | ||
break; | ||
default: break; | ||
} | ||
}; | ||
|
||
export default accountMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { expect } from 'chai'; | ||
import { spy, stub } from 'sinon'; | ||
import middleware from './account'; | ||
import * as accountApi from '../../utils/api/account'; | ||
import actionType from '../../constants/actions'; | ||
|
||
describe('Account middleware', () => { | ||
let store; | ||
let next; | ||
|
||
beforeEach(() => { | ||
store = stub(); | ||
store.getState = () => ({ | ||
peers: { | ||
data: {}, | ||
}, | ||
account: {}, | ||
}); | ||
next = spy(); | ||
}); | ||
|
||
it('should passes the action to next middleware', () => { | ||
const expectedAction = { | ||
type: 'TEST_ACTION', | ||
}; | ||
|
||
middleware(store)(next)(expectedAction); | ||
expect(next).to.have.been.calledWith(expectedAction); | ||
}); | ||
|
||
it(`should call account API methods on ${actionType.metronomeBeat} action`, () => { | ||
const stubGetAccount = stub(accountApi, 'getAccount').resolves(true); | ||
const stubGetAccountStatus = stub(accountApi, 'getAccountStatus').resolves(true); | ||
|
||
middleware(store)(next)({ type: actionType.metronomeBeat }); | ||
|
||
expect(stubGetAccount).to.have.been.calledWith(); | ||
expect(stubGetAccountStatus).to.have.been.calledWith(); | ||
|
||
stubGetAccount.restore(); | ||
stubGetAccountStatus.restore(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import metronomeMiddleware from './metronome'; | ||
import accountMiddleware from './account'; | ||
|
||
export default [ | ||
metronomeMiddleware, | ||
accountMiddleware, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import MetronomeService from '../../utils/metronome'; | ||
import actionTypes from '../../constants/actions'; | ||
|
||
const metronomeMiddleware = (store) => { | ||
const metronome = new MetronomeService(store.dispatch); | ||
// TODO: call metronome.init on login success event | ||
metronome.init(); | ||
return next => (action) => { | ||
switch (action.type) { | ||
case actionTypes.accountLoggedOut: | ||
metronome.terminate(); | ||
break; | ||
default: break; | ||
} | ||
next(action); | ||
}; | ||
}; | ||
|
||
export default metronomeMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { expect } from 'chai'; | ||
import { spy, stub } from 'sinon'; | ||
import middleware from './metronome'; | ||
import actionTypes from '../../constants/actions'; | ||
import * as MetronomeService from '../../utils/metronome'; | ||
|
||
describe('Metronome middleware', () => { | ||
let store; | ||
let next; | ||
|
||
beforeEach(() => { | ||
next = spy(); | ||
store = stub(); | ||
store.dispatch = spy(); | ||
}); | ||
|
||
it('should call Metronome constructor', () => { | ||
const metronome = spy(MetronomeService, 'default'); | ||
middleware(store); | ||
expect(metronome.calledWithNew()).to.equal(true); | ||
expect(metronome).to.have.been.calledWith(store.dispatch); | ||
metronome.restore(); | ||
}); | ||
|
||
it('should call Metronome init method', () => { | ||
const spyFn = spy(MetronomeService.default.prototype, 'init'); | ||
middleware(store); | ||
expect(spyFn).to.have.been.calledWith(); | ||
}); | ||
|
||
it(`should call metronome.terminate on ${actionTypes.accountLoggedOut} action`, () => { | ||
const spyFn = spy(MetronomeService.default.prototype, 'terminate'); | ||
middleware(store)(next)({ type: actionTypes.accountLoggedOut }); | ||
expect(spyFn).to.have.been.calledWith(); | ||
}); | ||
|
||
it('should passes the action to next middleware', () => { | ||
const expectedAction = { | ||
type: 'TEST_ACTION', | ||
}; | ||
middleware(store)(next)(expectedAction); | ||
expect(next).to.have.been.calledWith(expectedAction); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters