import * as redux from "react-redux"; import store from "../../store"; import { Router } from "react-router-dom"; import history from "../../store/utils/history"; import { mockState } from "../../mockState"; import { render } from "@testing-library/react"; import * as api from "../../request/candidatesRequest"; import { runSaga } from "redux-saga"; import { ADS_CANDIDATES_FETCH } from "../../store/actions/candidates/candidatesActionConstants"; import { getAdsCandidates } from "../../store/saga/candidatesSaga"; import { fetchAdsCandidates, fetchAdsCandidatesError, } from "../../store/actions/candidates/candidatesActions"; import AdsCandidatesPage from "../../pages/CandidatesPage/AdsCandidatesPage"; describe("AdsCandidatesPage render tests", () => { const cont = ( ); let spyOnUseSelector; let spyOnUseDispatch; let mockDispatch; beforeEach(() => { // Mock useSelector hook spyOnUseSelector = jest.spyOn(redux, "useSelector"); spyOnUseSelector .mockReturnValueOnce(mockState.candidates.adsCandidates) .mockReturnValueOnce(mockState.candidates.adsCandidates); // Mock useDispatch hook spyOnUseDispatch = jest.spyOn(redux, "useDispatch"); // Mock dispatch function returned from useDispatch mockDispatch = jest.fn(); spyOnUseDispatch.mockReturnValue(mockDispatch); }); afterEach(() => { jest.restoreAllMocks(); }); it("Should dispatch get adsCandidates request when rendered", () => { render(cont); expect(mockDispatch).toHaveBeenCalledWith({ type: ADS_CANDIDATES_FETCH, payload: { currentPage: 0, employmentType: "", maxDateOfApplication: "", maxExperience: 0, minDateOfApplication: "", minExperience: 0, pageSize: 0, technologies: [], }, }); }); it("should load and handle adsCandidates in case of success", async () => { const dispatchedActions = []; const mockedCall = { data: mockState.technologies.technologies }; api.getFilteredAdsCandidates = jest.fn(() => Promise.resolve(mockedCall)); const fakeStore = { getState: () => mockState.candidates.adsCandidates, dispatch: (action) => dispatchedActions.push(action), }; await runSaga(fakeStore, getAdsCandidates, {}).done; expect(api.getFilteredAdsCandidates.mock.calls.length).toBe(1); expect(dispatchedActions[0].payload).toBe( fetchAdsCandidates(mockedCall.data).payload ); }); // it("should handle adsCandidates load errors in case of failure", async () => { // const dispatchedActions = []; // const error = { // response: { // data: { message: mockState.candidates.fetchCandidatesErrorMessage }, // }, // }; // api.getFilteredAdsCandidates = jest.fn(() => Promise.reject(error)); // const fakeStore = { // getState: () => mockState.candidates.adsCandidates, // dispatch: (action) => dispatchedActions.push(action), // }; // await runSaga(fakeStore, getAdsCandidates,{}).done; // expect(api.getFilteredAdsCandidates.mock.calls.length).toBe(1); // expect(dispatchedActions).toContainEqual( // fetchAdsCandidates(error.response.data.message) // ); // }); });