import { screen, waitForElementToBeRemoved } from "@testing-library/react"
import React from "react"
import { mockAllIsIntersecting } from "react-intersection-observer/test-utils"
import { describe, test, vi, expect } from "vitest"
import { render } from "../../../tests/render"
import { ScrollingPaginator } from "./ScrollingPaginator"

describe("<ScrollingPaginator />", () => {
  test("simple pagination", async () => {
    const loadNext = vi.fn().mockImplementation(() => Promise.resolve())
    render(
      <ScrollingPaginator
        hasNext
        isLoadingNext={false}
        loadNext={loadNext}
        renderFallback={() => <div>Loading</div>}
      />,
    )

    mockAllIsIntersecting(true)

    expect(screen.getByText("Loading")).toBeInTheDocument()

    expect(loadNext).toHaveBeenCalled()

    mockAllIsIntersecting(false)

    await waitForElementToBeRemoved(() => screen.queryByText("Loading"))
  })
})
