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

describe("UrlInput (props-based composition)", () => {
  it("mounts and renders the label via FieldFrame", () => {
    const wrapper = mount(UrlInput, { props: { label: "Website" } })
    expect(wrapper.find("input").exists()).toBe(true)
    expect(wrapper.find("input").attributes("type")).toBe("url")
    expect(wrapper.text()).toContain("Website")
  })

  it("forwards modelValue", () => {
    const wrapper = mount(UrlInput, {
      props: { modelValue: "https://example.com" },
    })
    expect((wrapper.find("input").element as HTMLInputElement).value).toBe(
      "https://example.com",
    )
  })

  it("emits update:modelValue", async () => {
    const wrapper = mount(UrlInput, { props: { modelValue: "" } })
    await wrapper.find("input").setValue("https://gramercytech.com")
    const emitted = wrapper.emitted("update:modelValue")
    expect(emitted).toBeTruthy()
    expect(emitted![emitted!.length - 1]).toEqual(["https://gramercytech.com"])
  })

  it("renders errors", () => {
    const wrapper = mount(UrlInput, { props: { errors: ["Invalid URL"] } })
    expect(wrapper.find("[data-testid='field-frame-errors']").exists()).toBe(true)
    expect(wrapper.find("input").classes()).toContain("border-destructive")
  })

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