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 CandidatesPage from "../../pages/CandidatesPage/CandidatesPage";
import * as api from "../../request/technologiesRequest";
import { runSaga } from "redux-saga";
import { FETCH_TECHNOLOGIES_REQ } from "../../store/actions/technologies/technologiesActionConstants";
import { getTechnologies } from "../../store/saga/technologiesSaga";
import {
  setTechnologies,
  setTechnologiesError,
} from "../../store/actions/technologies/technologiesActions";
import * as helper from "../../util/helpers/rejectErrorCodeHelper";

describe("CandidatesPage render tests", () => {
  const cont = (
    <redux.Provider store={store}>
      <Router history={history}>
        <CandidatesPage />
      </Router>
    </redux.Provider>
  );

  let spyOnUseSelector;
  let spyOnUseDispatch;
  let mockDispatch;

  beforeEach(() => {
    spyOnUseSelector = jest.spyOn(redux, "useSelector");
    spyOnUseSelector.mockReturnValueOnce(mockState.technologies.technologies);
    spyOnUseDispatch = jest.spyOn(redux, "useDispatch");

    mockDispatch = jest.fn();
    spyOnUseDispatch.mockReturnValue(mockDispatch);
  });

  afterEach(() => {
    jest.restoreAllMocks();
  });

  it("Should dispatch get technologies request when rendered", () => {
    render(cont);
    expect(mockDispatch).toHaveBeenCalledWith({
      type: FETCH_TECHNOLOGIES_REQ,
    });
  });

  it("should load and handle techonologies in case of success", async () => {
    const dispatchedActions = [];

    const mockedCall = { data: mockState.technologies.technologies };
    api.getAllTechnologies = jest.fn(() => Promise.resolve(mockedCall));

    const fakeStore = {
      getState: () => mockState.technologies.technologies,
      dispatch: (action) => dispatchedActions.push(action),
    };

    await runSaga(fakeStore, getTechnologies).done;
    expect(api.getAllTechnologies.mock.calls.length).toBe(1);
    expect(dispatchedActions).toContainEqual(setTechnologies(mockedCall.data));
  });

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

    helper.rejectErrorCodeHelper = jest.fn(() =>  mockState.candidates.fetchCandidatesErrorMessage);

    const error = {
      response: {
        data: { message:  mockState.candidates.fetchCandidatesErrorMessage },
      },
    };
    api.getAllTechnologies = jest.fn(() => Promise.reject(error));

    const fakeStore = {
      getState: () => mockState.technologies.technologies,
      dispatch: (action) => dispatchedActions.push(action),
    };

    await runSaga(fakeStore, getTechnologies).done;

    expect(api.getAllTechnologies.mock.calls.length).toBe(1);
    expect(dispatchedActions).toContainEqual(
      setTechnologiesError(error.response.data.message)
    );
  });
});
