Files
tansy-ui/src/components/TextArea.test.tsx
T
thekhalidsha f01de83733 Rebuild Tansy UI on Base UI with one API and dark mode (v0.3.0)
Every component is rebuilt on @base-ui/react with a single prop API,
token-only colours and dark mode, and 28 new components are added.

Components
- New: Field, FeaturedTextArea, NumberInput, Combobox, CheckboxGroup,
  Slider, ToggleGroup/Toggle, IconButton, Dialog/Modal, AlertDialog,
  Drawer/Sheet, Popover, InfoTip, DropdownMenu, Tabs, Breadcrumb,
  Pagination, Toast (Toaster, useToast), Progress, Meter, Skeleton,
  CountBadge, Avatar/AvatarGroup, Kbd, Separator, Table
- Rewritten: Button, Input, FeaturedInput, TextArea, Select,
  FeaturedSelect (real ARIA combobox), NativeSelect, Checkbox, Radio,
  Switch, Tooltip, Accordion, Alert, Spinner, Badge, Card
- Removed TextField (use Input) and sonner (use Toast)

Foundations
- Semantic tokens (surfaces, text, borders, status ramps) in
  src/styles/tokens.css, with class-based dark mode
- Visible focus rings, linked labels/descriptions/errors, alert roles by
  severity, keyboard support from Base UI
- Fixed the Classic Blue ramp; Emerald, Amber and Teal 500 steps now
  carry white text at AA
- Library build (per-component ES modules, types, tokens.css, exports)

Quality
- Vitest, Testing Library and axe tests for every component (289 tests)
- scripts/check-stories.mjs renders every story in Chrome and fails on
  errors or axe violations, including contrast; all 423 stories pass in
  light and dark mode and in all seven palettes
- Storybook: controls for every prop, open states for every popup,
  light/dark toolbar, Introduction guides, docs/CONVENTIONS.md

BREAKING CHANGE: component props changed across the kit; see CHANGELOG.md.
2026-09-28 21:12:11 +05:30

91 lines
3.9 KiB
TypeScript

import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vitest'
import { expectNoA11yViolations } from '../test/a11y'
import { TextArea } from './TextArea'
describe('TextArea', () => {
it('renders a textbox named by its label', () => {
render(<TextArea label="Message" />)
const textarea = screen.getByRole('textbox', { name: 'Message' })
expect(textarea.tagName).toBe('TEXTAREA')
})
it('announces the description', () => {
render(<TextArea label="Message" description="We reply within a day." />)
expect(screen.getByRole('textbox')).toHaveAccessibleDescription('We reply within a day.')
})
it('marks the textarea invalid and announces the error', () => {
render(<TextArea label="Message" error="Write at least 20 characters." description="Hidden" />)
const textarea = screen.getByRole('textbox')
expect(textarea).toHaveAttribute('aria-invalid', 'true')
expect(textarea).toHaveAccessibleDescription('Write at least 20 characters.')
})
it('passes required, disabled, readOnly and rows through', () => {
const { rerender } = render(<TextArea label="Message" required disabled rows={5} />)
const textarea = screen.getByRole('textbox')
expect(textarea).toBeRequired()
expect(textarea).toBeDisabled()
expect(textarea).toHaveAttribute('rows', '5')
rerender(<TextArea label="Message" readOnly />)
expect(screen.getByRole('textbox')).toHaveAttribute('readonly')
})
it('works uncontrolled and accepts Enter as a new line', async () => {
const onValueChange = vi.fn()
render(<TextArea label="Message" defaultValue="Hi" onValueChange={onValueChange} />)
const textarea = screen.getByRole('textbox')
await userEvent.type(textarea, '{Enter}there')
expect(textarea).toHaveValue('Hi\nthere')
expect(onValueChange).toHaveBeenLastCalledWith('Hi\nthere')
})
it('works controlled and still fires native onChange', async () => {
const onValueChange = vi.fn()
const onChange = vi.fn()
render(<TextArea label="Message" value="Hi" onValueChange={onValueChange} onChange={onChange} />)
await userEvent.type(screen.getByRole('textbox'), '!')
expect(onValueChange).toHaveBeenLastCalledWith('Hi!')
expect(onChange).toHaveBeenCalled()
expect(screen.getByRole('textbox')).toHaveValue('Hi')
})
it('shows a live character counter linked to the textarea', async () => {
render(<TextArea label="Bio" maxLength={10} defaultValue="Jane" />)
const textarea = screen.getByRole('textbox')
expect(textarea).toHaveAttribute('maxlength', '10')
expect(screen.getByText('4 / 10')).toBeInTheDocument()
expect(textarea).toHaveAccessibleDescription(expect.stringContaining('4 of 10 characters used'))
const counter = screen.getByText('4 of 10 characters used').parentElement
expect(counter).toHaveAttribute('aria-live', 'polite')
await userEvent.type(textarea, ' Doe and more')
expect(textarea).toHaveValue('Jane Doe a')
expect(screen.getByText('10 / 10')).toBeInTheDocument()
})
it('applies the resize option', () => {
const { rerender } = render(<TextArea label="Message" resize="none" />)
expect(screen.getByRole('textbox')).toHaveClass('resize-none')
rerender(<TextArea label="Message" resize="both" />)
expect(screen.getByRole('textbox')).toHaveClass('resize')
})
it('sets an explicit height with autoResize and none without', () => {
const { rerender } = render(<TextArea label="Message" autoResize rows={2} maxRows={4} />)
const textarea = screen.getByRole('textbox')
expect(textarea.style.height).toMatch(/px$/)
rerender(<TextArea label="Message" rows={2} />)
expect(textarea.style.height).toBe('')
})
it('has no axe violations', async () => {
const { container } = render(
<TextArea label="Message" description="Be specific." required maxLength={200} defaultValue="Hello" />,
)
await expectNoA11yViolations(container)
})
})