You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

patternsReducer.test.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import * as redux from "react-redux";
  2. import store from "../../store";
  3. import { Router } from "react-router-dom";
  4. import history from "../../store/utils/history";
  5. import { mockState } from "../../mockState";
  6. import { render } from "@testing-library/react";
  7. import PatternsPage from "../../pages/PatternsPage/PatternsPage";
  8. import * as api from "../../request/patternsRequest";
  9. import { runSaga } from "redux-saga";
  10. import { FETCH_PATTERNS_REQ } from "../../store/actions/patterns/patternsActionConstants";
  11. import ColorModeProvider from "../../context/ColorModeContext";
  12. import * as fc from "../../store/saga/patternsSaga";
  13. import {
  14. setFilteredPatterns,
  15. setPatterns,
  16. } from "../../store/actions/patterns/patternsActions";
  17. import { setPattern } from "../../store/actions/pattern/patternActions";
  18. import { setPatternApplicants } from "../../store/actions/patternApplicants/patternApplicantsActions";
  19. import { createPattern } from "../../store/actions/createPattern/createPatternActions";
  20. import { updatePattern } from "../../store/actions/updatePattern/updatePatternActions";
  21. import { scheduleAppointment } from "../../store/actions/scheduleAppointment/scheduleAppointmentActions";
  22. describe("Patterns reducer tests", () => {
  23. const cont = (
  24. <redux.Provider store={store}>
  25. <Router history={history}>
  26. <ColorModeProvider>
  27. <PatternsPage />
  28. </ColorModeProvider>
  29. </Router>
  30. </redux.Provider>
  31. );
  32. let spyOnUseSelector;
  33. let spyOnUseDispatch;
  34. let mockDispatch;
  35. beforeEach(() => {
  36. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  37. spyOnUseSelector.mockReturnValueOnce(mockState.patterns);
  38. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  39. mockDispatch = jest.fn();
  40. spyOnUseDispatch.mockReturnValue(mockDispatch);
  41. });
  42. afterEach(() => {
  43. jest.restoreAllMocks();
  44. });
  45. it("Should dispatch get patterns request when rendered", () => {
  46. render(cont);
  47. expect(mockDispatch).toHaveBeenCalledWith({
  48. type: FETCH_PATTERNS_REQ,
  49. });
  50. });
  51. it("Should load and handle patterns in case of success", async () => {
  52. const dispatchedActions = [];
  53. const mockedCall = { data: mockState.patterns.patterns };
  54. api.getAllPatterns = jest.fn(() => Promise.resolve(mockedCall));
  55. const fakeStore = {
  56. getState: () => mockState.patterns.patterns,
  57. dispatch: (action) => dispatchedActions.push(action),
  58. };
  59. await runSaga(fakeStore, fc.getPatterns, {}).done;
  60. expect(api.getAllPatterns.mock.calls.length).toBe(1);
  61. expect(dispatchedActions).toContainEqual(setPatterns(mockedCall.data));
  62. });
  63. it("Should load and handle pattern by id in case of success", async () => {
  64. const dispatchedActions = [];
  65. const id = 1;
  66. const filteredData = mockState.patterns.patterns.filter(
  67. (pattern) => pattern.id === id
  68. );
  69. const mockedCall = { data: filteredData };
  70. api.getPatternById = jest.fn(() => Promise.resolve(mockedCall));
  71. const fakeStore = {
  72. getState: () => mockState.patterns.patterns,
  73. dispatch: (action) => dispatchedActions.push(action),
  74. };
  75. await runSaga(fakeStore, fc.getPattern, { payload: id }).done;
  76. expect(api.getPatternById.mock.calls.length).toBe(1);
  77. expect(dispatchedActions).toContainEqual(setPattern(mockedCall.data));
  78. });
  79. it("Should load and handle pattern applicants in case of success", async () => {
  80. const dispatchedActions = [];
  81. const id = 1;
  82. const filteredData = mockState.patterns.patterns.filter(
  83. (pattern) => pattern.id === id
  84. );
  85. const mockedCall = {
  86. data: {
  87. ...filteredData[0].selectionLevel.selectionProcesses[0].applicant,
  88. },
  89. };
  90. api.getPatternApplicantsById = jest.fn(() => Promise.resolve(mockedCall));
  91. const fakeStore = {
  92. getState: () => mockState.patterns.patterns,
  93. dispatch: (action) => dispatchedActions.push(action),
  94. };
  95. await runSaga(fakeStore, fc.getPatternApplicants, { payload: id }).done;
  96. expect(api.getPatternApplicantsById.mock.calls.length).toBe(1);
  97. expect(dispatchedActions).toContainEqual(
  98. setPatternApplicants(mockedCall.data)
  99. );
  100. });
  101. it("Should load and handle filtered patterns in case of success", async () => {
  102. const dispatchedActions = [];
  103. const filters = {
  104. fromDate: new Date("2-2-2021"),
  105. toDate: new Date("3-3-2023"),
  106. selectionLevels: [1, 2],
  107. };
  108. const filteredData = mockState.patterns.patterns.filter(
  109. (pattern) =>
  110. pattern.createdAt >= filters.fromDate &&
  111. pattern.createdAt <= filters.toDate
  112. );
  113. const mockedCall = {
  114. data: filteredData,
  115. };
  116. api.getFilteredPatterns = jest.fn(() => Promise.resolve(mockedCall));
  117. const fakeStore = {
  118. getState: () => mockState.patterns.patterns,
  119. dispatch: (action) => dispatchedActions.push(action),
  120. };
  121. await runSaga(fakeStore, fc.filterPatterns, filters).done;
  122. expect(api.getFilteredPatterns.mock.calls.length).toBe(1);
  123. expect(dispatchedActions).toContainEqual(
  124. setFilteredPatterns(mockedCall.data)
  125. );
  126. });
  127. it("Should create new pattern", async () => {
  128. const dispatchedActions = [];
  129. const mockedCall = {
  130. data: {
  131. title: "Neuspesan korak",
  132. selectionLevelId: 1,
  133. message: "Poruka",
  134. },
  135. };
  136. api.createPatternRequest = jest.fn(() => Promise.resolve(mockedCall));
  137. const fakeStore = {
  138. getState: () => mockState.patterns.patterns,
  139. dispatch: (action) => dispatchedActions.push(action),
  140. };
  141. await runSaga(fakeStore, fc.createPatternSaga, {
  142. payload: {
  143. title: "Neuspesan korak",
  144. selectionLevelId: 1,
  145. message: "Poruka",
  146. },
  147. }).done;
  148. expect(api.createPatternRequest.mock.calls.length).toBe(1);
  149. expect(dispatchedActions).toContainEqual(createPattern(mockedCall.data));
  150. });
  151. it("Should update pattern", async () => {
  152. const dispatchedActions = [];
  153. const mockedCall = {
  154. data: {
  155. id: 1,
  156. title: "Zakazan intervju",
  157. createdAt: new Date(),
  158. selectionLevelId: 1,
  159. message: "Message",
  160. },
  161. };
  162. api.updatePatternRequest = jest.fn(() => Promise.resolve(mockedCall));
  163. const fakeStore = {
  164. getState: () => mockState.patterns.patterns,
  165. dispatch: (action) => dispatchedActions.push(action),
  166. };
  167. await runSaga(fakeStore, fc.updatePatternSaga, {
  168. payload: {
  169. id: 1,
  170. title: "Zakazan intervju",
  171. createdAt: new Date(),
  172. selectionLevelId: 1,
  173. message: "Message",
  174. },
  175. }).done;
  176. expect(api.updatePatternRequest.mock.calls.length).toBe(1);
  177. expect(dispatchedActions).toContainEqual(updatePattern(mockedCall.data));
  178. });
  179. it("Should schedule appointment", async () => {
  180. const dispatchedActions = [];
  181. const mockedCall = {
  182. data: {
  183. emails: ["ermin.bronja@dilig.net"],
  184. patternId: 1,
  185. handleApiResponseSuccess: jest.fn,
  186. },
  187. };
  188. api.scheduleAppointmentRequest = jest.fn(() => Promise.resolve(mockedCall));
  189. const fakeStore = {
  190. getState: () => mockState.patterns.patterns,
  191. dispatch: (action) => dispatchedActions.push(action),
  192. };
  193. await runSaga(fakeStore, fc.scheduleAppointmentSaga, {
  194. payload: {
  195. emails: ["ermin.bronja@dilig.net"],
  196. patternId: 1,
  197. handleApiResponseSuccess: jest.fn,
  198. },
  199. }).done;
  200. expect(api.scheduleAppointmentRequest.mock.calls.length).toBe(1);
  201. expect(dispatchedActions).toContainEqual(
  202. scheduleAppointment(mockedCall.data)
  203. );
  204. });
  205. it("Should not return patterns when exception was thrown", async () => {
  206. const dispatchedActions = [];
  207. const error = {
  208. response: {
  209. data: { message: "Error" },
  210. },
  211. };
  212. api.getAllPatterns = jest.fn(() => Promise.reject(error));
  213. const mockfn = jest.fn();
  214. const fakeStore = {
  215. getState: () => mockState.patterns.patterns,
  216. dispatch: (action) => dispatchedActions.push(action),
  217. };
  218. // wait for saga to complete
  219. await runSaga(fakeStore, fc.getPatterns, {}).done;
  220. expect(mockfn).not.toHaveBeenCalled();
  221. });
  222. it("Should not return pattern when exception was thrown", async () => {
  223. const dispatchedActions = [];
  224. const error = {
  225. response: {
  226. data: { message: "Error" },
  227. },
  228. };
  229. api.getPatternById = jest.fn(() => Promise.reject(error));
  230. const mockfn = jest.fn();
  231. const fakeStore = {
  232. getState: () => mockState.patterns.patterns,
  233. dispatch: (action) => dispatchedActions.push(action),
  234. };
  235. // wait for saga to complete
  236. await runSaga(fakeStore, fc.getPattern, {
  237. payload: {
  238. id: 1,
  239. },
  240. }).done;
  241. expect(mockfn).not.toHaveBeenCalled();
  242. });
  243. it("Should not return pattern applicants when exception was thrown", async () => {
  244. const dispatchedActions = [];
  245. const error = {
  246. response: {
  247. data: { message: "Error" },
  248. },
  249. };
  250. api.getPatternApplicants = jest.fn(() => Promise.reject(error));
  251. const mockfn = jest.fn();
  252. const fakeStore = {
  253. getState: () => mockState.patterns.patterns,
  254. dispatch: (action) => dispatchedActions.push(action),
  255. };
  256. // wait for saga to complete
  257. await runSaga(fakeStore, fc.getPatternApplicants, {
  258. payload: {
  259. id: 1,
  260. },
  261. }).done;
  262. expect(mockfn).not.toHaveBeenCalled();
  263. });
  264. it("Should not return filtered patterns when exception was thrown", async () => {
  265. const dispatchedActions = [];
  266. const error = {
  267. response: {
  268. data: { message: "Error" },
  269. },
  270. };
  271. api.getFilteredPatterns = jest.fn(() => Promise.reject(error));
  272. const mockfn = jest.fn();
  273. const fakeStore = {
  274. getState: () => mockState.patterns.patterns,
  275. dispatch: (action) => dispatchedActions.push(action),
  276. };
  277. // wait for saga to complete
  278. await runSaga(fakeStore, fc.filterPatterns, {
  279. fromDate: new Date("2-2-2021"),
  280. toDate: new Date("3-3-2023"),
  281. selectionLevels: [1, 2],
  282. }).done;
  283. expect(mockfn).not.toHaveBeenCalled();
  284. });
  285. it("Should not create pattern when exception was thrown", async () => {
  286. const dispatchedActions = [];
  287. const error = {
  288. response: {
  289. data: { message: "Error" },
  290. },
  291. };
  292. api.createPatternRequest = jest.fn(() => Promise.reject(error));
  293. const mockfn = jest.fn();
  294. const fakeStore = {
  295. getState: () => mockState.patterns.patterns,
  296. dispatch: (action) => dispatchedActions.push(action),
  297. };
  298. // wait for saga to complete
  299. await runSaga(fakeStore, fc.createPatternSaga, {
  300. payload: {
  301. title: "Neuspesan korak",
  302. selectionLevelId: 1,
  303. message: "Poruka",
  304. },
  305. }).done;
  306. expect(mockfn).not.toHaveBeenCalled();
  307. });
  308. it("Should not update pattern when exception was thrown", async () => {
  309. const dispatchedActions = [];
  310. const error = {
  311. response: {
  312. data: { message: "Error" },
  313. },
  314. };
  315. api.updatePatternRequest = jest.fn(() => Promise.reject(error));
  316. const mockfn = jest.fn();
  317. const fakeStore = {
  318. getState: () => mockState.patterns.patterns,
  319. dispatch: (action) => dispatchedActions.push(action),
  320. };
  321. // wait for saga to complete
  322. await runSaga(fakeStore, fc.updatePatternSaga, {
  323. payload: {
  324. id: 1,
  325. title: "Zakazan intervju",
  326. createdAt: new Date(),
  327. selectionLevelId: 1,
  328. message: "Message",
  329. },
  330. }).done;
  331. expect(mockfn).not.toHaveBeenCalled();
  332. });
  333. it("Should not chedule appointment when exception was thrown", async () => {
  334. const dispatchedActions = [];
  335. const error = {
  336. response: {
  337. data: { message: "Error" },
  338. },
  339. };
  340. api.scheduleAppointmentRequest = jest.fn(() => Promise.reject(error));
  341. const mockfn = jest.fn();
  342. const fakeStore = {
  343. getState: () => mockState.patterns.patterns,
  344. dispatch: (action) => dispatchedActions.push(action),
  345. };
  346. // wait for saga to complete
  347. await runSaga(fakeStore, fc.scheduleAppointmentSaga, {
  348. payload: {
  349. emails: ["ermin.bronja@dilig.net"],
  350. patternId: 1,
  351. handleApiResponseSuccess: jest.fn,
  352. },
  353. }).done;
  354. expect(mockfn).not.toHaveBeenCalled();
  355. });
  356. });