From addd83604d0470894c6a5c7b31ad6a45d04aef1a Mon Sep 17 00:00:00 2001 From: Piotr Gawron <p.gawron@atcomp.pl> Date: Fri, 24 May 2024 08:52:59 +0200 Subject: [PATCH] failed login should result in Invalid credentials error message --- src/redux/user/user.thunks.test.ts | 14 ++++++++++++++ src/redux/user/user.thunks.ts | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/redux/user/user.thunks.test.ts b/src/redux/user/user.thunks.test.ts index 982274d9..e0baefa9 100644 --- a/src/redux/user/user.thunks.test.ts +++ b/src/redux/user/user.thunks.test.ts @@ -5,12 +5,15 @@ import { ToolkitStoreWithSingleSlice, createStoreInstanceUsingSliceReducer, } from '@/utils/createStoreInstanceUsingSliceReducer'; +import { showToast } from '@/utils/showToast'; import { apiPath } from '../apiPath'; import { closeModal } from '../modal/modal.slice'; import userReducer from './user.slice'; import { UserState } from './user.types'; import { login } from './user.thunks'; +jest.mock('../../utils/showToast'); + const mockedAxiosClient = mockNetworkResponse(); const CREDENTIALS = { login: 'test', @@ -50,4 +53,15 @@ describe('login thunk', () => { await store.dispatch(login(CREDENTIALS)); }); + + it('dispatch showToast on failed login with invalid data', async () => { + mockedAxiosClient.onPost(apiPath.postLogin()).reply(HttpStatusCode.Unauthorized, loginFixture); + + await store.dispatch(login(CREDENTIALS)); + + expect(showToast).toHaveBeenCalledWith({ + message: 'Invalid credentials.', + type: 'error', + }); + }); }); diff --git a/src/redux/user/user.thunks.ts b/src/redux/user/user.thunks.ts index 19aa06df..6abb07c3 100644 --- a/src/redux/user/user.thunks.ts +++ b/src/redux/user/user.thunks.ts @@ -6,6 +6,8 @@ import { sessionSchemaValid } from '@/models/sessionValidSchema'; import { Login, SessionValid, User, UserPrivilege } from '@/types/models'; import { USER_ROLE } from '@/constants/user'; import { getError } from '@/utils/error-report/getError'; +import axios, { HttpStatusCode } from 'axios'; +import { showToast } from '@/utils/showToast'; import { apiPath } from '../apiPath'; import { closeModal, openLoggedInMenuModal } from '../modal/modal.slice'; import { hasPrivilege } from './user.utils'; @@ -60,6 +62,15 @@ export const login = createAsyncThunk( return undefined; } catch (error) { + if (axios.isAxiosError(error)) { + if (error?.response?.status === HttpStatusCode.Unauthorized) { + showToast({ + type: 'error', + message: 'Invalid credentials.', + }); + return undefined; + } + } return Promise.reject(getError({ error, prefix: 'Login' })); } }, -- GitLab