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

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

  it("forwards modelValue to the input element", () => {
    const wrapper = mount(EmailInput, { props: { modelValue: "a@b.com" } })
    expect((wrapper.find("input").element as HTMLInputElement).value).toBe("a@b.com")
  })

  it("emits update:modelValue when the user types", async () => {
    const wrapper = mount(EmailInput, { props: { modelValue: "" } })
    await wrapper.find("input").setValue("you@example.com")
    const emitted = wrapper.emitted("update:modelValue")
    expect(emitted).toBeTruthy()
    expect(emitted![emitted!.length - 1]).toEqual(["you@example.com"])
  })

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

  it("forwards maxlength and minlength", () => {
    const wrapper = mount(EmailInput, {
      props: { maxlength: 100, minlength: 5 },
    })
    const el = wrapper.find("input").element as HTMLInputElement
    expect(el.maxLength).toBe(100)
    expect(el.minLength).toBe(5)
  })

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