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

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

  it("forwards modelValue", () => {
    const wrapper = mount(TelInput, { props: { modelValue: "555-123-4567" } })
    expect((wrapper.find("input").element as HTMLInputElement).value).toBe(
      "555-123-4567",
    )
  })

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

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