import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, it, expect, vi } from 'vitest'
import { axe } from 'vitest-axe'
import * as React from 'react'
import { Textarea } from './textarea'
import { Field, FieldLabel } from '../field/field'
describe('Textarea', () => {
it('associates with a Field label (matching for/id, like Input)', () => {
render(
Bio
,
)
// getByLabelText only resolves when the label's `for` matches the control id.
const textarea = screen.getByLabelText('Bio')
expect(textarea.tagName).toBe('TEXTAREA')
expect(textarea.id).not.toBe('')
})
it('associates with a Field label when wrapped (slots/clearable)', () => {
render(
Notes
,
)
expect(screen.getByLabelText('Notes').tagName).toBe('TEXTAREA')
})
it('renders with textbox role', () => {
render()
expect(screen.getByRole('textbox', { name: 'Bio' })).toBeInTheDocument()
})
it('accepts user typing across multiple lines', async () => {
const user = userEvent.setup()
render()
const textarea = screen.getByRole('textbox')
await user.type(textarea, 'line one{enter}line two')
expect(textarea).toHaveValue('line one\nline two')
})
it('calls onChange when typing in a controlled textarea', async () => {
const user = userEvent.setup()
const onChange = vi.fn()
function Controlled() {
const [value, setValue] = React.useState('')
return (