mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-03-02 06:02:48 +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';
|
export const UPDATE_TOKEN = 'accounts:updateToken';
|
||||||
/**
|
/**
|
||||||
* @param {string} token
|
* @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}
|
* @typedef {AccountsState}
|
||||||
@ -50,6 +50,9 @@ export default function accounts(
|
|||||||
active: payload
|
active: payload
|
||||||
};
|
};
|
||||||
|
|
||||||
|
case RESET:
|
||||||
|
return accounts(undefined, {});
|
||||||
|
|
||||||
case REMOVE:
|
case REMOVE:
|
||||||
if (!payload || !payload.id) {
|
if (!payload || !payload.id) {
|
||||||
throw new Error('Invalid or empty payload passed for accounts.remove');
|
throw new Error('Invalid or empty payload passed for accounts.remove');
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { routeActions } from 'react-router-redux';
|
import { routeActions } from 'react-router-redux';
|
||||||
|
|
||||||
import accounts from 'services/api/accounts';
|
import accounts from 'services/api/accounts';
|
||||||
|
import { reset as resetAccounts } from 'components/accounts/actions';
|
||||||
import authentication from 'services/api/authentication';
|
import authentication from 'services/api/authentication';
|
||||||
import { setLocale } from 'components/i18n/actions';
|
import { setLocale } from 'components/i18n/actions';
|
||||||
|
|
||||||
@ -64,6 +65,8 @@ export function logout() {
|
|||||||
isGuest: true
|
isGuest: true
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
dispatch(resetAccounts());
|
||||||
|
|
||||||
dispatch(routeActions.push('/login'));
|
dispatch(routeActions.push('/login'));
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -2,7 +2,14 @@ import expect from 'unexpected';
|
|||||||
|
|
||||||
import accounts from 'services/api/accounts';
|
import accounts from 'services/api/accounts';
|
||||||
import authentication from 'services/api/authentication';
|
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 { SET_LOCALE } from 'components/i18n/actions';
|
||||||
|
|
||||||
import { updateUser } from 'components/user/actions';
|
import { updateUser } from 'components/user/actions';
|
||||||
@ -152,6 +159,9 @@ describe('components/accounts/actions', () => {
|
|||||||
{payload: {isGuest: true}}
|
{payload: {isGuest: true}}
|
||||||
// updateUser({isGuest: true})
|
// updateUser({isGuest: true})
|
||||||
]);
|
]);
|
||||||
|
expect(dispatch, 'to have a call satisfying', [
|
||||||
|
reset()
|
||||||
|
]);
|
||||||
// expect(dispatch, 'to have calls satisfying', [
|
// expect(dispatch, 'to have calls satisfying', [
|
||||||
// [remove(account)],
|
// [remove(account)],
|
||||||
// [expect.it('to be a function')]
|
// [expect.it('to be a function')]
|
||||||
|
@ -2,8 +2,8 @@ import expect from 'unexpected';
|
|||||||
|
|
||||||
import accounts from 'components/accounts/reducer';
|
import accounts from 'components/accounts/reducer';
|
||||||
import {
|
import {
|
||||||
updateToken, add, remove, activate,
|
updateToken, add, remove, activate, reset,
|
||||||
ADD, REMOVE, ACTIVATE, UPDATE_TOKEN
|
ADD, REMOVE, ACTIVATE, UPDATE_TOKEN, RESET
|
||||||
} from 'components/accounts/actions';
|
} from 'components/accounts/actions';
|
||||||
|
|
||||||
const account = {
|
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, () => {
|
describe(UPDATE_TOKEN, () => {
|
||||||
it('should update token', () => {
|
it('should update token', () => {
|
||||||
const newToken = 'newToken';
|
const newToken = 'newToken';
|
||||||
|
@ -3,6 +3,7 @@ import expect from 'unexpected';
|
|||||||
import { routeActions } from 'react-router-redux';
|
import { routeActions } from 'react-router-redux';
|
||||||
|
|
||||||
import request from 'services/request';
|
import request from 'services/request';
|
||||||
|
import { reset, RESET } from 'components/accounts/actions';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
logout,
|
logout,
|
||||||
@ -70,6 +71,7 @@ describe('components/user/actions', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
testChangedToGuest();
|
testChangedToGuest();
|
||||||
|
testAccountsReset();
|
||||||
testRedirectedToLogin();
|
testRedirectedToLogin();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -89,6 +91,7 @@ describe('components/user/actions', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
testChangedToGuest();
|
testChangedToGuest();
|
||||||
|
testAccountsReset();
|
||||||
testRedirectedToLogin();
|
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…
x
Reference in New Issue
Block a user