topics.spec.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import configureMockStore, {
  2. MockStoreCreator,
  3. MockStoreEnhanced,
  4. } from 'redux-mock-store';
  5. import thunk, { ThunkDispatch } from 'redux-thunk';
  6. import fetchMock from 'fetch-mock-jest';
  7. import { Middleware } from 'redux';
  8. import { RootState, Action } from 'redux/interfaces';
  9. import * as actions from 'redux/actions/actions';
  10. import * as thunks from 'redux/actions/thunks';
  11. const middlewares: Array<Middleware> = [thunk];
  12. type DispatchExts = ThunkDispatch<RootState, undefined, Action>;
  13. const mockStoreCreator: MockStoreCreator<
  14. RootState,
  15. DispatchExts
  16. > = configureMockStore<RootState, DispatchExts>(middlewares);
  17. const store: MockStoreEnhanced<RootState, DispatchExts> = mockStoreCreator();
  18. const clusterName = 'local';
  19. const topicName = 'localTopic';
  20. describe('Thunks', () => {
  21. afterEach(() => {
  22. fetchMock.restore();
  23. store.clearActions();
  24. });
  25. describe('deleteTopis', () => {
  26. it('creates DELETE_TOPIC__SUCCESS when deleting existing topic', async () => {
  27. fetchMock.deleteOnce(
  28. `/api/clusters/${clusterName}/topics/${topicName}`,
  29. 200
  30. );
  31. await store.dispatch(thunks.deleteTopic(clusterName, topicName));
  32. expect(store.getActions()).toEqual([
  33. actions.deleteTopicAction.request(),
  34. actions.deleteTopicAction.success(topicName),
  35. ]);
  36. });
  37. it('creates DELETE_TOPIC__FAILURE when deleting existing topic', async () => {
  38. fetchMock.deleteOnce(
  39. `/api/clusters/${clusterName}/topics/${topicName}`,
  40. 404
  41. );
  42. try {
  43. await store.dispatch(thunks.deleteTopic(clusterName, topicName));
  44. } catch (error) {
  45. expect(error.status).toEqual(404);
  46. expect(store.getActions()).toEqual([
  47. actions.deleteTopicAction.request(),
  48. actions.deleteTopicAction.failure(),
  49. ]);
  50. }
  51. });
  52. });
  53. });