183 lines
7.8 KiB
TypeScript
183 lines
7.8 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
|
import { render, screen, fireEvent } from '@testing-library/react'
|
|
import { Button, buttonClass } from '@/components/ui/button'
|
|
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'
|
|
|
|
describe('Button', () => {
|
|
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('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 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('buttonClass gives links the same look as buttons', () => {
|
|
expect(buttonClass()).toContain('bg-ink-2')
|
|
expect(buttonClass('secondary')).toContain('shadow-secondary')
|
|
expect(buttonClass('primary', { block: true })).toContain('w-full')
|
|
})
|
|
|
|
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('StatusTag and PriorityTag', () => {
|
|
it.each([
|
|
['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)
|
|
})
|
|
|
|
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').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('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('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('h-9')
|
|
expect(input.className).toContain('shadow-input')
|
|
expect(input.className).toContain('rounded-md')
|
|
})
|
|
|
|
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()
|
|
})
|
|
})
|