import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { TextArea } from '../TextArea';
import { ValidatedOptions } from '../../../helpers/constants';
const props = {
onChange: jest.fn(),
value: 'test textarea'
};
test('Textarea input passes value and event to onChange handler', async () => {
const user = userEvent.setup();
render();
await user.type(screen.getByRole('textbox'), 'a');
expect(props.onChange).toHaveBeenCalledWith(expect.any(Object), 'a');
});
test('Renders simple text input', () => {
render(
);
expect(screen.getByTestId('textarea').firstChild).toBeVisible();
});
test('Renders with custom class passed', () => {
render();
expect(screen.getByRole('textbox').parentElement).toHaveClass('test-class');
});
test('Renders text area with required attribute using isRequired', () => {
render();
expect(screen.getByRole('textbox')).toBeRequired();
});
test('Text area is not required by default', () => {
render();
expect(screen.getByRole('textbox')).not.toBeRequired();
});
test('Renders disabled text area using isDisabled', () => {
render();
expect(screen.getByRole('textbox')).toBeDisabled();
});
test('Text area is not disabled by default', () => {
render();
expect(screen.getByRole('textbox')).not.toBeDisabled();
});
test('Renders read only text area using readOnlyVariant', () => {
render();
expect(screen.getByRole('textbox')).toHaveAttribute('readonly');
});
test('Text area is not read only by default', () => {
render();
expect(screen.getByRole('textbox')).not.toHaveAttribute('readonly');
});
test('Renders text area with default class name only', () => {
render();
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-v5-c-form-control');
});
test('Renders validated text area with success className', () => {
render();
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-success');
});
test('Renders validated text area with warning className', () => {
render();
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-warning');
});
test('Renders invalid text area', () => {
render();
expect(screen.getByRole('textbox')).toBeInvalid();
});
test('Text area is not invalid by default', () => {
render();
expect(screen.getByRole('textbox')).not.toBeInvalid();
});
test('Renders vertically resizable text area', () => {
render();
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-resize-vertical');
});
test('Renders horizontally resizable text area', () => {
render();
expect(screen.getByRole('textbox').parentElement).toHaveClass('pf-m-resize-horizontal');
});
test('Throws console error when no aria-label or id is given', () => {
jest.spyOn(global.console, 'error');
render();
expect(console.error).toHaveBeenCalled();
});
test('Does not throw console error when id is given but no aria-label', () => {
jest.spyOn(global.console, 'error');
render();
expect(console.error).not.toHaveBeenCalled();
});
test('Does not throw console error when aria-label is given but no id', () => {
jest.spyOn(global.console, 'error');
render();
expect(console.error).not.toHaveBeenCalled();
});
test('TextArea can be accessed via passed ref', () => {
const testRef: React.RefObject = React.createRef();
render();
global.scrollTo = jest.fn();
testRef.current?.focus();
expect(screen.getByRole('textbox')).toHaveFocus();
});
test('Matches the snapshot', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});