mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-11-17 21:53:03 +05:30
#48: reset accounts state on logout
This commit is contained in:
parent
b6b8468904
commit
9e7d5b8338
@ -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
|
||||
|
@ -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');
|
||||
|
@ -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();
|
||||
|
@ -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')]
|
||||
|
@ -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';
|
||||
|
@ -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()
|
||||
]);
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user