Skip to content

Commit

Permalink
Merge pull request #501 from LiskHQ/496-Increase-unit-test-coverage-r…
Browse files Browse the repository at this point in the history
…educers

Increase unit test coverage of reducers - Closes #496
  • Loading branch information
faival committed Mar 6, 2018
2 parents 2889895 + cc6f81b commit 1ad4180
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 3 deletions.
3 changes: 0 additions & 3 deletions karma.conf.js
Expand Up @@ -58,9 +58,6 @@ module.exports = function (config) {
'src/components/setting/index.js',
'src/components/setting/setting.js',
'src/components/menuBar/menuBar.js',
'src/store/reducers/voting.js',
'src/store/reducers/transactions.js',
'src/store/reducers/savedAccounts.js',
'src/store/middlewares/account.js',
'src/store/middlewares/login.js',
'src/store/middlewares/peers.js',
Expand Down
38 changes: 38 additions & 0 deletions src/store/reducers/savedAccounts.test.js
Expand Up @@ -29,6 +29,28 @@ describe('Reducer: savedAccounts(state, action)', () => {
expect(changedState).to.deep.equal(action.data);
});

it('should update corresponding account phassprasse when saving account', () => {
const state = { accounts: [account, account2], lastActive: [] };
const action = {
type: actionTypes.accountSaved,
data: {
...account,
passphrase: accounts.genesis.passphrase,
balance: 0,
},
};
const accountUpdatedWithPassphrase = { ...state };
accountUpdatedWithPassphrase.accounts[0] = {
...accountUpdatedWithPassphrase.accounts[0],
passphrase: accounts.genesis.passphrase,
balance: 0,
};
accountUpdatedWithPassphrase.lastActive = accountUpdatedWithPassphrase.accounts[0];

const changedState = savedAccounts(state, action);
expect(changedState).to.deep.equal(accountUpdatedWithPassphrase);
});

it('should return action.data with address if action.type = actionTypes.accountSaved', () => {
const state = { accounts: [] };
const action = {
Expand Down Expand Up @@ -59,5 +81,21 @@ describe('Reducer: savedAccounts(state, action)', () => {
delete account2WithoutPassphrase.passphrase;
expect(changedState).to.deep.equal({ accounts: [account, account2WithoutPassphrase] });
});

it('should update corresponding account passprasse when a passphrase is used', () => {
const state = {
accounts: [account, account2],
lastActive: account2,
};
const action = {
type: actionTypes.passphraseUsed,
data: accounts.genesis.passphrase,
};
const accountsUpdated = savedAccounts(state, action);
const accountPreserved = accountsUpdated.accounts[0];
const accountsWithPassphraseUpdated = accountsUpdated.accounts[1];
expect(accountPreserved).to.deep.equal(account);
expect(accountsWithPassphraseUpdated.passphrase).to.deep.equal(accounts.genesis.passphrase);
});
});

16 changes: 16 additions & 0 deletions src/store/reducers/transactions.test.js
Expand Up @@ -49,6 +49,22 @@ describe('Reducer: transactions(state, action)', () => {
expect(changedState).to.deep.equal({ ...state, failed: { errorMessage } });
});

it('should filter out failed transactions from pending', () => {
const state = {
...defaultState,
pending: [mockTransactions[1]],
};
const data = {
failed: [mockTransactions[1]],
};
const action = {
data,
type: actionTypes.transactionsFailed,
};
const pendingTransactionsFiltered = transactions(state, action);
const stateWithNoPendingTransactions = { ...defaultState };
expect(pendingTransactionsFiltered).to.deep.equal(stateWithNoPendingTransactions);
});

it('should concat action.data to state.confirmed if action.type = actionTypes.transactionsLoaded', () => {
const state = { ...defaultState };
Expand Down
119 changes: 119 additions & 0 deletions src/store/reducers/voting.test.js
Expand Up @@ -4,6 +4,12 @@ import voting from './voting';

describe('Reducer: voting(state, action)', () => {
const initialState = { votes: {}, delegates: [], refresh: true };
const defaultVoteUserData = {
confirmed: true,
unconfirmed: true,
pending: false,
publicKey: 'sample_key',
};
const cleanVotes = {
username1: { confirmed: false, unconfirmed: false, publicKey: 'sample_key' },
username2: { confirmed: true, unconfirmed: true, publicKey: 'sample_key' },
Expand All @@ -19,6 +25,7 @@ describe('Reducer: voting(state, action)', () => {
username2: { confirmed: true, unconfirmed: true, pending: false, publicKey: 'sample_key' },
username3: { confirmed: false, unconfirmed: false, pending: false, publicKey: 'sample_key' },
};

const restoredVotes = {
username1: { confirmed: false, unconfirmed: false, pending: false, publicKey: 'sample_key' },
username2: { confirmed: true, unconfirmed: true, pending: false, publicKey: 'sample_key' },
Expand Down Expand Up @@ -182,6 +189,118 @@ describe('Reducer: voting(state, action)', () => {
expect(changedState).to.be.deep.equal(expectedState);
});

it('should update new username in votes when we\'ve voted but it\'s not in the new list', () => {
const action = {
type: actionTypes.votesUpdated,
data: {
list: [{ username: 'username5', publicKey: 'sample_key' }],
},
};
const votedButNotYetInList = {
username1: { confirmed: true, unconfirmed: true, pending: true, publicKey: 'sample_key' },
};
const state = {
votes: { ...votedButNotYetInList },
};
const newUserNameRegisteredInVotes = {
votes: {
...votedButNotYetInList,
username5: { ...defaultVoteUserData },
},
refresh: false,
};
const saveNewUserInVotes = voting(state, action);
expect(saveNewUserInVotes).to.be.deep.equal(newUserNameRegisteredInVotes);
});

it('should not change votes, when we\'ve un-voted but user still exists in the new list', () => {
const updateVotesWithExistingUsernameAction = {
type: actionTypes.votesUpdated,
data: {
list: [{ username: 'username1', publicKey: 'sample_key' }],
},
};
const updateVotesUnvotedWithExistingUsername = {
username1: { confirmed: true, unconfirmed: false, pending: true, publicKey: 'sample_key' },
};
const state = {
votes: { ...updateVotesUnvotedWithExistingUsername },
};
const notChangedVotesRecords = {
votes: { ...updateVotesUnvotedWithExistingUsername },
refresh: false,
};

const changedState = voting(state, updateVotesWithExistingUsernameAction);
expect(changedState).to.be.deep.equal(notChangedVotesRecords);
});

it('should add new record of username in votes, when dirty and not voted for and username not yet in the new list', () => {
const action = {
type: actionTypes.votesUpdated,
data: {
list: [{ username: 'username5', publicKey: 'sample_key' }],
},
};
const updateVotesDirtyNotVotedNotExistingUsername = {
username1: { confirmed: true, unconfirmed: false, pending: false, publicKey: 'sample_key' },
};
const state = {
votes: { ...updateVotesDirtyNotVotedNotExistingUsername },
};
const newUsernameAddedToVotes = {
votes: {
...updateVotesDirtyNotVotedNotExistingUsername,
username5: { ...defaultVoteUserData },
},
refresh: false,
};
const changedState = voting(state, action);
expect(changedState).to.be.deep.equal(newUsernameAddedToVotes);
});

it('should keep record of username in votes, when dirty and not voted for and username is already in the new list', () => {
const action = {
type: actionTypes.votesUpdated,
data: {
list: [{ username: 'username1', publicKey: 'sample_key' }],
},
};
const updateVotesDirtyNotVotedExistingUsername = {
username1: { confirmed: true, unconfirmed: false, pending: false, publicKey: 'sample_key' },
};
const state = {
votes: { ...updateVotesDirtyNotVotedExistingUsername },
};
const votesRecordsUnchanged = {
votes: { ...updateVotesDirtyNotVotedExistingUsername },
refresh: false,
};
const changedState = voting(state, action);
expect(changedState).to.be.deep.equal(votesRecordsUnchanged);
});

it('should set default (confirmed, unconfirmed, pending) values on username vote records, when non of previous cases are met', () => {
const action = {
type: actionTypes.votesUpdated,
data: {
list: [{ username: 'username1', publicKey: 'sample_key' }],
},
};
const updateVotesNonConditionsMet = {
username1: { confirmed: true, unconfirmed: true, pending: true, publicKey: 'sample_key' },
};
const state = {
votes: { ...updateVotesNonConditionsMet },
};
const votesRecordsWithDefaultFlags = {
votes: { username1: { ...defaultVoteUserData } },
refresh: false,
};
const changedState = voting(state, action);
expect(changedState).to.be.deep.equal(votesRecordsWithDefaultFlags);
});

it('should set voteLookupStatus of given username to given status, with action: voteLookupStatusUpdated', () => {
const action = {
type: actionTypes.voteLookupStatusUpdated,
Expand Down

0 comments on commit 1ad4180

Please sign in to comment.