#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,24 +111,28 @@ export function logoutAll() {
*/
export function logoutStrangers() {
return (dispatch, getState) => {
const {accounts: {available}} = getState();
const {accounts: {available, active}} = getState();
const isStranger = ({refreshToken, id}) => !refreshToken && !sessionStorage.getItem(`stranger${id}`);
const accountToReplace = available.filter((account) => !isStranger(account))[0];
if (available.some(isStranger)) {
const accountToReplace = available.filter((account) => !isStranger(account))[0];
if (accountToReplace) {
available.filter(isStranger)
.forEach((account) => {
dispatch(remove(account));
authentication.logout(account);
});
if (accountToReplace) {
available.filter(isStranger)
.forEach((account) => {
dispatch(remove(account));
authentication.logout(account);
});
return dispatch(authenticate(accountToReplace));
if (isStranger(active)) {
return dispatch(authenticate(accountToReplace));
}
} else {
return dispatch(logoutAll());
}
}
dispatch(logoutAll());
return Promise.resolve();
};
}

View File

@ -301,7 +301,7 @@ describe('components/accounts/actions', () => {
beforeEach(() => {
getState.returns({
accounts: {
active: account,
active: foreignAccount,
available: [account, foreignAccount, foreignAccount2]
},
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', () => {
beforeEach(() => {
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}`;
beforeEach(() => {
@ -382,12 +413,9 @@ describe('components/accounts/actions', () => {
});
it('should not log out', () =>
expect(dispatch, 'to have calls satisfying', [
[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})]
])
expect(dispatch, 'was always called with',
expect.it('not to equal', {payload: foreignAccount})
)
);
});
});