import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { Label } from './Label'

describe('Label', () => {
  it('renders label text', () => {
    render(<Label>Email address</Label>)
    expect(screen.getByText('Email address')).toBeInTheDocument()
  })

  it('does not show asterisk when required is not set', () => {
    render(<Label>Username</Label>)
    expect(screen.queryByText('*')).not.toBeInTheDocument()
  })

  it('shows asterisk when required', () => {
    render(<Label required>Password</Label>)
    expect(screen.getByText('*')).toBeInTheDocument()
  })

  it('passes htmlFor to the label element', () => {
    render(<Label htmlFor="email-input">Email</Label>)
    const label = screen.getByText('Email')
    expect(label).toHaveAttribute('for', 'email-input')
  })

  it('forwards ref to the label element', () => {
    let ref: HTMLLabelElement | null = null
    render(
      <Label ref={(el) => { ref = el }}>Ref test</Label>
    )
    expect(ref).toBeInstanceOf(HTMLLabelElement)
  })
})
