| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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 = (
- <redux.Provider store={store}>
- <Router history={history}>
- <AdsCandidatesPage search="" />
- </Router>
- </redux.Provider>
- );
-
- 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)
- // );
- // });
- });
|