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

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

  it("renders the leading search icon", () => {
    const wrapper = mount(SearchInput, { props: { label: "Search" } })
    expect(wrapper.find("svg").exists()).toBe(true)
  })

  it("forwards modelValue", () => {
    const wrapper = mount(SearchInput, { props: { modelValue: "kittens" } })
    expect((wrapper.find("input").element as HTMLInputElement).value).toBe("kittens")
  })

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

  it("applies pl-9 to the input to leave room for the icon", () => {
    const wrapper = mount(SearchInput)
    expect(wrapper.find("input").classes()).toContain("pl-9")
  })

  it("renders errors", () => {
    const wrapper = mount(SearchInput, { props: { errors: ["Bad search"] } })
    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(SearchInput)
    expect(wrapper.find("input").exists()).toBe(true)
  })
})
