Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

processesReducer.test.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { runSaga } from "redux-saga";
  2. import * as api from "../../request/processesReguest";
  3. import { render } from "@testing-library/react";
  4. import * as redux from "react-redux";
  5. import SelectionProcessPage from "../../pages/selectionProcessPage/selectionProcessPage";
  6. ("../../pages/SelectionProcessPage/SelectionProcessPage");
  7. import store from "../../store";
  8. import "../../i18n";
  9. import { mockState } from "../../mockState";
  10. import { FETCH_PROCESSES_REQ } from "../../store/actions/processes/processesActionConstants";
  11. import { Router } from "react-router-dom";
  12. import history from "../../store/utils/history";
  13. import {
  14. getProcesses,
  15. getFilteredProcesses,
  16. } from "../../store/saga/processSaga";
  17. import {
  18. setProcesses,
  19. setProcessesError,
  20. } from "../../store/actions/processes/processesAction";
  21. import { SelectionProvider } from "../../context/SelectionContext";
  22. describe("SelectionProcessPage render tests", () => {
  23. const cont = (
  24. <redux.Provider store={store}>
  25. <SelectionProvider>
  26. <Router history={history}>
  27. <SelectionProcessPage />
  28. </Router>
  29. </SelectionProvider>
  30. </redux.Provider>
  31. );
  32. let spyOnUseSelector;
  33. let spyOnUseDispatch;
  34. let mockDispatch;
  35. beforeEach(() => {
  36. // Mock useSelector hook
  37. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  38. spyOnUseSelector
  39. .mockReturnValueOnce(mockState.selections)
  40. .mockReturnValueOnce(mockState.selections.processes)
  41. .mockReturnValueOnce(mockState.selections.statuses);
  42. // Mock useDispatch hook
  43. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  44. // Mock dispatch function returned from useDispatch
  45. mockDispatch = jest.fn();
  46. spyOnUseDispatch.mockReturnValue(mockDispatch);
  47. });
  48. afterEach(() => {
  49. jest.restoreAllMocks();
  50. });
  51. it("Should dispatch get processes request when rendered", () => {
  52. render(cont);
  53. expect(mockDispatch).toHaveBeenCalledWith({
  54. type: FETCH_PROCESSES_REQ,
  55. });
  56. });
  57. it("should load and handle levels with processes in case of success", async () => {
  58. // we push all dispatched actions to make assertions easier
  59. // and our tests less brittle
  60. const dispatchedActions = [];
  61. // we don't want to perform an actual api call in our tests
  62. // so we will mock the getAllUsers api with jest
  63. // this will mutate the dependency which we may reset if other tests
  64. // are dependent on it
  65. const mockedCall = { data: mockState.selections.processes };
  66. api.getAllLevels = jest.fn(() => Promise.resolve(mockedCall));
  67. const fakeStore = {
  68. getState: () => mockState.selections.processes,
  69. dispatch: (action) => dispatchedActions.push(action),
  70. };
  71. // wait for saga to complete
  72. await runSaga(fakeStore, getProcesses).done;
  73. expect(api.getAllLevels.mock.calls.length).toBe(1);
  74. expect(dispatchedActions).toContainEqual(setProcesses(mockedCall.data));
  75. });
  76. it("should handle processes load errors in case of failure", async () => {
  77. const dispatchedActions = [];
  78. // we simulate an error by rejecting the promise
  79. // then we assert if our saga dispatched the action(s) correctly
  80. const error = {
  81. response: {
  82. data: { message: mockState.selections.fetchSelectionsErrorMessage },
  83. },
  84. };
  85. api.getAllLevels = jest.fn(() => Promise.reject(error));
  86. const fakeStore = {
  87. getState: () => mockState.users.users,
  88. dispatch: (action) => dispatchedActions.push(action),
  89. };
  90. await runSaga(fakeStore, getProcesses).done;
  91. expect(api.getAllLevels.mock.calls.length).toBe(1);
  92. expect(dispatchedActions).toContainEqual(
  93. setProcessesError(error.response.data.message)
  94. );
  95. });
  96. it("should load and handle levels with filtered processes in case of success", async () => {
  97. // we push all dispatched actions to make assertions easier
  98. // and our tests less brittle
  99. const dispatchedActions = [];
  100. const filter = {
  101. statuses: ["Zakazan", "Odrađen"],
  102. dateStart: new Date(2023, 0, 5),
  103. dateEnd: new Date(2024, 1, 1),
  104. };
  105. const filteredData = [];
  106. mockState.selections.processes.forEach((level) => {
  107. const filteredLevel = level;
  108. filteredLevel.selectionProcesses = level.selectionProcesses.filter(
  109. (v) =>
  110. v.date >= filter.dateStart &&
  111. v.date <= filter.dateEnd &&
  112. filter.statuses.includes(v.status)
  113. );
  114. filteredData.push(filteredLevel);
  115. });
  116. // we don't want to perform an actual api call in our tests
  117. // so we will mock the getAllUsers api with jest
  118. // this will mutate the dependency which we may reset if other tests
  119. // are dependent on it
  120. const mockedCall = { data: filteredData };
  121. api.getAllFilteredProcessesReq = jest.fn(() => Promise.resolve(mockedCall));
  122. const fakeStore = {
  123. getState: () => mockState.selections.processes,
  124. dispatch: (action) => dispatchedActions.push(action),
  125. };
  126. // wait for saga to complete
  127. await runSaga(fakeStore, getFilteredProcesses, filter).done;
  128. expect(api.getAllFilteredProcessesReq.mock.calls.length).toBe(1);
  129. expect(dispatchedActions).toContainEqual(setProcesses(filteredData));
  130. });
  131. it("should handle process to set it done in case of success", async () => {
  132. // we push all dispatched actions to make assertions easier
  133. // and our tests less brittle
  134. const dispatchedActions = [];
  135. const filter = {
  136. statuses: ["Zakazan", "Odrađen"],
  137. dateStart: new Date(2023, 0, 5),
  138. dateEnd: new Date(2024, 1, 1),
  139. };
  140. const filteredData = [];
  141. mockState.selections.processes.forEach((level) => {
  142. const filteredLevel = level;
  143. filteredLevel.selectionProcesses = level.selectionProcesses.filter(
  144. (v) =>
  145. v.date >= filter.dateStart &&
  146. v.date <= filter.dateEnd &&
  147. filter.statuses.includes(v.status)
  148. );
  149. filteredData.push(filteredLevel);
  150. });
  151. // we don't want to perform an actual api call in our tests
  152. // so we will mock the getAllUsers api with jest
  153. // this will mutate the dependency which we may reset if other tests
  154. // are dependent on it
  155. const mockedCall = { data: filteredData };
  156. api.getAllFilteredProcessesReq = jest.fn(() => Promise.resolve(mockedCall));
  157. const fakeStore = {
  158. getState: () => mockState.selections.processes,
  159. dispatch: (action) => dispatchedActions.push(action),
  160. };
  161. // wait for saga to complete
  162. await runSaga(fakeStore, getFilteredProcesses, filter).done;
  163. expect(api.getAllFilteredProcessesReq.mock.calls.length).toBe(1);
  164. expect(dispatchedActions).toContainEqual(setProcesses(filteredData));
  165. });
  166. });