import { describe, expect, it } from "vitest"
import { mount } from "@vue/test-utils"
import LinkAction from "./LinkAction.vue"

describe("LinkAction (props-based composition)", () => {
  it("mounts and renders the label", () => {
    const wrapper = mount(LinkAction, { props: { label: "More" } })
    expect(wrapper.text()).toContain("More")
  })

  it("renders as a button when no href is provided", () => {
    const wrapper = mount(LinkAction, { props: { label: "Refresh" } })
    expect(wrapper.find("button").exists()).toBe(true)
    expect(wrapper.find("a").exists()).toBe(false)
  })

  it("renders as an anchor when href is provided", () => {
    const wrapper = mount(LinkAction, {
      props: { label: "Docs", href: "https://example.com", target: "_blank" },
    })
    const a = wrapper.find("a")
    expect(a.exists()).toBe(true)
    expect(a.attributes("href")).toBe("https://example.com")
    expect(a.attributes("target")).toBe("_blank")
  })

  it("emits a click event", async () => {
    const wrapper = mount(LinkAction, { props: { label: "X" } })
    await wrapper.find("button").trigger("click")
    expect(wrapper.emitted("click")).toBeTruthy()
  })

  it("emits a named action when emit is set and href is not", async () => {
    const wrapper = mount(LinkAction, {
      props: { label: "Refresh", emit: "refresh" },
    })
    await wrapper.find("button").trigger("click")
    const emitted = wrapper.emitted("action")
    expect(emitted).toBeTruthy()
    expect(emitted![0]).toEqual([{ name: "refresh" }])
  })

  it("respects disabled", () => {
    const wrapper = mount(LinkAction, {
      props: { label: "Off", disabled: true },
    })
    expect(
      (wrapper.find("button").element as HTMLButtonElement).disabled,
    ).toBe(true)
  })

  it("works without any props at all", () => {
    const wrapper = mount(LinkAction)
    expect(wrapper.find("button").exists()).toBe(true)
  })
})
