diff --git a/src/ducks/Auth.test.js b/src/ducks/Auth.test.js index 9fe8dae1..a5ce9baa 100644 --- a/src/ducks/Auth.test.js +++ b/src/ducks/Auth.test.js @@ -1,4 +1,4 @@ -import { clearCurrentUser, currentUserShowRequest } from './user.duck'; +import { clearCurrentUser, currentUserShowRequest, currentUserShowSuccess } from './user.duck'; import reducer, { authenticationInProgress, authInfoSuccess, @@ -192,10 +192,10 @@ describe('Auth duck', () => { describe('login thunk', () => { it('should dispatch success and fetch current user', () => { + const initialState = reducer(); const getState = () => ({ Auth: initialState }); const sdk = { login: jest.fn(() => Promise.resolve({})) }; const dispatch = createFakeDispatch(getState, sdk); - const initialState = reducer(); const username = 'x.x@example.com'; const password = 'pass'; @@ -205,6 +205,11 @@ describe('Auth duck', () => { loginRequest(), loginSuccess(), currentUserShowRequest(), + + // Test restriction: Since the getState mock always returns + // the initial state, user isn't marked as logged in and + // current user is still null. + currentUserShowSuccess(null), ]); }); }); @@ -355,6 +360,11 @@ describe('Auth duck', () => { loginRequest(), loginSuccess(), currentUserShowRequest(), + + // Test restriction: Since the getState mock always returns + // the initial state, user isn't marked as logged in and + // current user is still null. + currentUserShowSuccess(null), ]); }); }); diff --git a/src/ducks/user.duck.js b/src/ducks/user.duck.js index 07ba1d8b..4644bd6e 100644 --- a/src/ducks/user.duck.js +++ b/src/ducks/user.duck.js @@ -1,3 +1,4 @@ +import { authInfo } from './Auth.duck'; import { updatedEntities, denormalisedEntities } from '../util/data'; import { TX_TRANSITION_PREAUTHORIZE } from '../util/propTypes'; @@ -323,7 +324,8 @@ export const fetchCurrentUser = () => const { isAuthenticated } = getState().Auth; if (!isAuthenticated) { - // Ignore when not logged in, current user should be null + // Make sure current user is null + dispatch(currentUserShowSuccess(null)); return Promise.resolve({}); } @@ -344,8 +346,14 @@ export const fetchCurrentUser = () => if (!currentUser.attributes.emailVerified) { dispatch(fetchCurrentUserHasOrders()); } + + // Make sure auth info is up to date + dispatch(authInfo()); }) .catch(e => { + // Make sure auth info is up to date + dispatch(authInfo()); + // TODO: dispatch flash message: "Something went wrong while retrieving user info" dispatch(currentUserShowError(e)); });