import React from 'react';
import { describe, it, expect } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { Select } from '@/components/ui/Select';

const options = [
  { value: 'de', label: 'Deutsch' },
  { value: 'en', label: 'English' },
];

describe('Select', () => {
  it('renders label and options', () => {
    render(<Select label="Sprache" options={options} />);
    expect(screen.getByText('Sprache')).toBeInTheDocument();
    expect(screen.getByText('Deutsch')).toBeInTheDocument();
    expect(screen.getByText('English')).toBeInTheDocument();
  });

  it('renders error with role=alert', () => {
    render(<Select label="Sprache" options={options} error="Bitte auswählen" />);
    expect(screen.getByRole('alert')).toHaveTextContent('Bitte auswählen');
  });

  it('renders helper text', () => {
    render(<Select label="Sprache" options={options} helperText="Wählen Sie eine Sprache" />);
    expect(screen.getByText('Wählen Sie eine Sprache')).toBeInTheDocument();
  });

  it('calls onChange when value changes', () => {
    const onChange = vi.fn();
    render(<Select label="Sprache" options={options} onChange={onChange} />);
    fireEvent.change(screen.getByLabelText(/Sprache/), { target: { value: 'en' } });
    expect(onChange).toHaveBeenCalled();
  });

  it('has min-h-touch for 44px target', () => {
    render(<Select label="Sprache" options={options} />);
    expect(screen.getByLabelText(/Sprache/).className).toContain('min-h-touch');
  });

  it('sets aria-required when required', () => {
    render(<Select label="Sprache" options={options} required />);
    expect(screen.getByLabelText(/Sprache/)).toHaveAttribute('aria-required', 'true');
  });
});
