From 6d34f98f8e71b532c6b131fe0f2f89b35a78fca9 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <p.gawron@atcomp.pl> Date: Wed, 15 May 2024 14:29:43 +0200 Subject: [PATCH] reject redux with error --- src/redux/overlays/overlays.reducers.test.ts | 7 ++++--- src/redux/overlays/overlays.thunks.ts | 7 +++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/redux/overlays/overlays.reducers.test.ts b/src/redux/overlays/overlays.reducers.test.ts index 38b4652f..1c4cada3 100644 --- a/src/redux/overlays/overlays.reducers.test.ts +++ b/src/redux/overlays/overlays.reducers.test.ts @@ -14,6 +14,7 @@ import { import { mockNetworkResponse } from '@/utils/mockNetworkResponse'; import { HttpStatusCode } from 'axios'; import { waitFor } from '@testing-library/react'; +import { unwrapResult } from '@reduxjs/toolkit'; import { apiPath } from '../apiPath'; import overlaysReducer from './overlays.slice'; import { @@ -80,11 +81,11 @@ describe('overlays reducer', () => { .onGet(apiPath.getAllOverlaysByProjectIdQuery(PROJECT_ID, { publicOverlay: true })) .reply(HttpStatusCode.NotFound, []); - const { type, payload } = await store.dispatch(getAllPublicOverlaysByProjectId(PROJECT_ID)); + const action = await store.dispatch(getAllPublicOverlaysByProjectId(PROJECT_ID)); const { data, loading, error } = store.getState().overlays; - expect(type).toBe('overlays/getAllPublicOverlaysByProjectId/rejected'); - expect(payload).toBe( + expect(action.type).toBe('overlays/getAllPublicOverlaysByProjectId/rejected'); + expect(() => unwrapResult(action)).toThrow( "Failed to fetch overlays: The page you're looking for doesn't exist. Please verify the URL and try again.", ); expect(loading).toEqual('failed'); diff --git a/src/redux/overlays/overlays.thunks.ts b/src/redux/overlays/overlays.thunks.ts index 67fd7b7d..e410b64f 100644 --- a/src/redux/overlays/overlays.thunks.ts +++ b/src/redux/overlays/overlays.thunks.ts @@ -15,6 +15,7 @@ import { showToast } from '@/utils/showToast'; import { getErrorMessage } from '@/utils/getErrorMessage'; import { ThunkConfig } from '@/types/store'; import { BASE_API_URL } from '@/constants'; +import { getError } from '@/utils/error-report/getError'; import { apiPath } from '../apiPath'; import { CHUNK_SIZE, @@ -32,7 +33,7 @@ import type { RootState } from '../store'; export const getAllPublicOverlaysByProjectId = createAsyncThunk<MapOverlay[], string, ThunkConfig>( 'overlays/getAllPublicOverlaysByProjectId', - async (projectId: string, { rejectWithValue }) => { + async (projectId: string) => { try { const response = await axiosInstance.get<MapOverlay[]>( apiPath.getAllOverlaysByProjectIdQuery(projectId, { publicOverlay: true }), @@ -42,9 +43,7 @@ export const getAllPublicOverlaysByProjectId = createAsyncThunk<MapOverlay[], st return isDataValid ? response.data : []; } catch (error) { - const errorMessage = getErrorMessage({ error, prefix: OVERLAYS_FETCHING_ERROR_PREFIX }); - - return rejectWithValue(errorMessage); + return Promise.reject(getError({ error, prefix: OVERLAYS_FETCHING_ERROR_PREFIX })); } }, ); -- GitLab