import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { render, fireEvent } from "@testing-library/react";
import Pagination from "../pagination";

test("shows default pagination", () => {
  const { queryByTestId } = render(<Pagination itemCount={50} currentPageIndex={0} />);

  expect(queryByTestId("honeyui-pagination")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-0")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-1")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-2")).toBeTruthy();
  // With page size 20 3rd item should be null
  expect(queryByTestId("honeyui-pagination-item-3")).toBeFalsy();
});

test("shows with different page size", () => {
  const { queryByTestId } = render(
    <Pagination itemCount={50} pageSize={10} currentPageIndex={0} />
  );

  expect(queryByTestId("honeyui-pagination-item-0")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-1")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-2")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-3")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-4")).toBeTruthy();
  expect(queryByTestId("honeyui-pagination-item-5")).toBeFalsy();
});

test("shows with custom class name", () => {
  const { queryByTestId } = render(
    <Pagination itemCount={50} className="custom-class" currentPageIndex={0} />
  );
  expect(queryByTestId("honeyui-pagination-list")).toHaveClass("custom-class");
});

test("calls onPageChange on page change", () => {
  const onPageChange = jest.fn();
  const { queryByTestId } = render(
    <Pagination itemCount={50} currentPageIndex={0} onPageChange={onPageChange} />
  );

  expect(onPageChange).toHaveBeenCalledTimes(0);
  fireEvent.click(queryByTestId("honeyui-pagination-item-1-link"));
  expect(onPageChange).toHaveBeenCalledTimes(1);
  expect(onPageChange).lastCalledWith(1);
});

test("calls onPageChange on page next", () => {
  const onPageChange = jest.fn();
  const { queryByTestId } = render(
    <Pagination itemCount={50} currentPageIndex={0} onPageChange={onPageChange} />
  );

  expect(onPageChange).toHaveBeenCalledTimes(0);
  fireEvent.click(queryByTestId("honeyui-pagination-item-forward-link"));
  expect(onPageChange).toHaveBeenCalledTimes(1);
  expect(onPageChange).lastCalledWith(1);
});

test("calls onPageChange on page next", () => {
  const onPageChange = jest.fn();
  const { queryByTestId } = render(
    <Pagination itemCount={50} currentPageIndex={1} onPageChange={onPageChange} />
  );

  expect(onPageChange).toHaveBeenCalledTimes(0);
  fireEvent.click(queryByTestId("honeyui-pagination-item-back-link"));
  expect(onPageChange).toHaveBeenCalledTimes(1);
  expect(onPageChange).lastCalledWith(0);
});
