From 81a5437be0d4132411b9b994d3571a009a497e91 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Sun, 13 Nov 2016 14:16:21 +0200 Subject: [PATCH] #48: add account sorting --- src/components/accounts/reducer.js | 8 ++++++++ tests/components/accounts/reducer.test.js | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/components/accounts/reducer.js b/src/components/accounts/reducer.js index 1eb225a..2b69268 100644 --- a/src/components/accounts/reducer.js +++ b/src/components/accounts/reducer.js @@ -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: diff --git a/tests/components/accounts/reducer.test.js b/tests/components/accounts/reducer.test.js index 3769870..4729aa6 100644 --- a/tests/components/accounts/reducer.test.js +++ b/tests/components/accounts/reducer.test.js @@ -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');