import { render, screen } from '../../test-utils';
import { ListItem, type ListItemProps } from '../ListItem';

describe('ListItem.AdditionalInfo', () => {
  const renderWithInfo = (info: ListItemProps['additionalInfo']) =>
    render(<ListItem title="Test title" subtitle="Test subtitle" additionalInfo={info} />);

  it('renders children content', () => {
    renderWithInfo(
      <ListItem.AdditionalInfo>Additional information content</ListItem.AdditionalInfo>,
    );
    expect(screen.getByText('Additional information content')).toBeInTheDocument();
  });

  it('renders action button when onClick provided', () => {
    const handleActionClick = jest.fn();
    renderWithInfo(
      <ListItem.AdditionalInfo action={{ label: 'Learn more', onClick: handleActionClick }}>
        Additional info
      </ListItem.AdditionalInfo>,
    );

    expect(screen.getByRole('button', { name: 'Learn more' })).toBeInTheDocument();
  });

  it('renders action link with href when provided', () => {
    renderWithInfo(
      <ListItem.AdditionalInfo action={{ label: 'Learn more', href: '/learn' }}>
        Additional info
      </ListItem.AdditionalInfo>,
    );

    const actionLink = screen.getByRole('link', { name: 'Learn more' });
    expect(actionLink).toBeInTheDocument();
    expect(actionLink).toHaveAttribute('href', '/learn');
  });

  it('renders action link with target when provided', () => {
    renderWithInfo(
      <ListItem.AdditionalInfo action={{ label: 'Learn more', href: '/learn', target: '_blank' }}>
        Additional info
      </ListItem.AdditionalInfo>,
    );

    const actionLink = screen.getByRole('link', { name: 'Learn more (opens in new tab)' });
    expect(actionLink).toBeInTheDocument();
    expect(actionLink).toHaveAttribute('target', '_blank');
  });

  it('does not render action when not provided', () => {
    renderWithInfo(<ListItem.AdditionalInfo>Additional info only</ListItem.AdditionalInfo>);

    expect(screen.queryByRole('link')).not.toBeInTheDocument();
    expect(screen.queryByRole('button')).not.toBeInTheDocument();
  });
});
