mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-29 16:30:54 +05:30
#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:
parent
f1110b0067
commit
85942d27ed
@ -111,24 +111,28 @@ 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}`);
|
||||||
|
|
||||||
const accountToReplace = available.filter((account) => !isStranger(account))[0];
|
if (available.some(isStranger)) {
|
||||||
|
const accountToReplace = available.filter((account) => !isStranger(account))[0];
|
||||||
|
|
||||||
if (accountToReplace) {
|
if (accountToReplace) {
|
||||||
available.filter(isStranger)
|
available.filter(isStranger)
|
||||||
.forEach((account) => {
|
.forEach((account) => {
|
||||||
dispatch(remove(account));
|
dispatch(remove(account));
|
||||||
authentication.logout(account);
|
authentication.logout(account);
|
||||||
});
|
});
|
||||||
|
|
||||||
return dispatch(authenticate(accountToReplace));
|
if (isStranger(active)) {
|
||||||
|
return dispatch(authenticate(accountToReplace));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return dispatch(logoutAll());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch(logoutAll());
|
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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})]
|
|
||||||
])
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user