|
| 1 | +import { describe, it, beforeEach, vi, expect } from 'vitest' |
| 2 | +import { screen, fireEvent } from '@testing-library/react' |
| 3 | + |
| 4 | +import { renderWithProviders } from '../../../__testing-utils__' |
| 5 | +import { TextAreaField } from '../' |
| 6 | + |
| 7 | +import type { ComponentProps } from 'react' |
| 8 | + |
| 9 | +const render = (props: ComponentProps<typeof TextAreaField>) => { |
| 10 | + return renderWithProviders(<TextAreaField {...props} />) |
| 11 | +} |
| 12 | + |
| 13 | +describe('TextAreaField', () => { |
| 14 | + let props: ComponentProps<typeof TextAreaField> |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + props = { |
| 18 | + title: 'TextAreaField', |
| 19 | + placeholder: 'Enter text...', |
| 20 | + value: '', |
| 21 | + onChange: vi.fn(), |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + it('renders the TextAreaField component', () => { |
| 26 | + render(props) |
| 27 | + screen.getByText('TextAreaField') |
| 28 | + expect(screen.getByTestId('TextAreaField')).toBeInTheDocument() |
| 29 | + }) |
| 30 | + |
| 31 | + it('displays the correct placeholder text', () => { |
| 32 | + render(props) |
| 33 | + expect(screen.getByPlaceholderText('Enter text...')).toBeInTheDocument() |
| 34 | + }) |
| 35 | + |
| 36 | + it('updates value when user types', () => { |
| 37 | + render(props) |
| 38 | + const textarea = screen.getByTestId('TextAreaField') |
| 39 | + |
| 40 | + fireEvent.change(textarea, { target: { value: 'Hello, world!' } }) |
| 41 | + |
| 42 | + expect(props.onChange).toHaveBeenCalledTimes(1) |
| 43 | + }) |
| 44 | + |
| 45 | + it('disables the textarea when disabled prop is true', () => { |
| 46 | + props.disabled = true |
| 47 | + render(props) |
| 48 | + expect(screen.getByTestId('TextAreaField')).toBeDisabled() |
| 49 | + }) |
| 50 | + |
| 51 | + it('displays an error message when error prop is provided', () => { |
| 52 | + props.error = 'Error: Invalid input' |
| 53 | + render(props) |
| 54 | + |
| 55 | + expect(screen.getByText('Error: Invalid input')).toBeInTheDocument() |
| 56 | + }) |
| 57 | + |
| 58 | + it('display an icon when tooltip prop is provided', () => { |
| 59 | + props.tooltipText = 'ot-icon-check' |
| 60 | + render(props) |
| 61 | + screen.getByTestId('tooltip-icon') |
| 62 | + }) |
| 63 | + |
| 64 | + it('display left icon when leftIcon prop is provided', () => { |
| 65 | + props.leftIcon = 'information' |
| 66 | + render(props) |
| 67 | + screen.getByTestId('left-icon') |
| 68 | + }) |
| 69 | +}) |
0 commit comments