Skip to content

Commit

Permalink
Merge pull request #515 from LiskHQ/157-imporove-middleware-test-cove…
Browse files Browse the repository at this point in the history
…rage

Improve middleware test coverage up to 100% - Closes #157
  • Loading branch information
michaeltomasik authored Mar 12, 2018
2 parents 66b05b0 + fed99c8 commit fa35b82
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 9 deletions.
5 changes: 0 additions & 5 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ module.exports = function (config) {
'src/components/setting/index.js',
'src/components/setting/setting.js',
'src/components/menuBar/menuBar.js',
'src/store/middlewares/account.js',
'src/store/middlewares/login.js',
'src/store/middlewares/peers.js',
'src/store/middlewares/savedAccounts.js',
'src/store/middlewares/socket.js',
'src/components/accountVisual/demo.js',
'src/components/delegateList/votingHeader.js',
'src/components/app/index.js',
Expand Down
49 changes: 48 additions & 1 deletion src/store/middlewares/account.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { spy, stub, useFakeTimers } from 'sinon';
import { spy, stub, useFakeTimers, match } from 'sinon';
import { accountUpdated } from '../../actions/account';
import accountConfig from '../../constants/account';
import { activePeerUpdate } from '../../actions/peers';
Expand Down Expand Up @@ -48,6 +48,14 @@ describe('Account middleware', () => {
},
};

const blockWithNullTransaction = {
type: actionTypes.newBlockCreated,
data: {
windowIsFocused: true,
block: { transactions: [null] },
},
};

let clock;

beforeEach(() => {
Expand Down Expand Up @@ -145,6 +153,45 @@ describe('Account middleware', () => {
expect(stubTransactions).to.have.been.calledWith();
});

it(`should call transactions API methods on ${actionTypes.newBlockCreated} action if block.transactions contains null element`, () => {
middleware(store)(next)(blockWithNullTransaction);

expect(store.dispatch).to.have.been.calledWith(match.has('type', actionTypes.transactionsUpdated));
});

it(`should call API methods on ${actionTypes.newBlockCreated} action if state.transaction.transactions.confired does not contain recent transaction. Case with transactions address`, () => {
stubGetAccount.resolves({ balance: 0 });

store.getState = () => ({ ...state,
transactions: {
...state.transactions,
confirmed: [{ confirmations: 10 }],
address: 'sample_address',
},
});

middleware(store)(next)(newBlockCreated);
expect(stubGetAccount).to.have.been.calledWith({});
expect(store.dispatch).to.have.been.calledWith(match.has('type', actionTypes.transactionsUpdated));
});

it(`should call API methods on ${actionTypes.newBlockCreated} action if state.transaction.transactions.confired does not contain recent transaction. Case with confirmed address`, () => {
stubGetAccount.resolves({ balance: 0 });

store.getState = () => ({ ...state,
transactions: {
pending: [{
id: 12498250891724098,
}],
confirmed: [{ confirmations: 10, address: 'sample_address' }],
},
});

middleware(store)(next)(newBlockCreated);
expect(stubGetAccount).to.have.been.calledWith({});
expect(store.dispatch).to.have.been.calledWith(match.has('type', actionTypes.transactionsUpdated));
});

it(`should fetch delegate info on ${actionTypes.newBlockCreated} action if account.balance changes and account.isDelegate`, () => {
const delegateApiMock = stub(delegateApi, 'getDelegate').returnsPromise().resolves({ success: true, delegate: {} });
stubGetAccount.resolves({ balance: 10e8 });
Expand Down
1 change: 1 addition & 0 deletions src/store/middlewares/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('Login middleware', () => {
data: {},
},
account: {},
settings: { autoLog: true },
});
store.dispatch = spy();

Expand Down
1 change: 1 addition & 0 deletions src/store/middlewares/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const peersMiddleware = store => next => (action) => {

switch (action.type) {
case actionTypes.storeCreated:
/* istanbul ignore else */
if (hasNoSavedAccounts) {
store.dispatch(activePeerSet({ network, noSavedAccounts: true }));
store.dispatch(activePeerUpdate({ online: true }));
Expand Down
1 change: 1 addition & 0 deletions src/store/middlewares/savedAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const savedAccountsMiddleware = (store) => {
if (savedAccounts && savedAccounts.lastActive) {
const account = savedAccounts.lastActive;
const network = Object.assign({}, getNetwork(account.network));

if (account.network === networks.customNode.code) {
network.address = account.address;
}
Expand Down
22 changes: 21 additions & 1 deletion src/store/middlewares/savedAccounts.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import { spy, mock, match } from 'sinon';

import { accountLoading } from '../../actions/account';
import { accountLoading, accountLoggedOut } from '../../actions/account';
import { accountSaved } from '../../actions/savedAccounts';
import * as peersActions from '../../actions/peers';
import actionTypes from '../../constants/actions';
Expand Down Expand Up @@ -37,6 +37,7 @@ describe('SavedAccounts middleware', () => {
account: {
balance,
publicKey,
network: null,
},
};
store.getState = () => state;
Expand Down Expand Up @@ -128,4 +129,23 @@ describe('SavedAccounts middleware', () => {
publicKey: publicKey2,
}));
});

it(`should dispatch accountRemoved action on ${actionTypes.accountLoggedOut} action if given account is logged in`, () => {
const publicKey2 = 'hab9d261ea050b9e326d7e11587eccc343a20e64e29d8781b50fd06683cacc88';
state.account = {
publicKey: publicKey2,
balance,
};

state.savedAccounts = {
accounts: [],
};

store.getState = () => state;
const action = {
type: actionTypes.accountRemoved,
};
middleware(store)(next)(action);
expect(store.dispatch).to.have.been.calledWith(accountLoggedOut());
});
});
3 changes: 3 additions & 0 deletions src/store/middlewares/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ const closeConnection = () => {
forcedClosing = true;
connection.close();
forcedClosing = false;
connection = undefined;
}
};

const socketSetup = (store) => {
let windowIsFocused = true;
const { ipc } = window;
/* istanbul ignore else */
if (ipc) {
ipc.on('blur', () => { windowIsFocused = false; });
ipc.on('focus', () => { windowIsFocused = true; });
Expand All @@ -36,6 +38,7 @@ const socketSetup = (store) => {
});
});
connection.on('disconnect', () => {
/* istanbul ignore else */
if (!forcedClosing) {
store.dispatch(activePeerUpdate({ online: false }));
}
Expand Down
14 changes: 14 additions & 0 deletions src/store/middlewares/socket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ describe('Socket middleware', () => {
expect(io.connect().close).to.have.been.calledWith();
expect(store.dispatch).to.not.have.been.calledWith(activePeerUpdate({ online: false }));
});
// it depends on actionTypes.accountLoggedOut in test above that sets connection to null
it('should not dispatch any action then there is no connection', () => {
middleware(store)(next)({ type: actionTypes.accountLoggedOut });
expect(io.connect().close).to.not.have.been.calledWith();
expect(store.dispatch).to.not.have.been.calledWith(activePeerUpdate({ online: false }));
});

it('should dispatch online event on reconnect', () => {
middleware(store)(next)({ type: actionTypes.accountLoggedIn });
socketCallbacks.reconnect();
expect(store.dispatch).to.have.been.calledWith(activePeerUpdate({ online: true }));
});

it(`should dispatch ${actionTypes.accountLoggedIn} with https protocol`, () => {
store.getState = () => ({ ...store,
peers: { data: { options: { ssl: true, address: 'localhost:4000' } } },
});
middleware(store)(next)({ type: actionTypes.accountLoggedIn });
expect(store.dispatch).to.not.have.been.calledWith(activePeerUpdate({ online: true }));
});

it('should dispatch offline event on disconnect', () => {
middleware(store)(next)({ type: actionTypes.accountLoggedIn });
socketCallbacks.disconnect();
Expand Down
18 changes: 16 additions & 2 deletions test/integration/accountSwitch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ describe('@integration: Account switch', () => {
publicKey: accounts.genesis.publicKey,
balance: accounts.genesis.balance,
}, {
network: networks.mainnet.code,
network: networks.customNode.code,
publicKey: accounts.delegate.publicKey,
address: 'http://localhost:8080',
balance: accounts.delegate.balance,
}, {
network: networks.mainnet.code,
Expand Down Expand Up @@ -77,7 +78,20 @@ describe('@integration: Account switch', () => {
helper = new GenericStepDefinition(wrapper, store);
};

describe('Scenario: should allow to remove a saved account', () => {
describe('Scenario: should allow to remove a saved account with lastActiveAccount on mainnet', () => {
step('Given I\'m on "account switcher" with accounts: "genesis,delegate,empty account"', setupStep);
step('Then I should see 3 instances of "saved account card"', () => helper.shouldSeeCountInstancesOf(3, '.saved-account-card'));
step('When I click "edit button"', () => helper.clickOnElement('button.edit-button'));
step('When I click "remove button"', () => helper.clickOnElement('button.remove-button'));
step('When I click "remove button"', () => helper.clickOnElement('button.remove-button'));
step('Then I should see 2 instances of "saved account card"', () => helper.shouldSeeCountInstancesOf(2, '.saved-account-card'));
});

describe('Scenario: should allow to remove a saved account with lastActiveAccount on customNode', () => {
beforeEach(() => {
localStorageStub.withArgs('lastActiveAccountIndex').returns(1);
});

step('Given I\'m on "account switcher" with accounts: "genesis,delegate,empty account"', setupStep);
step('Then I should see 3 instances of "saved account card"', () => helper.shouldSeeCountInstancesOf(3, '.saved-account-card'));
step('When I click "edit button"', () => helper.clickOnElement('button.edit-button'));
Expand Down

0 comments on commit fa35b82

Please sign in to comment.