From 3a00598a765ed16559eb2dffd37fb2cd642354be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadeusz=20Miesi=C4=85c?= <tadeusz.miesiac@gmail.com> Date: Tue, 3 Oct 2023 04:28:10 +0400 Subject: [PATCH] test(drugs reducers): added missing test for loading case --- src/redux/drugs/drugs.reducers.test.ts | 52 ++++++++++++-------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/redux/drugs/drugs.reducers.test.ts b/src/redux/drugs/drugs.reducers.test.ts index 73053159..d62b3a1e 100644 --- a/src/redux/drugs/drugs.reducers.test.ts +++ b/src/redux/drugs/drugs.reducers.test.ts @@ -1,9 +1,11 @@ -import { AnyAction, ThunkMiddleware } from '@reduxjs/toolkit'; -import { ToolkitStore, configureStore } from '@reduxjs/toolkit/dist/configureStore'; import { PROJECT_ID } from '@/constants/mapId'; import { drugsFixture } from '@/models/fixtures/drugFixtures'; import { HTTP_NOT_FOUND, HTTP_OK } from '@/constants/httpResponses'; import { mockNetworkResponse } from '@/utils/mockNetworkResponse'; +import { + ToolkitStoreWithSingleSlice, + createStoreInstanceUsingSliceReducer, +} from '@/utils/createStoreInstanceUsingSliceReducer'; import { getDrugs } from './drugs.thunks'; import drugsReducer from './drugs.slice'; import { DrugsState } from './drugs.types'; @@ -17,33 +19,10 @@ const INITIAL_STATE: DrugsState = { error: { name: '', message: '' }, }; -type SliceReducerType = ToolkitStore< - { - drugs: DrugsState; - }, - AnyAction, - [ - ThunkMiddleware< - { - drugs: DrugsState; - }, - AnyAction - >, - ] ->; - -const createStoreInstanceUsingSliceReducer = (): SliceReducerType => - configureStore({ - reducer: { - drugs: drugsReducer, - }, - }); - describe('drugs reducer', () => { - let store = {} as SliceReducerType; - + let store = {} as ToolkitStoreWithSingleSlice<DrugsState>; beforeEach(() => { - store = createStoreInstanceUsingSliceReducer(); + store = createStoreInstanceUsingSliceReducer('drugs', drugsReducer); }); it('should match initial state', () => { @@ -79,7 +58,22 @@ describe('drugs reducer', () => { expect(data).toEqual([]); }); - it.skip('should update store on loading getDrugs query', () => { - // TODO + it('should update store on loading getDrugs query', async () => { + mockedAxiosClient + .onGet(`projects/${PROJECT_ID}/drugs:search?query=${SEARCH_QUERY}`) + .reply(HTTP_OK, drugsFixture); + + const drugsPromise = store.dispatch(getDrugs(SEARCH_QUERY)); + + const { data, loading } = store.getState().drugs; + expect(data).toEqual([]); + expect(loading).toEqual('pending'); + + drugsPromise.then(() => { + const { data: dataPromiseFulfilled, loading: promiseFulfilled } = store.getState().drugs; + + expect(dataPromiseFulfilled).toEqual(drugsFixture); + expect(promiseFulfilled).toEqual('succeeded'); + }); }); }); -- GitLab