import React from "react"
import { render, fireEvent, screen } from "@testing-library/react"
import "@testing-library/jest-dom/extend-expect"
import Popover, { PopoverButton, PopoverContent } from "../"

describe("Popover", () => {
  it("runs onClose function", () => {
    const handleClose = jest.fn()

    render(
      <Popover label="Popover test">
        <PopoverButton>Toggle</PopoverButton>
        <PopoverContent onClose={handleClose} tabIndex={0}>
          Content
        </PopoverContent>
      </Popover>
    )

    const button = screen.getByText("Toggle")

    expect(handleClose).not.toHaveBeenCalled()
    fireEvent.click(button)
    expect(handleClose).not.toHaveBeenCalled()
    fireEvent.click(button)
    expect(handleClose).toHaveBeenCalledTimes(1)
  })
})
