Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

processesReducer.test.js 6.4KB

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