import React from 'react';
import { cleanup, fireEvent } from '@testing-library/react';
import { render } from '../../../utils/theme-render-wrapper';

import { Textarea } from './textarea';

afterEach(cleanup);

const changeFn = jest.fn();

describe('<Textarea />', () => {
  it('should render successfully', () => {
    const { baseElement } = render(<Textarea />);
    expect(baseElement).toBeTruthy();
  });

  it('should render and change text', () => {
    const { container } = render(<Textarea onChange={changeFn} />);
    const textareaEl = container.querySelector('textarea');
    textareaEl &&
      fireEvent.change(textareaEl, { target: { value: 'Test' } }) &&
      fireEvent.keyDown(textareaEl, { key: 'Enter' });
    expect(changeFn).toBeCalled();
  });

  it('should render with autoHeight enabled', () => {
    const useRefSpy = jest.spyOn(React, 'useRef').mockReturnValueOnce({
      current: document.createElement('textarea')
    });
    render(<Textarea autoHeight minRows={5} maxRows={10} value="Test" />);
    expect(useRefSpy).toBeCalled();
  });
});
