import { fireEvent, render, screen } from '@testing-library/react';
import { Textarea } from './Textarea';
import styles from './Textarea.module.scss';
describe('textarea tests', () => {
it('renders textarea', () => {
render();
const textarea = screen.getByRole('textbox');
expect(textarea).toBeInTheDocument();
});
it('renders textarea with label', () => {
const { rerender } = render();
const textarea = screen.getByLabelText('test label');
expect(textarea).toBeInTheDocument();
rerender();
const inputWrapper = screen.getByTestId('inputWrapper');
expect(inputWrapper).not.toHaveClass(styles.x);
expect(inputWrapper).toHaveClass(styles.y);
rerender();
expect(inputWrapper).toHaveStyle('grid-template-columns: 7fr 9fr');
rerender();
const tooltipWrapper = screen.getByTestId('tooltipWrapper');
const tooltipIcon = screen.getByTestId('tooltipIcon');
expect(tooltipIcon).toBeInTheDocument();
expect(screen.queryByText('random tooltip')).not.toBeInTheDocument();
expect(tooltipWrapper).toHaveAttribute('data-tooltip', 'random tooltip');
});
it('applies className to wrapper', () => {
render();
const inputWrapper = screen.getByTestId('inputWrapper');
const textarea = screen.getByRole('textbox');
expect(inputWrapper).toHaveClass('className');
expect(textarea).not.toHaveClass('className');
});
it('renders read only textarea', () => {
render();
const wrapper = screen.getByTestId('wrapper');
const input = screen.getByRole('textbox');
expect(wrapper).toHaveClass(styles.readOnly);
expect(input).toHaveAttribute('readOnly');
});
it('renders disabled textarea', () => {
render();
const inputWrapper = screen.getByTestId('inputWrapper');
const textarea = screen.getByRole('textbox');
expect(inputWrapper).toHaveClass('disabled');
expect(textarea).toBeDisabled();
});
it('passes rows attribute', () => {
render();
const textarea = screen.getByRole('textbox');
expect(textarea).toHaveAttribute('rows', '10');
});
it('enters text and clicks clear button', () => {
const handleChange = jest.fn();
const handleFocus = jest.fn();
const handleBlur = jest.fn();
render(
,
);
const inputWrapper = screen.getByTestId('inputWrapper');
const textarea = screen.getByRole('textbox');
expect(inputWrapper).not.toContainElement(screen.queryByRole('button'));
fireEvent.focus(textarea);
const clearButton = screen.getByRole('button');
expect(inputWrapper).toContainElement(clearButton);
fireEvent.change(textarea, { target: { value: '123' } });
expect(textarea).toHaveValue('123');
fireEvent.click(clearButton);
expect(textarea).toHaveValue('');
expect(inputWrapper).toContainElement(clearButton);
fireEvent.blur(textarea);
expect(inputWrapper).not.toContainElement(clearButton);
expect(handleChange).toBeCalledTimes(2);
expect(handleBlur).toBeCalledTimes(1);
expect(handleFocus).toBeCalledTimes(1);
});
it('renders large textarea with light color', () => {
render();
const wrapper = screen.getByTestId('wrapper');
expect(wrapper).toHaveClass(styles.light, styles.large);
expect(wrapper).not.toHaveClass(styles.dark, styles.normal);
});
it('renders textarea with error', () => {
render();
const inputWrapper = screen.getByTestId('inputWrapper');
const error = screen.getByText('random error');
expect(inputWrapper).toContainElement(error);
});
it('passes ref', () => {
const ref = { current: null };
render();
const textarea = screen.getByRole('textbox');
expect(ref.current).toBe(textarea);
});
it('renders block textarea', () => {
render();
const wrapper = screen.getByTestId('wrapper');
expect(wrapper).toHaveClass(styles.block);
});
});