Files
tansy-ui/src/components/Dialog.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

95 lines
3.7 KiB
TypeScript

import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vitest'
import { expectNoA11yViolations } from '../test/a11y'
import { Button } from './Button'
import { Dialog, Modal } from './Dialog'
import { Input } from './Input'
function Example(props: Partial<React.ComponentProps<typeof Dialog>>) {
return (
<Dialog
trigger={<Button>Edit profile</Button>}
title="Edit profile"
description="Changes are visible to your team."
footer={
<>
<Dialog.Close render={<Button variant="outline" color="neutral" />}>Cancel</Dialog.Close>
<Button>Save</Button>
</>
}
{...props}
>
<Input label="Full name" defaultValue="Jane Doe" />
</Dialog>
)
}
describe('Dialog', () => {
it('opens from the trigger as a named dialog', async () => {
render(<Example />)
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: 'Edit profile' }))
const dialog = await screen.findByRole('dialog', { name: 'Edit profile' })
expect(dialog).toHaveAccessibleDescription('Changes are visible to your team.')
expect(screen.getByRole('textbox', { name: 'Full name' })).toBeInTheDocument()
})
it('closes on Escape and returns focus to the trigger', async () => {
render(<Example />)
const trigger = screen.getByRole('button', { name: 'Edit profile' })
await userEvent.click(trigger)
await screen.findByRole('dialog')
await userEvent.keyboard('{Escape}')
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
await waitFor(() => expect(trigger).toHaveFocus())
})
it('moves focus inside and has a labelled close button', async () => {
render(<Example />)
await userEvent.click(screen.getByRole('button', { name: 'Edit profile' }))
const dialog = await screen.findByRole('dialog')
await waitFor(() => expect(dialog).toContainElement(document.activeElement as HTMLElement))
await userEvent.click(screen.getByRole('button', { name: 'Close' }))
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
})
it('hides the close button on request', async () => {
render(<Example defaultOpen hideCloseButton />)
await screen.findByRole('dialog')
expect(screen.queryByRole('button', { name: 'Close' })).not.toBeInTheDocument()
})
it('works controlled', async () => {
const onOpenChange = vi.fn()
const { rerender } = render(<Example open={false} onOpenChange={onOpenChange} />)
await userEvent.click(screen.getByRole('button', { name: 'Edit profile' }))
expect(onOpenChange).toHaveBeenCalledWith(true)
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
rerender(<Example open onOpenChange={onOpenChange} />)
expect(await screen.findByRole('dialog')).toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }))
expect(onOpenChange).toHaveBeenLastCalledWith(false)
})
it('builds a custom layout from compound parts, and Modal is an alias', async () => {
render(
<Modal.Root defaultOpen>
<Modal.Content size="sm">
<Modal.Header>
<Modal.Title>Invite people</Modal.Title>
</Modal.Header>
<Modal.Body>Body text</Modal.Body>
</Modal.Content>
</Modal.Root>,
)
expect(await screen.findByRole('dialog', { name: 'Invite people' })).toHaveTextContent('Body text')
})
it('has no axe violations when open', async () => {
render(<Example defaultOpen />)
await screen.findByRole('dialog')
await expectNoA11yViolations(document.body)
})
})