import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import * as redux from "react-redux";
import SelectionProcessPage from "../../pages/selectionProcessPage/selectionProcessPage";
import store from "../../store";
import "../../i18n";
import { mockState } from "../../mockState";
import { Router } from "react-router-dom";
import history from "../../store/utils/history";
import { SelectionProvider } from "../../context/SelectionContext";

describe("SelectionProcessPage render tests", () => {
  var props = {
    history: {
      replace: jest.fn(),
      push: jest.fn(),
      location: {
        pathname: "/selectionFlow",
      },
    },
  };

  const cont = (
    <redux.Provider store={store}>
      <SelectionProvider>
        <Router history={history}>
          <SelectionProcessPage {...props} />
        </Router>
      </SelectionProvider>
    </redux.Provider>
  );

  let spyOnUseSelector;

  beforeEach(() => {
    // Mock useSelector hook
    spyOnUseSelector = jest.spyOn(redux, "useSelector");
    spyOnUseSelector
      .mockReturnValueOnce(mockState.selections)
      .mockReturnValueOnce(mockState.selections.processes)
      .mockReturnValueOnce(mockState.selections.statuses);
    // MOZDA VRATITI KANDIDATE i korisnike?
    // spyOnUseSelector.mockReturnValue(mockState.selections);
  });

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

  it("Should render", () => {
    render(cont);
    expect(screen.getByTestId("selections-page")).toBeDefined();
  });

  it("Should render a card foreach mocked level", () => {
    const { container } = render(cont);
    expect(container.getElementsByClassName("selection-card").length).toBe(4);
  });

  // it("Should render a button with specific class for an enabled user", () => {
  //   const { container } = render(cont);
  //   expect(container.getElementsByClassName("td-btn").length).toBe(0);
  // });

  it("Should render filter button", () => {
    const { container } = render(cont);
    expect(container.getElementsByClassName("userPageBtn").length).toBe(1);
  });

  it("Should render date if process is scheduled", () => {
    render(cont);
    expect(screen.getAllByTestId("process-date")[0]).toBeDefined();
  });

  it("Should render scheduler if process is scheduled", () => {
    const { container } = render(cont);
    expect(
      container.getElementsByClassName("sel-item-scheduler")[0]
    ).toBeDefined();
  });

  it("Should render interviewBtn", () => {
    render(cont);
    expect(screen.getByTestId("interview-image")).toBeDefined();
  });

  // it("Should render interview dailog when clicked on", () => {
  //   render(cont);
  //   fireEvent.click(screen.getByTestId("interview-image"));
  //   waitFor(() => expect(screen.getByTestId("interview-dialog")).toBeDefined());
  // });

  it("Should render selection card if process failed", () => {
    render(cont);
    expect(
      screen.getByRole("button", {
        name: /neuspešno/i,
      })
    ).toBeDefined();
  });

  it("Should navigate to applicant selection when status is clicked if status is 'failed'", () => {
    render(cont);

    var statusBtn = screen.getByRole("button", {
      name: /neuspešno/i,
    });

    fireEvent.click(statusBtn);
    // 21364 is the id of the applicant in mockState
    expect(props.history.push).toHaveBeenCalledWith("/selectionFlow/21364");
  });

  it("Should navigate to applicant selection when status is clicked if status is 'done'", () => {
    render(cont);

    var statusBtn = screen.getAllByRole("button", {
      name: /zakazan/i,
    })[0];

    fireEvent.click(statusBtn);

    expect(screen.getByTestId("status-select")).toBeDefined();
  });

  it("Should not render selection card if process is done", () => {
    render(cont);
    expect(
      screen.queryByRole("button", {
        name: /odrađen/i,
      })
    ).toBe(null);
  });

  it("Drag and drop", () => {
    const { container } = render(cont);
    fireEvent.drop(container.getElementsByClassName("selection-card")[0], {
      dataTransfer: {
        getData: (type) =>
          '{"id":32,"name":"random","status":"Kandidat primljen","date":null,"link":"link","applicant":{"applicantId":6,"firstName":"Safet","lastName":"Purkovic","position":"React Developer","dateOfApplication":"2021-05-05T00:00:00","cv":"dasdas","email":"safet@gmail.com","phoneNumber":"2313123","linkedlnLink":"sda","githubLink":null,"bitBucketLink":null,"experience":2,"applicationChannel":null,"typeOfEmployment":"Posao","technologyApplicants":[],"comments":[],"ads":[],"selectionProcesses":[{"status":"Kandidat primljen","date":null,"link":"link","scheduler":{"id":7,"firstName":"Safet","lastName":"Purkovic","email":"safet.purkovic@dilig.net","isEnabled":true},"selectionLevel":{"id":4,"name":"Konacna odluka"}}]},"selectionLevelId":4}',
      },
    });
  });
});
