feat(design): add IMS Admin primitives (Button, StatusTag, Checkbox, FilterChip, Segmented, Avatar, Drawer) and restyle Field, Card, Modal
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,49 +2,27 @@ import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { Modal } from '@/components/ui/modal'
|
||||
|
||||
describe('Modal (Modernist dialog)', () => {
|
||||
describe('Modal', () => {
|
||||
it('renders nothing when closed', () => {
|
||||
const { container } = render(
|
||||
<Modal open={false} onClose={() => {}} title="T">
|
||||
body
|
||||
</Modal>,
|
||||
)
|
||||
const { container } = render(<Modal open={false} onClose={() => {}} title="T">body</Modal>)
|
||||
expect(container.firstChild).toBeNull()
|
||||
})
|
||||
|
||||
it('renders a dialog with title, body and footer on the surface fill', () => {
|
||||
render(
|
||||
<Modal open onClose={() => {}} title="Confirm" footer={<span>actions</span>}>
|
||||
body text
|
||||
</Modal>,
|
||||
)
|
||||
it('renders a 12px card dialog with the floating shadow and a 1px header rule', () => {
|
||||
render(<Modal open onClose={() => {}} title="Confirm" footer={<span>actions</span>}>body text</Modal>)
|
||||
const dialog = screen.getByRole('dialog')
|
||||
expect(dialog).toBeTruthy()
|
||||
expect(screen.getByText('Confirm')).toBeTruthy()
|
||||
expect(screen.getByText('body text')).toBeTruthy()
|
||||
expect(screen.getByText('actions')).toBeTruthy()
|
||||
expect(dialog.innerHTML).toContain('bg-surface')
|
||||
expect(dialog.innerHTML).toContain('shadow-lg')
|
||||
})
|
||||
|
||||
it('has no rounded corners and uses a 2px title rule', () => {
|
||||
render(
|
||||
<Modal open onClose={() => {}} title="T">
|
||||
x
|
||||
</Modal>,
|
||||
)
|
||||
const html = screen.getByRole('dialog').innerHTML
|
||||
expect(html).not.toMatch(/rounded/)
|
||||
expect(html).toContain('border-b')
|
||||
expect(dialog.innerHTML).toContain('rounded-xl')
|
||||
expect(dialog.innerHTML).toContain('shadow-float')
|
||||
expect(dialog.innerHTML).toContain('border-b border-rule')
|
||||
expect(dialog.innerHTML).not.toContain('border-b-2')
|
||||
})
|
||||
|
||||
it('closes from the X button and on Escape', () => {
|
||||
const onClose = vi.fn()
|
||||
render(
|
||||
<Modal open onClose={onClose} title="T">
|
||||
x
|
||||
</Modal>,
|
||||
)
|
||||
render(<Modal open onClose={onClose} title="T">x</Modal>)
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
|
||||
fireEvent.keyDown(document, { key: 'Escape' })
|
||||
expect(onClose).toHaveBeenCalledTimes(2)
|
||||
|
||||
@@ -1,132 +1,176 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { render, screen, fireEvent } from '@testing-library/react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Tag } from '@/components/ui/tag'
|
||||
import { StatusTag, PriorityTag, statusTone } from '@/components/ui/status-tag'
|
||||
import { StatusTag, PriorityTag, statusHue } from '@/components/ui/status-tag'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { FilterChip } from '@/components/ui/filter-chip'
|
||||
import { Segmented } from '@/components/ui/segmented'
|
||||
import { Avatar } from '@/components/ui/avatar'
|
||||
import { Drawer } from '@/components/ui/drawer'
|
||||
import { Field, Input } from '@/components/ui/field'
|
||||
import { Card } from '@/components/ui/card'
|
||||
import { TABLE, TH, TR, TD } from '@/components/ui/table-styles'
|
||||
|
||||
describe('Button', () => {
|
||||
it('renders a primary accent button that is type=button and flush left', () => {
|
||||
render(<Button variant="primary">Save</Button>)
|
||||
const btn = screen.getByRole('button', { name: 'Save' })
|
||||
expect(btn.getAttribute('type')).toBe('button')
|
||||
expect(btn.className).toContain('bg-accent')
|
||||
expect(btn.className).toContain('justify-start')
|
||||
expect(btn.className).not.toContain('justify-center')
|
||||
it('primary is the dark 32px button, type=button by default', () => {
|
||||
render(<Button>Report incident</Button>)
|
||||
const b = screen.getByRole('button', { name: 'Report incident' })
|
||||
expect(b.getAttribute('type')).toBe('button')
|
||||
expect(b.className).toContain('bg-ink-2')
|
||||
expect(b.className).toContain('h-8')
|
||||
expect(b.className).toContain('rounded-md')
|
||||
expect(b.className).toContain('shadow-primary')
|
||||
})
|
||||
|
||||
it('block buttons span the row and stay flush left', () => {
|
||||
render(<Button block>Go</Button>)
|
||||
const btn = screen.getByRole('button', { name: 'Go' })
|
||||
expect(btn.className).toContain('w-full')
|
||||
expect(btn.className).toContain('justify-start')
|
||||
it('secondary is white with a ring', () => {
|
||||
render(<Button variant="secondary">Export</Button>)
|
||||
const b = screen.getByRole('button', { name: 'Export' })
|
||||
expect(b.className).toContain('bg-surface')
|
||||
expect(b.className).toContain('shadow-secondary')
|
||||
})
|
||||
|
||||
it('icon buttons are square and centered', () => {
|
||||
render(<Button variant="icon" aria-label="Close" />)
|
||||
const btn = screen.getByRole('button', { name: 'Close' })
|
||||
expect(btn.className).toContain('w-9')
|
||||
expect(btn.className).toContain('justify-center')
|
||||
expect(btn.className).not.toContain('justify-start')
|
||||
it('icon is a 28px ghost button', () => {
|
||||
render(<Button variant="icon" aria-label="More" />)
|
||||
const b = screen.getByRole('button', { name: 'More' })
|
||||
expect(b.className).toContain('w-7')
|
||||
expect(b.className).toContain('h-7')
|
||||
})
|
||||
|
||||
it('never uses a rounded class', () => {
|
||||
render(<Button variant="secondary">x</Button>)
|
||||
expect(screen.getByRole('button').className).not.toMatch(/rounded/)
|
||||
it('mobile and block make a full-width 48px button', () => {
|
||||
render(<Button block mobile>Submit</Button>)
|
||||
const b = screen.getByRole('button', { name: 'Submit' })
|
||||
expect(b.className).toContain('w-full')
|
||||
expect(b.className).toContain('h-12')
|
||||
expect(b.className).toContain('rounded-xl')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Tag', () => {
|
||||
describe('StatusTag and PriorityTag', () => {
|
||||
it.each([
|
||||
['accent', 'bg-accent-100'],
|
||||
['neutral', 'bg-neutral-100'],
|
||||
['outline', 'border-accent'],
|
||||
] as const)('%s variant carries %s', (variant, cls) => {
|
||||
render(<Tag variant={variant}>Label</Tag>)
|
||||
expect(screen.getByText('Label').className).toContain(cls)
|
||||
['reported', 'amber'], ['triaged', 'blue'], ['investigating', 'violet'], ['capa_pending', 'orange'],
|
||||
['verification', 'indigo'], ['closed', 'green'], ['open', 'grey'], ['in_progress', 'blue'],
|
||||
['overdue', 'red'], ['pending_verification', 'indigo'], ['verified', 'green'], ['reopened', 'red'],
|
||||
['high', 'orange'], ['med', 'amber'], ['low', 'grey'], ['nonsense', 'grey'],
|
||||
])('%s uses the %s pair', (key, hue) => {
|
||||
expect(statusHue(key)).toBe(hue)
|
||||
})
|
||||
})
|
||||
|
||||
describe('statusTone', () => {
|
||||
it.each([
|
||||
['reported', 'accent'],
|
||||
['triaged', 'outline'],
|
||||
['investigating', 'outline'],
|
||||
['capa_pending', 'outline'],
|
||||
['verification', 'outline'],
|
||||
['closed', 'neutral'],
|
||||
['open', 'accent'],
|
||||
['in_progress', 'outline'],
|
||||
['overdue', 'accent'],
|
||||
['pending_verification', 'outline'],
|
||||
['verified', 'neutral'],
|
||||
['reopened', 'accent'],
|
||||
['something_else', 'neutral'],
|
||||
])('%s maps to %s', (status, tone) => {
|
||||
expect(statusTone(status)).toBe(tone)
|
||||
})
|
||||
})
|
||||
|
||||
describe('StatusTag', () => {
|
||||
it('shows a humanized text label plus an icon, never color alone', () => {
|
||||
const { container } = render(<StatusTag status="capa_pending" />)
|
||||
expect(screen.getByText('CAPA Pending')).toBeTruthy()
|
||||
expect(container.querySelector('svg')).not.toBeNull()
|
||||
it('renders a dark-on-tint pill with a text label', () => {
|
||||
render(<StatusTag status="capa_pending" />)
|
||||
const el = screen.getByText('CAPA pending')
|
||||
expect(el.className).toContain('bg-st-orange-bg')
|
||||
expect(el.className).toContain('text-st-orange-fg')
|
||||
expect(el.className).toContain('rounded-md')
|
||||
})
|
||||
|
||||
it('accepts a label override', () => {
|
||||
render(<StatusTag status="overdue">Late</StatusTag>)
|
||||
expect(screen.getByText('Late')).toBeTruthy()
|
||||
expect(screen.getByText('Late').className).toContain('bg-st-red-bg')
|
||||
})
|
||||
|
||||
it('priority med reads Medium on the amber pair', () => {
|
||||
render(<PriorityTag priority="med" />)
|
||||
expect(screen.getByText('Medium').className).toContain('bg-st-amber-bg')
|
||||
})
|
||||
|
||||
it('never uses white text on a saturated fill', () => {
|
||||
const { container } = render(<StatusTag status="closed" />)
|
||||
expect(container.innerHTML).not.toContain('text-white')
|
||||
})
|
||||
})
|
||||
|
||||
describe('PriorityTag', () => {
|
||||
it.each([
|
||||
['high', 'bg-accent-100'],
|
||||
['med', 'border-accent'],
|
||||
['low', 'bg-neutral-100'],
|
||||
])('%s priority uses %s', (priority, cls) => {
|
||||
const { container } = render(<PriorityTag priority={priority} />)
|
||||
expect(container.firstElementChild?.className).toContain(cls)
|
||||
describe('Checkbox', () => {
|
||||
it('toggles and reports the next value', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<Checkbox checked={false} onChange={onChange} label="Select row" />)
|
||||
const cb = screen.getByRole('checkbox', { name: 'Select row' })
|
||||
expect(cb.getAttribute('aria-checked')).toBe('false')
|
||||
fireEvent.click(cb)
|
||||
expect(onChange).toHaveBeenCalledWith(true)
|
||||
})
|
||||
|
||||
it('shows checked and indeterminate states', () => {
|
||||
const { rerender } = render(<Checkbox checked onChange={() => {}} label="a" />)
|
||||
expect(screen.getByRole('checkbox').getAttribute('aria-checked')).toBe('true')
|
||||
expect(screen.getByRole('checkbox').className).toContain('bg-accent')
|
||||
rerender(<Checkbox checked={false} indeterminate onChange={() => {}} label="a" />)
|
||||
expect(screen.getByRole('checkbox').getAttribute('aria-checked')).toBe('mixed')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Field and Input', () => {
|
||||
it('associates the label with the input', () => {
|
||||
render(
|
||||
<Field label="Email" htmlFor="email">
|
||||
<Input id="email" />
|
||||
</Field>,
|
||||
)
|
||||
describe('FilterChip', () => {
|
||||
const options = [{ value: 'open', label: 'Open' }, { value: 'closed', label: 'Closed' }]
|
||||
|
||||
it('unset shows the label and a select with an empty option', () => {
|
||||
const { container } = render(<FilterChip label="Status" value="" options={options} onChange={() => {}} />)
|
||||
expect(container.querySelector('label > span')?.textContent).toBe('Status')
|
||||
expect((screen.getByLabelText('Status') as HTMLSelectElement).value).toBe('')
|
||||
})
|
||||
|
||||
it('changes through the select', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<FilterChip label="Status" value="" options={options} onChange={onChange} />)
|
||||
fireEvent.change(screen.getByLabelText('Status'), { target: { value: 'closed' } })
|
||||
expect(onChange).toHaveBeenCalledWith('closed')
|
||||
})
|
||||
|
||||
it('set shows Label: value in the teal tint with a clear button', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<FilterChip label="Status" value="closed" options={options} onChange={onChange} />)
|
||||
expect(screen.getByText('Status: Closed').closest('label')?.className).toContain('bg-accent-tint-2')
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Clear Status' }))
|
||||
expect(onChange).toHaveBeenCalledWith('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Segmented', () => {
|
||||
it('marks the active option and reports changes', () => {
|
||||
const onChange = vi.fn()
|
||||
render(<Segmented value="list" onChange={onChange} options={[{ value: 'list', label: 'List' }, { value: 'board', label: 'Board' }]} />)
|
||||
expect(screen.getByRole('button', { name: 'List' }).getAttribute('aria-pressed')).toBe('true')
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Board' }))
|
||||
expect(onChange).toHaveBeenCalledWith('board')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Avatar', () => {
|
||||
it('shows up to two initials on the teal tile', () => {
|
||||
render(<Avatar name="Ada Lovelace King" />)
|
||||
const el = screen.getByText('AL')
|
||||
expect(el.className).toContain('bg-accent-200')
|
||||
expect(el.className).toContain('rounded-[6px]')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Drawer', () => {
|
||||
it('renders nothing when closed', () => {
|
||||
const { container } = render(<Drawer open={false} onClose={() => {}} title="Notifications">x</Drawer>)
|
||||
expect(container.firstChild).toBeNull()
|
||||
})
|
||||
|
||||
it('renders a labelled dialog, closes on the X button and on Escape', () => {
|
||||
const onClose = vi.fn()
|
||||
render(<Drawer open onClose={onClose} title="Notifications" actions={<span>Mark all read</span>}>body</Drawer>)
|
||||
expect(screen.getByRole('dialog', { name: 'Notifications' })).toBeTruthy()
|
||||
expect(screen.getByText('Mark all read')).toBeTruthy()
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
|
||||
fireEvent.keyDown(document, { key: 'Escape' })
|
||||
expect(onClose).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Field, Input, Card', () => {
|
||||
it('Input is 36px with the faint ring and is labelled', () => {
|
||||
render(<Field label="Email" htmlFor="email"><Input id="email" /></Field>)
|
||||
const input = screen.getByLabelText('Email')
|
||||
expect(input.className).toContain('bg-surface')
|
||||
expect(input.className).toContain('border-divider')
|
||||
expect(input.className).toContain('h-9')
|
||||
expect(input.className).toContain('shadow-input')
|
||||
expect(input.className).toContain('rounded-md')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Card', () => {
|
||||
it('renders kicker, title, body and meta on the surface fill', () => {
|
||||
const { container } = render(
|
||||
<Card kicker="Open" title="Forklift" meta="Due Friday">
|
||||
Body text
|
||||
</Card>,
|
||||
)
|
||||
expect(screen.getByText('Open')).toBeTruthy()
|
||||
it('Card is a white 12px card with the card shadow', () => {
|
||||
const { container } = render(<Card title="Forklift" kicker="Open" meta="Due Friday">Body</Card>)
|
||||
expect(container.firstElementChild?.className).toContain('rounded-xl')
|
||||
expect(container.firstElementChild?.className).toContain('shadow-card')
|
||||
expect(screen.getByText('Forklift')).toBeTruthy()
|
||||
expect(screen.getByText('Body text')).toBeTruthy()
|
||||
expect(screen.getByText('Due Friday')).toBeTruthy()
|
||||
expect(container.firstElementChild?.className).toContain('bg-surface')
|
||||
})
|
||||
})
|
||||
|
||||
describe('table styles', () => {
|
||||
it('uses a 2px header rule and 1px row rules', () => {
|
||||
expect(TH).toContain('border-b-2')
|
||||
expect(TH).toContain('border-divider')
|
||||
expect(TD).toContain('border-b')
|
||||
expect(TR).toContain('hover:bg-ink/4')
|
||||
expect(TABLE).toContain('w-full')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user