import configureMockStore, { MockStoreCreator, MockStoreEnhanced, } from 'redux-mock-store'; import thunk, { ThunkDispatch } from 'redux-thunk'; import fetchMock from 'fetch-mock-jest'; import { Middleware } from 'redux'; import { RootState, Action } from 'redux/interfaces'; import * as actions from 'redux/actions/actions'; import * as thunks from 'redux/actions/thunks'; const middlewares: Array = [thunk]; type DispatchExts = ThunkDispatch; const mockStoreCreator: MockStoreCreator< RootState, DispatchExts > = configureMockStore(middlewares); const store: MockStoreEnhanced = mockStoreCreator(); const clusterName = 'local'; const topicName = 'localTopic'; describe('Thunks', () => { afterEach(() => { fetchMock.restore(); store.clearActions(); }); describe('deleteTopis', () => { it('creates DELETE_TOPIC__SUCCESS when deleting existing topic', async () => { fetchMock.deleteOnce( `/api/clusters/${clusterName}/topics/${topicName}`, 200 ); await store.dispatch(thunks.deleteTopic(clusterName, topicName)); expect(store.getActions()).toEqual([ actions.deleteTopicAction.request(), actions.deleteTopicAction.success(topicName), ]); }); it('creates DELETE_TOPIC__FAILURE when deleting existing topic', async () => { fetchMock.deleteOnce( `/api/clusters/${clusterName}/topics/${topicName}`, 404 ); try { await store.dispatch(thunks.deleteTopic(clusterName, topicName)); } catch (error) { expect(error.status).toEqual(404); expect(store.getActions()).toEqual([ actions.deleteTopicAction.request(), actions.deleteTopicAction.failure(), ]); } }); }); });