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

candidateDetailsPageReducer.test.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 { CANDIDATE_FETCH } from "../../store/actions/candidate/candidateActionConstants";
  10. import { FETCH_USERS_REQ } from "../../store/actions/users/usersActionConstants";
  11. import { getSingleCandidate } from "../../store/saga/candidatesSaga";
  12. import {
  13. fetchCandidateSuccess,
  14. fetchCandidateError,
  15. } from "../../store/actions/candidate/candidateActions";
  16. import * as helper from "../../util/helpers/rejectErrorCodeHelper";
  17. import CandidateDetailsPage from "../../pages/CandidatesPage/CandidateDetailsPage";
  18. const mockHistoryPush = jest.fn();
  19. // mock param which we send as part of URL
  20. jest.mock("react-router-dom", () => ({
  21. ...jest.requireActual("react-router-dom"),
  22. useHistory: () => ({
  23. push: mockHistoryPush,
  24. }),
  25. useParams: () => ({
  26. id: 1,
  27. }),
  28. }));
  29. describe("CandidateDetailsPage render tests", () => {
  30. var props = {
  31. history: {
  32. replace: jest.fn(),
  33. push: jest.fn(),
  34. location: {
  35. pathname: "",
  36. },
  37. },
  38. };
  39. const cont = (
  40. <redux.Provider store={store}>
  41. <Router history={history}>
  42. <CandidateDetailsPage {...props} />
  43. </Router>
  44. </redux.Provider>
  45. );
  46. let spyOnUseSelector;
  47. let spyOnUseDispatch;
  48. let mockDispatch;
  49. beforeEach(() => {
  50. spyOnUseSelector = jest.spyOn(redux, "useSelector");
  51. spyOnUseSelector
  52. .mockReturnValueOnce(mockState.users.users)
  53. .mockReturnValueOnce(mockState.users.user)
  54. .mockReturnValueOnce(mockState.candidate.candidate);
  55. spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
  56. // Mock dispatch function returned from useDispatch
  57. mockDispatch = jest.fn();
  58. spyOnUseDispatch.mockReturnValue(mockDispatch);
  59. });
  60. afterEach(() => {
  61. jest.restoreAllMocks();
  62. });
  63. it("Should dispatch fetch candidate request when rendered", () => {
  64. render(cont);
  65. expect(mockDispatch).toHaveBeenCalledWith({
  66. payload: { id: 1 },
  67. type: CANDIDATE_FETCH,
  68. });
  69. });
  70. it("Should dispatch fetch users request when rendered", () => {
  71. render(cont);
  72. expect(mockDispatch).toHaveBeenCalledWith({
  73. type: FETCH_USERS_REQ,
  74. });
  75. });
  76. it("should load and handle candidate in case of success", async () => {
  77. const dispatchedActions = [];
  78. helper.rejectErrorCodeHelper = jest.fn(() => "Server error");
  79. const mockedCall = { data: mockState.candidate.candidate };
  80. api.getCandidate = jest.fn(() => Promise.resolve(mockedCall));
  81. const fakeStore = {
  82. getState: () => mockState.candidate.candidate,
  83. dispatch: (action) => dispatchedActions.push(action),
  84. };
  85. await runSaga(fakeStore, getSingleCandidate, { payload: { id: 1 } }).done;
  86. expect(api.getCandidate.mock.calls.length).toBe(1);
  87. expect(dispatchedActions).toContainEqual(
  88. fetchCandidateSuccess(mockedCall.data)
  89. );
  90. });
  91. it("should handle candidate load errors in case of failure", async () => {
  92. const dispatchedActions = [];
  93. helper.rejectErrorCodeHelper = jest.fn(
  94. () => mockState.candidate.fetchCandidateErrorMessage
  95. );
  96. const error = {
  97. response: {
  98. data: { message: mockState.candidate.fetchCandidateErrorMessage },
  99. },
  100. };
  101. api.getCandidate = jest.fn(() => Promise.reject(error));
  102. const fakeStore = {
  103. getState: () => mockState.candidate.candidate,
  104. dispatch: (action) => dispatchedActions.push(action),
  105. };
  106. await runSaga(fakeStore, getSingleCandidate, { payload: { id: 1 } }).done;
  107. expect(api.getCandidate.mock.calls.length).toBe(1);
  108. expect(dispatchedActions).toContainEqual(
  109. fetchCandidateError(error.response.data.message)
  110. );
  111. });
  112. });