From b6f87fc988b3769402f5f16d774b0467a7ea2013 Mon Sep 17 00:00:00 2001 From: Santiago Palenque Date: Fri, 18 Sep 2026 16:09:00 -0300 Subject: [PATCH] chore: call callback --- src/utils/__tests__/actions.test.js | 23 ++++++++++++++++++++++- src/utils/actions.js | 8 +++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/utils/__tests__/actions.test.js b/src/utils/__tests__/actions.test.js index 1362b5c6..da7143bd 100644 --- a/src/utils/__tests__/actions.test.js +++ b/src/utils/__tests__/actions.test.js @@ -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'; @@ -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); + }); + }); }); diff --git a/src/utils/actions.js b/src/utils/actions.js index 72535d78..6ce0dac4 100644 --- a/src/utils/actions.js +++ b/src/utils/actions.js @@ -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) =>