import React from 'react';
import { fireEvent } from '@testing-library/react';

import renderWithTheme from '../../../../testUtils/renderWithTheme';
import ActionButtons from '../index';

describe('ActionButtons', () => {
  it('renders correctly', () => {
    const { getByText } = renderWithTheme(
      <ActionButtons
        buttons={[
          { icon: 'add', text: 'Add', role: 'primary' },
          { text: 'Secondary' },
        ]}
      />
    );

    expect(getByText('Add')).toBeVisible();
    expect(getByText('Secondary')).toBeVisible();
  });

  it('passes onClick to rendered buttons', () => {
    const clickHandler = jest.fn();
    const { getByText } = renderWithTheme(
      <ActionButtons
        buttons={[
          { icon: 'add', text: 'Add', role: 'primary', onClick: clickHandler },
        ]}
      />
    );

    fireEvent.click(getByText('Add'));
    expect(clickHandler).toBeCalled();
  });

  it('throws error when there is no buttons', () => {
    expect(() => renderWithTheme(<ActionButtons buttons={[]} />)).toThrow(
      '[ActionButtons] Need at least 1 button to render'
    );
  });

  it('throws error when there are more than 4 buttons', () => {
    expect(() =>
      renderWithTheme(
        <ActionButtons
          buttons={[
            { text: 'Test', role: 'primary' },
            { text: 'Test' },
            { text: 'Test' },
            { text: 'Test' },
            { text: 'Test' },
          ]}
        />
      )
    ).toThrow('[ActionButtons] Maximum 4 buttons could be rendered');
  });

  it('throws error when there is no primary buttons', () => {
    expect(() =>
      renderWithTheme(<ActionButtons buttons={[{ text: 'Test' }]} />)
    ).toThrow('[ActionButtons] Need to have exactly 1 primary button');
  });

  it('throws error when there are more than 1 primary buttons', () => {
    expect(() =>
      renderWithTheme(
        <ActionButtons
          buttons={[
            { text: 'Test', role: 'primary' },
            { text: 'Test', role: 'primary' },
            { text: 'Test' },
          ]}
        />
      )
    ).toThrow('[ActionButtons] Need to have exactly 1 primary button');
  });
});
