import { render, screen } from '@testing-library/react';
import { LiveRegion, LiveRegionProps } from './LiveRegion';

describe('LiveRegion', () => {
  const renderLiveRegion = (props: Partial<LiveRegionProps> & Pick<LiveRegionProps, 'aria-live'>) =>
    render(<LiveRegion {...props}>{props.children ?? 'Live content'}</LiveRegion>);

  describe('when aria-live is "polite"', () => {
    it('renders with role="status"', () => {
      renderLiveRegion({ 'aria-live': 'polite' });
      expect(screen.getByRole('status')).toBeInTheDocument();
    });

    it('sets aria-live="polite"', () => {
      renderLiveRegion({ 'aria-live': 'polite' });
      expect(screen.getByRole('status')).toHaveAttribute('aria-live', 'polite');
    });

    it('sets aria-atomic="true"', () => {
      renderLiveRegion({ 'aria-live': 'polite' });
      expect(screen.getByRole('status')).toHaveAttribute('aria-atomic', 'true');
    });
  });

  describe('when aria-live is "assertive"', () => {
    it('renders with role="alert"', () => {
      renderLiveRegion({ 'aria-live': 'assertive' });
      expect(screen.getByRole('alert')).toBeInTheDocument();
    });

    it('sets aria-live="assertive"', () => {
      renderLiveRegion({ 'aria-live': 'assertive' });
      expect(screen.getByRole('alert')).toHaveAttribute('aria-live', 'assertive');
    });

    it('sets aria-atomic="true"', () => {
      renderLiveRegion({ 'aria-live': 'assertive' });
      expect(screen.getByRole('alert')).toHaveAttribute('aria-atomic', 'true');
    });
  });

  it('renders children', () => {
    renderLiveRegion({ 'aria-live': 'polite', children: 'Transfer sent' });
    expect(screen.getByText('Transfer sent')).toBeInTheDocument();
  });

  it('passes additional HTML attributes to the wrapper div', () => {
    renderLiveRegion({ 'aria-live': 'polite', className: 'custom' });
    expect(screen.getByRole('status')).toHaveClass('custom');
  });

  it('supports data-testid prop', () => {
    renderLiveRegion({ 'aria-live': 'polite', 'data-testid': 'live-region' });
    expect(screen.getByTestId('live-region')).toBeInTheDocument();
  });
});
