import { composeStories } from "@storybook/react"
import { fireEvent, screen } from "@testing-library/react"
import { userEvent } from "@testing-library/user-event"
import React from "react"
import { expect, describe, test } from "vitest"
import { render } from "../../../tests/render"
import * as stories from "./SideSheet.stories"

const { Uncontrolled, Controlled } = composeStories(stories)

describe("<SideSheet />", () => {
  test("Uncontrolled", async () => {
    render(<Uncontrolled />)

    await userEvent.click(screen.getByText("Open sheet"))

    expect(await screen.findByRole("dialog")).toBeInTheDocument()
    expect(screen.getByText("Title content")).toBeInTheDocument()
    expect(screen.getByText("Body content")).toBeInTheDocument()
  })

  test("closes on overlay click", async () => {
    render(<Uncontrolled />)

    await userEvent.click(screen.getByText("Open sheet"))

    expect(await screen.findByRole("dialog")).toBeInTheDocument()
    expect(screen.getByText("Title content")).toBeInTheDocument()

    await userEvent.click(screen.getByTestId("Overlay"))

    expect(screen.getByRole("dialog")).toHaveAttribute("data-state", "closed")
  })

  test("closes on close icon click", async () => {
    render(<Uncontrolled />)

    await userEvent.click(screen.getByText("Open sheet"))

    expect(await screen.findByRole("dialog")).toBeInTheDocument()
    expect(screen.getByText("Title content")).toBeInTheDocument()

    await fireEvent.click(screen.getByRole("button", { name: "Close" }))

    expect(screen.getByRole("dialog")).toHaveAttribute("data-state", "closed")
  })

  test("Controlled", async () => {
    render(<Controlled />)

    await userEvent.click(screen.getByText("Open sheet"))

    expect(await screen.findByRole("dialog")).toBeInTheDocument()
    expect(screen.getByText("Title content")).toBeInTheDocument()

    await userEvent.click(screen.getByTestId("Overlay"))
    expect(screen.getByRole("dialog")).toHaveAttribute("data-state", "closed")
  })
})
