thunks.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. import * as schemaFixtures from 'redux/reducers/schemas/__test__/fixtures';
  12. import * as fixtures from './fixtures';
  13. const middlewares: Array<Middleware> = [thunk];
  14. type DispatchExts = ThunkDispatch<RootState, undefined, Action>;
  15. const mockStoreCreator: MockStoreCreator<
  16. RootState,
  17. DispatchExts
  18. > = configureMockStore<RootState, DispatchExts>(middlewares);
  19. const store: MockStoreEnhanced<RootState, DispatchExts> = mockStoreCreator();
  20. const clusterName = 'local';
  21. const subject = 'test';
  22. describe('Thunks', () => {
  23. afterEach(() => {
  24. fetchMock.restore();
  25. store.clearActions();
  26. });
  27. describe('fetchClusterStats', () => {
  28. it('creates GET_CLUSTER_STATUS__SUCCESS when fetching cluster stats', async () => {
  29. fetchMock.getOnce(`/api/clusters/${clusterName}/stats`, {
  30. body: fixtures.clusterStats,
  31. });
  32. await store.dispatch(thunks.fetchClusterStats(clusterName));
  33. expect(store.getActions()).toEqual([
  34. actions.fetchClusterStatsAction.request(),
  35. actions.fetchClusterStatsAction.success(fixtures.clusterStats),
  36. ]);
  37. });
  38. it('creates GET_CLUSTER_STATUS__FAILURE when fetching cluster stats', async () => {
  39. fetchMock.getOnce(`/api/clusters/${clusterName}/stats`, 404);
  40. await store.dispatch(thunks.fetchClusterStats(clusterName));
  41. expect(store.getActions()).toEqual([
  42. actions.fetchClusterStatsAction.request(),
  43. actions.fetchClusterStatsAction.failure(),
  44. ]);
  45. });
  46. });
  47. describe('fetchSchemasByClusterName', () => {
  48. it('creates GET_CLUSTER_SCHEMAS__SUCCESS when fetching cluster schemas', async () => {
  49. fetchMock.getOnce(`/api/clusters/${clusterName}/schemas`, {
  50. body: schemaFixtures.clusterSchemasPayload,
  51. });
  52. await store.dispatch(thunks.fetchSchemasByClusterName(clusterName));
  53. expect(store.getActions()).toEqual([
  54. actions.fetchSchemasByClusterNameAction.request(),
  55. actions.fetchSchemasByClusterNameAction.success(
  56. schemaFixtures.clusterSchemasPayload
  57. ),
  58. ]);
  59. });
  60. it('creates GET_CLUSTER_SCHEMAS__FAILURE when fetching cluster schemas', async () => {
  61. fetchMock.getOnce(`/api/clusters/${clusterName}/schemas`, 404);
  62. await store.dispatch(thunks.fetchSchemasByClusterName(clusterName));
  63. expect(store.getActions()).toEqual([
  64. actions.fetchSchemasByClusterNameAction.request(),
  65. actions.fetchSchemasByClusterNameAction.failure(),
  66. ]);
  67. });
  68. });
  69. describe('fetchSchemaVersions', () => {
  70. it('creates GET_SCHEMA_VERSIONS__SUCCESS when fetching schema versions', async () => {
  71. fetchMock.getOnce(
  72. `/api/clusters/${clusterName}/schemas/${subject}/versions`,
  73. {
  74. body: schemaFixtures.schemaVersionsPayload,
  75. }
  76. );
  77. await store.dispatch(thunks.fetchSchemaVersions(clusterName, subject));
  78. expect(store.getActions()).toEqual([
  79. actions.fetchSchemaVersionsAction.request(),
  80. actions.fetchSchemaVersionsAction.success(
  81. schemaFixtures.schemaVersionsPayload
  82. ),
  83. ]);
  84. });
  85. it('creates GET_SCHEMA_VERSIONS__FAILURE when fetching schema versions', async () => {
  86. fetchMock.getOnce(
  87. `/api/clusters/${clusterName}/schemas/${subject}/versions`,
  88. 404
  89. );
  90. await store.dispatch(thunks.fetchSchemaVersions(clusterName, subject));
  91. expect(store.getActions()).toEqual([
  92. actions.fetchSchemaVersionsAction.request(),
  93. actions.fetchSchemaVersionsAction.failure(),
  94. ]);
  95. });
  96. });
  97. describe('createSchema', () => {
  98. it('creates POST_SCHEMA__SUCCESS when posting new schema', async () => {
  99. fetchMock.postOnce(`/api/clusters/${clusterName}/schemas/${subject}`, {
  100. body: schemaFixtures.schemaVersionsPayload[0],
  101. });
  102. await store.dispatch(
  103. thunks.createSchema(clusterName, subject, fixtures.schemaPayload)
  104. );
  105. expect(store.getActions()).toEqual([
  106. actions.createSchemaAction.request(),
  107. actions.createSchemaAction.success(
  108. schemaFixtures.schemaVersionsPayload[0]
  109. ),
  110. ]);
  111. });
  112. // it('creates POST_SCHEMA__FAILURE when posting new schema', async () => {
  113. // fetchMock.postOnce(
  114. // `/api/clusters/${clusterName}/schemas/${subject}`,
  115. // 404
  116. // );
  117. // await store.dispatch(
  118. // thunks.createSchema(clusterName, subject, fixtures.schemaPayload)
  119. // );
  120. // expect(store.getActions()).toEqual([
  121. // actions.createSchemaAction.request(),
  122. // actions.createSchemaAction.failure(),
  123. // ]);
  124. // expect(store.getActions()).toThrow();
  125. // });
  126. });
  127. });