Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/utils/__tests__/actions.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {authErrorHandler} from "../actions";
import {authErrorHandler, snackbarErrorHandler, SET_SNACKBAR_MESSAGE} from "../actions";
import { CLEAR_SESSION_STATE } from "../../components/security/actions";
import configureMockStore from 'redux-mock-store';
import request from 'superagent/lib/client';
import Swal from 'sweetalert2';
Expand Down Expand Up @@ -85,4 +86,24 @@ describe('Utils Actions', () => {
expect(Swal.fire).toBeCalled();
});
});

describe('snackbarErrorHandler', () => {
test('401 shows the snackbar and still runs the re-login', () => {
windowSpy.mockImplementation(() => ({
location: {
href: 'https://example.com',
pathname:'/',
replace: () => {}
},
localStorage: localStorageMock
}));

store.dispatch(snackbarErrorHandler({ status:401 }));

const types = store.getActions().map((a) => a.type);
expect(types).toContain(SET_SNACKBAR_MESSAGE);
// CLEAR_SESSION_STATE is only dispatched by initLogin
expect(types).toContain(CLEAR_SESSION_STATE);
});
});
});
8 changes: 7 additions & 1 deletion src/utils/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ export const setSnackbarMessage = (message) => (dispatch) => {
dispatch(createAction(SET_SNACKBAR_MESSAGE)(message));
};

// setSnackbarMessage is message-only, so run authErrorHandler's 401 re-login / 403 logout callback here
const notifySnackbarError = (message, callback) => (dispatch) => {
setSnackbarMessage(message)(dispatch);
if (typeof callback === "function") callback();
};

export const snackbarErrorHandler = (err, res) => (dispatch, state) => {
authErrorHandler(err, res, setSnackbarMessage)(dispatch, state);
authErrorHandler(err, res, notifySnackbarError)(dispatch, state);
};

export const snackbarSuccessHandler = (message) => (dispatch, state) =>
Expand Down
Loading