import * as redux from "react-redux";
import store from "../../store";
import { Router } from "react-router-dom";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import history from "../../store/utils/history";
import ResetPasswordPage from "../../pages/ForgotPasswordPage/ResetPasswordPageMUI";

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

  let spyOnUseDispatch;
  let mockDispatch;

  beforeEach(() => {
    spyOnUseDispatch = jest.spyOn(redux, "useDispatch");
    mockDispatch = jest.fn();
    spyOnUseDispatch.mockReturnValue(mockDispatch);
  });

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

  it("Should render", () => {
    const { container } = render(cont);
    expect(
      container.getElementsByClassName("c-login-container")[0]
    ).toBeDefined();
  });

  it("Should render login logo", () => {
    const { container } = render(cont);
    expect(container.getElementsByClassName("login-logo")[0]).toBeDefined();
  });

  it("Should render header", () => {
    const { container } = render(cont);
    expect(
      container
        .getElementsByClassName("c-login-container")[0]
        .querySelector("h")
    ).toBeDefined();
  });

  it("Should render paragraph", () => {
    const { container } = render(cont);
    expect(
      container
        .getElementsByClassName("c-login-container")[0]
        .querySelector("p")
    ).toBeDefined();
  });

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

  it("Should render confirm password input", () => {
    render(cont);
    expect(screen.getByTestId("confirm-password-input")).toBeDefined();
  });

  it("Should render submit button", () => {
    const { container } = render(cont);
    expect(container.getElementsByClassName("c-btn")[0]).toBeDefined();
  });

  it("Should render link for going to login page", () => {
    render(cont);
    expect(screen.getByTestId("back-link")).toBeDefined();
  });

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

  it("Should not dispatch function because input for password is empty", async () => {
    const { container } = render(cont);
    fireEvent.change(screen.getByTestId("confirm-password-input"), {
      target: { value: "nekaSifra" },
    });
    fireEvent.click(container.getElementsByClassName("c-btn")[0]);
    await waitFor(() => expect(mockDispatch).toBeCalledTimes(0));
  });

  it("Should not dispatch function because input for confirm password is empty", async () => {
    const { container } = render(cont);
    fireEvent.change(screen.getByTestId("password-input"), {
      target: { value: "nekaSifra" },
    });
    fireEvent.click(container.getElementsByClassName("c-btn")[0]);
    await waitFor(() => expect(mockDispatch).toBeCalledTimes(0));
  });

  it("Should not dispatch function because paswwords doesn't match", async () => {
    const { container } = render(cont);
    fireEvent.change(screen.getByTestId("password-input"), {
      target: { value: "nekaSifra" },
    });
    fireEvent.change(screen.getByTestId("confirm-password-input"), {
      target: { value: "nekaDrugaSifra" },
    });
    fireEvent.click(container.getElementsByClassName("c-btn")[0]);
    await waitFor(() => expect(mockDispatch).toBeCalledTimes(0));
  });

  it("Should dispatch function because no input is empty and passwords match", async () => {
    const { container } = render(cont);
    fireEvent.change(screen.getByTestId("password-input"), {
      target: { value: "nekaSifra" },
    });
    fireEvent.change(screen.getByTestId("confirm-password-input"), {
      target: { value: "nekaSifra" },
    });
    fireEvent.click(container.getElementsByClassName("c-btn")[0]);
    await waitFor(() => expect(mockDispatch).toBeCalledTimes(1));
  });
});
