#246: fixed bug, when active account reset to the first one after page refresh.

removed authenticate call, when there are no strangers to logout on app startup.
This commit is contained in:
SleepWalker 2017-01-12 07:29:39 +02:00
parent f1110b0067
commit 85942d27ed
2 changed files with 51 additions and 19 deletions

View File

@ -111,10 +111,11 @@ export function logoutAll() {
*/ */
export function logoutStrangers() { export function logoutStrangers() {
return (dispatch, getState) => { return (dispatch, getState) => {
const {accounts: {available}} = getState(); const {accounts: {available, active}} = getState();
const isStranger = ({refreshToken, id}) => !refreshToken && !sessionStorage.getItem(`stranger${id}`); const isStranger = ({refreshToken, id}) => !refreshToken && !sessionStorage.getItem(`stranger${id}`);
if (available.some(isStranger)) {
const accountToReplace = available.filter((account) => !isStranger(account))[0]; const accountToReplace = available.filter((account) => !isStranger(account))[0];
if (accountToReplace) { if (accountToReplace) {
@ -124,10 +125,13 @@ export function logoutStrangers() {
authentication.logout(account); authentication.logout(account);
}); });
if (isStranger(active)) {
return dispatch(authenticate(accountToReplace)); return dispatch(authenticate(accountToReplace));
} }
} else {
dispatch(logoutAll()); return dispatch(logoutAll());
}
}
return Promise.resolve(); return Promise.resolve();
}; };

View File

@ -301,7 +301,7 @@ describe('components/accounts/actions', () => {
beforeEach(() => { beforeEach(() => {
getState.returns({ getState.returns({
accounts: { accounts: {
active: account, active: foreignAccount,
available: [account, foreignAccount, foreignAccount2] available: [account, foreignAccount, foreignAccount2]
}, },
user user
@ -343,6 +343,37 @@ describe('components/accounts/actions', () => {
) )
); );
it('should not activate another account if active account is already not a stranger', () => {
getState.returns({
accounts: {
active: account,
available: [account, foreignAccount]
},
user
});
return logoutStrangers()(dispatch, getState)
.then(() =>
expect(dispatch, 'was always called with',
expect.it('not to satisfy', activate(account)))
);
});
it('should not dispatch if no strangers', () => {
getState.returns({
accounts: {
active: account,
available: [account]
},
user
});
return logoutStrangers()(dispatch, getState)
.then(() =>
expect(dispatch, 'was not called')
);
});
describe('when all accounts are strangers', () => { describe('when all accounts are strangers', () => {
beforeEach(() => { beforeEach(() => {
getState.returns({ getState.returns({
@ -368,7 +399,7 @@ describe('components/accounts/actions', () => {
}); });
}); });
describe('when an stranger has a mark in sessionStorage', () => { describe('when a stranger has a mark in sessionStorage', () => {
const key = `stranger${foreignAccount.id}`; const key = `stranger${foreignAccount.id}`;
beforeEach(() => { beforeEach(() => {
@ -382,12 +413,9 @@ describe('components/accounts/actions', () => {
}); });
it('should not log out', () => it('should not log out', () =>
expect(dispatch, 'to have calls satisfying', [ expect(dispatch, 'was always called with',
[expect.it('not to equal', {payload: foreignAccount})], expect.it('not to equal', {payload: foreignAccount})
// for some reason it says, that dispatch(authenticate(...)) )
// must be removed if only one args assertion is listed :(
[expect.it('not to equal', {payload: foreignAccount})]
])
); );
}); });
}); });