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

adsCandidatesPageReducer.test.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 * as api from "../../request/candidatesRequest";
  8. import { runSaga } from "redux-saga";
  9. import { ADS_CANDIDATES_FETCH } from "../../store/actions/candidates/candidatesActionConstants";
  10. import { getAdsCandidates } from "../../store/saga/candidatesSaga";
  11. import {
  12. fetchAdsCandidates,
  13. fetchAdsCandidatesError,
  14. } from "../../store/actions/candidates/candidatesActions";
  15. import AdsCandidatesPage from "../../pages/CandidatesPage/AdsCandidatesPage";
  16. describe("AdsCandidatesPage render tests", () => {
  17. const cont = (
  18. <redux.Provider store={store}>
  19. <Router history={history}>
  20. <AdsCandidatesPage search="" />
  21. </Router>
  22. </redux.Provider>
  23. );
  24. let spyOnUseSelector;
  25. let spyOnUseDispatch;
  26. let mockDispatch;
  27. beforeEach(() => {
  28. // Mock useSelector hook
  29. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  30. spyOnUseSelector
  31. .mockReturnValueOnce(mockState.candidates.adsCandidates)
  32. .mockReturnValueOnce(mockState.candidates.adsCandidates);
  33. // Mock useDispatch hook
  34. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  35. // Mock dispatch function returned from useDispatch
  36. mockDispatch = jest.fn();
  37. spyOnUseDispatch.mockReturnValue(mockDispatch);
  38. });
  39. afterEach(() => {
  40. jest.restoreAllMocks();
  41. });
  42. it("Should dispatch get adsCandidates request when rendered", () => {
  43. render(cont);
  44. expect(mockDispatch).toHaveBeenCalledWith({
  45. type: ADS_CANDIDATES_FETCH,
  46. payload: {
  47. currentPage: 0,
  48. employmentType: "",
  49. maxDateOfApplication: "",
  50. maxExperience: 0,
  51. minDateOfApplication: "",
  52. minExperience: 0,
  53. pageSize: 0,
  54. technologies: [],
  55. },
  56. });
  57. });
  58. it("should load and handle adsCandidates in case of success", async () => {
  59. const dispatchedActions = [];
  60. const mockedCall = { data: mockState.technologies.technologies };
  61. api.getFilteredAdsCandidates = jest.fn(() => Promise.resolve(mockedCall));
  62. const fakeStore = {
  63. getState: () => mockState.candidates.adsCandidates,
  64. dispatch: (action) => dispatchedActions.push(action),
  65. };
  66. await runSaga(fakeStore, getAdsCandidates, {}).done;
  67. expect(api.getFilteredAdsCandidates.mock.calls.length).toBe(1);
  68. expect(dispatchedActions[0].payload).toBe(
  69. fetchAdsCandidates(mockedCall.data).payload
  70. );
  71. });
  72. // it("should handle adsCandidates load errors in case of failure", async () => {
  73. // const dispatchedActions = [];
  74. // const error = {
  75. // response: {
  76. // data: { message: mockState.candidates.fetchCandidatesErrorMessage },
  77. // },
  78. // };
  79. // api.getFilteredAdsCandidates = jest.fn(() => Promise.reject(error));
  80. // const fakeStore = {
  81. // getState: () => mockState.candidates.adsCandidates,
  82. // dispatch: (action) => dispatchedActions.push(action),
  83. // };
  84. // await runSaga(fakeStore, getAdsCandidates,{}).done;
  85. // expect(api.getFilteredAdsCandidates.mock.calls.length).toBe(1);
  86. // expect(dispatchedActions).toContainEqual(
  87. // fetchAdsCandidates(error.response.data.message)
  88. // );
  89. // });
  90. });