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 {
  fetchAdsCandidatesError,
  fetchAdsCandidatesSuccess,
} from "../../store/actions/candidates/candidatesActions";
import AdsCandidatesPage from "../../pages/CandidatesPage/AdsCandidatesPage";
import * as helper from "../../util/helpers/rejectErrorCodeHelper";

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(() => {
    spyOnUseSelector = jest.spyOn(redux, "useSelector");
    spyOnUseSelector
      .mockReturnValueOnce(mockState.candidates.adsCandidates)
      .mockReturnValueOnce(mockState.candidates.adsCandidates);

    spyOnUseDispatch = jest.spyOn(redux, "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.candidates.adsCandidates };
    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).toContainEqual(
      fetchAdsCandidatesSuccess(mockedCall.data)
    );
  });

  it("should handle adsCandidates load errors in case of failure", async () => {
    const dispatchedActions = [];

    helper.rejectErrorCodeHelper = jest.fn(() =>  mockState.technologies.fetchTecnologiesErrorMessage);

    const error = {
      response: {
        data: { message: mockState.technologies.fetchTecnologiesErrorMessage },
      },
    };
    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(
      fetchAdsCandidatesError(error.response.data.message)
    );
  });
});
