Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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