#48: add account sorting

This commit is contained in:
SleepWalker 2016-11-13 14:16:21 +02:00
parent 420ce65392
commit 81a5437be0
2 changed files with 22 additions and 1 deletions

View File

@ -30,6 +30,14 @@ export default function accounts(
.filter((account) => account.id !== payload.id)
.concat(payload);
state.available.sort((account1, account2) => {
if (account1.username === account2.username) {
return 0;
}
return account1.username > account2.username ? 1 : -1;
});
return state;
case ACTIVATE:

View File

@ -56,13 +56,26 @@ describe('Accounts reducer', () => {
token: 'newToken'
};
return expect(
expect(
accounts({...initial, available: [outdatedAccount]}, add(updatedAccount)),
'to satisfy', {
available: [updatedAccount]
});
});
it('should sort accounts by username', () => {
const newAccount = {
...account,
id: 2,
username: 'abc'
};
expect(accounts({...initial, available: [account]}, add(newAccount)),
'to satisfy', {
available: [newAccount, account]
});
});
it('throws, when account is invalid', () => {
expect(() => accounts(initial, add()),
'to throw', 'Invalid or empty payload passed for accounts.add');