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

describe('PrimitiveAnchor', () => {
  const defaultProps = {
    children: 'Click me',
    href: 'https://example.com',
  };

  const renderAnchor = (props?: Partial<typeof defaultProps>) => {
    return render(<PrimitiveAnchor {...defaultProps} {...props} />);
  };

  it('renders an anchor by default', () => {
    renderAnchor();
    expect(screen.getByRole('link')).toBeInTheDocument();
    expect(screen.getByRole('link')).toHaveTextContent('Click me');
    expect(screen.getByRole('link')).toHaveAttribute('href', 'https://example.com');
  });

  it('applies the correct classes based on props', () => {
    const props = {
      ...defaultProps,
      className: 'custom-class',
    };

    renderAnchor(props);

    const link = screen.getByRole('link');
    expect(link).toHaveClass('wds-Anchor');
    expect(link).toHaveClass('custom-class');
  });

  it('disables the anchor when isDisabled is true', () => {
    const props = {
      ...defaultProps,
      disabled: true,
    };

    renderAnchor(props);

    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('aria-disabled', 'true');
    expect(link).toHaveClass('wds-Anchor--disabled');
    expect(link).not.toHaveAttribute('href', 'https://example.com');
  });

  it('sets data-testid attribute', () => {
    const props = {
      ...defaultProps,
      testId: 'custom-id',
    };

    renderAnchor(props);

    const link = screen.getByTestId('custom-id');
    expect(link).toBeInTheDocument();
  });

  it('sets the target attribute to _blank', () => {
    const props = {
      ...defaultProps,
      target: '_blank',
    };

    renderAnchor(props);

    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('target', '_blank');
  });

  it('sets the rel attribute to noopener noreferrer', () => {
    const props = {
      ...defaultProps,
      rel: 'noopener noreferrer',
    };

    renderAnchor(props);

    const link = screen.getByRole('link');
    expect(link).toHaveAttribute('rel', 'noopener noreferrer');
  });

  it('applies custom styles', () => {
    const props = {
      ...defaultProps,
      style: { color: 'red' },
    };

    renderAnchor(props);

    const link = screen.getByRole('link');
    expect(link).toHaveStyle('color: red');
  });
});
