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

describe('withId', () => {
  type ButtonProps = {
    id?: string;
  };
  const Button = withId<ButtonProps>((props) => (
    <button id={props.id} type="button">
      click me
    </button>
  ));

  it('should provide id', () => {
    render(<Button />);
    expect(screen.getByRole('button').getAttribute('id')).toBeTruthy();
  });

  it('should respect existing id', () => {
    render(<Button id="myId" />);
    expect(screen.getByRole('button')).toHaveAttribute('id', 'myId');
  });

  it('should treat an empty string id as no id', () => {
    render(<Button id="" />);
    expect(screen.getByRole('button').getAttribute('id')).toBeTruthy();
  });
});
