Skip to content

Commit

Permalink
test: textArea add tests KER-346
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkojamG committed Oct 31, 2024
1 parent 08a5f2f commit 844c11f
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/components/forms/__tests__/TextArea.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable sonarjs/no-duplicate-string */
import React from 'react';
import { fireEvent } from '@testing-library/react';

import TextArea from '../TextArea';
import renderWithProviders from '../../../utils/renderWithProviders';

const renderComponent = (props) => renderWithProviders(<TextArea {...props} />);

describe('<TextArea />', () => {
it('renders correctly', () => {
renderComponent({ value: 'Initial Value', labelId: 'label.test' });
});

it('calls onBlur function when blurred', () => {
const onBlurMock = jest.fn();

const { getByRole } = renderComponent({ onBlur: onBlurMock, labelId: 'label.test' });

const textArea = getByRole('textbox');

fireEvent.blur(textArea);

expect(onBlurMock).toHaveBeenCalled();
});

it('updates value on change', () => {
const { getByRole } = renderComponent({ value: '', labelId: 'label.test' });

const textArea = getByRole('textbox');

fireEvent.change(textArea, { target: { value: 'New Value' } });

expect(textArea.value).toBe('New Value');
});

it('renders with placeholder', () => {
const { getByPlaceholderText } = renderComponent({ placeholderId: 'placeholder.test', labelId: 'label.test' });

expect(getByPlaceholderText('placeholder.test')).toBeInTheDocument();
});

it('renders with helper text', () => {
const { getByText } = renderComponent({ helperText: 'Helper Text', labelId: 'label.test' });

expect(getByText('Helper Text')).toBeInTheDocument();
});

it('renders with hint', () => {
const { getByText } = renderComponent({ hint: 'Hint Text', labelId: 'label.test' });

expect(getByText('(Hint Text)')).toBeInTheDocument();
});

it('renders with required attribute', () => {
const { getByRole } = renderComponent({ required: true, labelId: 'label.test' });

const textArea = getByRole('textbox');

expect(textArea).toBeRequired();
});

it('renders with maxLength attribute', () => {
const { getByRole } = renderComponent({ maxLength: 10, labelId: 'label.test' });

const textArea = getByRole('textbox');

expect(textArea).toHaveAttribute('maxLength', '10');
});
});

0 comments on commit 844c11f

Please sign in to comment.