From 9e7d5b83389c8e8f56945363287033ef4e566817 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Mon, 14 Nov 2016 07:28:25 +0200 Subject: [PATCH] #48: reset accounts state on logout --- src/components/accounts/actions.js | 10 ++++++++++ src/components/accounts/reducer.js | 5 ++++- src/components/user/actions.js | 3 +++ tests/components/accounts/actions.test.js | 12 +++++++++++- tests/components/accounts/reducer.test.js | 11 +++++++++-- tests/components/user/actions.test.js | 13 +++++++++++++ 6 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/components/accounts/actions.js b/src/components/accounts/actions.js index 7bc297c..59f7fa9 100644 --- a/src/components/accounts/actions.js +++ b/src/components/accounts/actions.js @@ -111,6 +111,16 @@ export function activate(account) { }; } +export const RESET = 'accounts:reset'; +/** + * @return {object} - action definition + */ +export function reset() { + return { + type: RESET + }; +} + export const UPDATE_TOKEN = 'accounts:updateToken'; /** * @param {string} token diff --git a/src/components/accounts/reducer.js b/src/components/accounts/reducer.js index 2b69268..36d6eb4 100644 --- a/src/components/accounts/reducer.js +++ b/src/components/accounts/reducer.js @@ -1,4 +1,4 @@ -import { ADD, REMOVE, ACTIVATE, UPDATE_TOKEN } from './actions'; +import { ADD, REMOVE, ACTIVATE, RESET, UPDATE_TOKEN } from './actions'; /** * @typedef {AccountsState} @@ -50,6 +50,9 @@ export default function accounts( active: payload }; + case RESET: + return accounts(undefined, {}); + case REMOVE: if (!payload || !payload.id) { throw new Error('Invalid or empty payload passed for accounts.remove'); diff --git a/src/components/user/actions.js b/src/components/user/actions.js index 713233f..a19f6b3 100644 --- a/src/components/user/actions.js +++ b/src/components/user/actions.js @@ -1,6 +1,7 @@ import { routeActions } from 'react-router-redux'; import accounts from 'services/api/accounts'; +import { reset as resetAccounts } from 'components/accounts/actions'; import authentication from 'services/api/authentication'; import { setLocale } from 'components/i18n/actions'; @@ -64,6 +65,8 @@ export function logout() { isGuest: true })); + dispatch(resetAccounts()); + dispatch(routeActions.push('/login')); resolve(); diff --git a/tests/components/accounts/actions.test.js b/tests/components/accounts/actions.test.js index fffab50..3c48e56 100644 --- a/tests/components/accounts/actions.test.js +++ b/tests/components/accounts/actions.test.js @@ -2,7 +2,14 @@ import expect from 'unexpected'; import accounts from 'services/api/accounts'; import authentication from 'services/api/authentication'; -import { authenticate, revoke, add, activate, remove, ADD, REMOVE, ACTIVATE } from 'components/accounts/actions'; +import { + authenticate, + revoke, + add, ADD, + activate, ACTIVATE, + remove, + reset +} from 'components/accounts/actions'; import { SET_LOCALE } from 'components/i18n/actions'; import { updateUser } from 'components/user/actions'; @@ -152,6 +159,9 @@ describe('components/accounts/actions', () => { {payload: {isGuest: true}} // updateUser({isGuest: true}) ]); + expect(dispatch, 'to have a call satisfying', [ + reset() + ]); // expect(dispatch, 'to have calls satisfying', [ // [remove(account)], // [expect.it('to be a function')] diff --git a/tests/components/accounts/reducer.test.js b/tests/components/accounts/reducer.test.js index 4729aa6..140766e 100644 --- a/tests/components/accounts/reducer.test.js +++ b/tests/components/accounts/reducer.test.js @@ -2,8 +2,8 @@ import expect from 'unexpected'; import accounts from 'components/accounts/reducer'; import { - updateToken, add, remove, activate, - ADD, REMOVE, ACTIVATE, UPDATE_TOKEN + updateToken, add, remove, activate, reset, + ADD, REMOVE, ACTIVATE, UPDATE_TOKEN, RESET } from 'components/accounts/actions'; const account = { @@ -94,6 +94,13 @@ describe('Accounts reducer', () => { }); }); + describe(RESET, () => { + it('should reset accounts state', () => + expect(accounts({...initial, available: [account]}, reset()), + 'to equal', initial) + ); + }); + describe(UPDATE_TOKEN, () => { it('should update token', () => { const newToken = 'newToken'; diff --git a/tests/components/user/actions.test.js b/tests/components/user/actions.test.js index 0b05cb0..450cefa 100644 --- a/tests/components/user/actions.test.js +++ b/tests/components/user/actions.test.js @@ -3,6 +3,7 @@ import expect from 'unexpected'; import { routeActions } from 'react-router-redux'; import request from 'services/request'; +import { reset, RESET } from 'components/accounts/actions'; import { logout, @@ -70,6 +71,7 @@ describe('components/user/actions', () => { }); testChangedToGuest(); + testAccountsReset(); testRedirectedToLogin(); }); @@ -89,6 +91,7 @@ describe('components/user/actions', () => { ); testChangedToGuest(); + testAccountsReset(); testRedirectedToLogin(); }); @@ -114,5 +117,15 @@ describe('components/user/actions', () => { }) ); } + + function testAccountsReset() { + it(`should dispatch ${RESET}`, () => + callThunk(logout).then(() => { + expect(dispatch, 'to have a call satisfying', [ + reset() + ]); + }) + ); + } }); });