import React from 'react';
import {fireEvent, render} from '@shared/testingLibrary';

import {AIButton} from './index';

describe('AIButton component', () => {
  it('renders label text in idle state', () => {
    const {getByText} = render(<AIButton text="Ask AI" />);

    expect(getByText('Ask AI')).toBeTruthy();
  });

  it('hides label and sets busy accessibility state when in progress', () => {
    const {queryByText, getByRole} = render(
      <AIButton text="Ask AI" isInProgress />,
    );

    expect(queryByText('Ask AI')).toBeNull();
    expect(getByRole('button').props.accessibilityState).toMatchObject({
      busy: true,
    });
  });

  it('does not call onPress when disabled', () => {
    const onPress = jest.fn();
    const {getByRole} = render(
      <AIButton text="Ask AI" disabled onPress={onPress} />,
    );

    fireEvent.press(getByRole('button'));

    expect(onPress).not.toHaveBeenCalled();
  });

  it('calls onPress when enabled', () => {
    const onPress = jest.fn();
    const {getByRole} = render(<AIButton text="Ask AI" onPress={onPress} />);

    fireEvent.press(getByRole('button'));

    expect(onPress).toHaveBeenCalledTimes(1);
  });

  it('uses label text as accessibility label in idle state', () => {
    const {getByRole} = render(<AIButton text="Ask AI" />);

    expect(getByRole('button').props.accessibilityLabel).toBe('Ask AI');
  });
});
