Files
ims/tests/components/ui/table-card.test.tsx
T

109 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent, within } from '@testing-library/react'
import { TableCard, type Column } from '@/components/ui/table-card'
type Row = { id: string; ref: string; status: string }
const rows: Row[] = [
{ id: '1', ref: 'IMS-0001', status: 'Open' },
{ id: '2', ref: 'IMS-0002', status: 'Closed' },
{ id: '3', ref: 'IMS-0003', status: 'Open' },
]
const columns: Column<Row>[] = [
{ key: 'ref', header: 'Reference', sortKey: 'ref', cell: r => r.ref },
{ key: 'status', header: 'Status', cell: r => r.status },
]
const base = { columns, rows, rowKey: (r: Row) => r.id }
describe('TableCard', () => {
it('renders a 12px card with a grey header and the rows', () => {
const { container } = render(<TableCard {...base} />)
expect(container.firstElementChild?.className).toContain('rounded-xl')
expect(container.firstElementChild?.className).toContain('shadow-card')
expect(screen.getByText('Reference').closest('th')?.className).toContain('bg-surface-2')
expect(screen.getByText('IMS-0002')).toBeTruthy()
expect(screen.getAllByRole('row')).toHaveLength(4)
})
it('opens a row on click and on Enter, but not from the actions cell', () => {
const onRowOpen = vi.fn()
render(<TableCard {...base} onRowOpen={onRowOpen} rowActions={() => <button>quick</button>} />)
fireEvent.click(screen.getByText('IMS-0001'))
expect(onRowOpen).toHaveBeenCalledWith(rows[0])
fireEvent.keyDown(screen.getByText('IMS-0002').closest('tr')!, { key: 'Enter' })
expect(onRowOpen).toHaveBeenCalledWith(rows[1])
onRowOpen.mockClear()
fireEvent.click(screen.getAllByText('quick')[0])
expect(onRowOpen).not.toHaveBeenCalled()
})
it('sorts a single column: sortable header is a button, aria-sort reflects state', () => {
const onSortChange = vi.fn()
render(<TableCard {...base} sort={{ key: 'ref', dir: 'asc' }} onSortChange={onSortChange} />)
expect(screen.getByText('Reference').closest('th')?.getAttribute('aria-sort')).toBe('ascending')
expect(screen.getByText('Status').closest('th')?.getAttribute('aria-sort')).toBeNull()
expect(screen.queryByRole('button', { name: /Status/ })).toBeNull()
fireEvent.click(screen.getByRole('button', { name: /Reference/ }))
expect(onSortChange).toHaveBeenCalledWith('ref')
})
it('selects rows and all rows, and shows the bulk bar with the count', () => {
const onChange = vi.fn()
const { rerender } = render(
<TableCard {...base} selection={{ selected: new Set(), onChange }} bulkActions={<button>Export</button>} />,
)
expect(screen.queryByText(/selected/)).toBeNull()
fireEvent.click(screen.getByRole('checkbox', { name: 'Select all rows' }))
expect(onChange).toHaveBeenLastCalledWith(new Set(['1', '2', '3']))
fireEvent.click(screen.getAllByRole('checkbox', { name: 'Select row' })[1])
expect(onChange).toHaveBeenLastCalledWith(new Set(['2']))
rerender(<TableCard {...base} selection={{ selected: new Set(['1']), onChange }} bulkActions={<button>Export</button>} />)
expect(screen.getByText('1 selected')).toBeTruthy()
expect(screen.getByRole('button', { name: 'Export' })).toBeTruthy()
expect(screen.getByRole('checkbox', { name: 'Select all rows' }).getAttribute('aria-checked')).toBe('mixed')
expect(screen.getByText('IMS-0001').closest('tr')?.className).toContain('bg-accent-tint')
})
it('renders saved views with counts and reports changes', () => {
const onChange = vi.fn()
render(
<TableCard
{...base}
views={{ items: [{ key: 'all', label: 'All', count: 248 }, { key: 'open', label: 'Open', count: 37 }], active: 'all', onChange }}
/>,
)
expect(screen.getByRole('button', { name: /All/ }).className).toContain('bg-rule-2')
expect(screen.getByText('248')).toBeTruthy()
fireEvent.click(screen.getByRole('button', { name: /Open/ }))
expect(onChange).toHaveBeenCalledWith('open')
})
it('renders the footer count and pager', () => {
const onPage = vi.fn()
render(<TableCard {...base} footer={{ from: 1, to: 3, total: 248, page: 1, pageCount: 83, onPage }} />)
expect(screen.getByText('Showing 1–3 of 248')).toBeTruthy()
expect((screen.getByRole('button', { name: 'Previous page' }) as HTMLButtonElement).disabled).toBe(true)
fireEvent.click(screen.getByRole('button', { name: 'Next page' }))
expect(onPage).toHaveBeenCalledWith(2)
})
it('shows the empty state instead of the table', () => {
render(<TableCard {...base} rows={[]} empty={{ title: 'No incidents found', action: <button>Report incident</button> }} />)
expect(screen.queryByRole('table')).toBeNull()
expect(screen.getByText('No incidents found')).toBeTruthy()
expect(screen.getByRole('button', { name: 'Report incident' })).toBeTruthy()
})
it('renders a stacked mobile list beside the desktop table', () => {
const onRowOpen = vi.fn()
const { container } = render(<TableCard {...base} onRowOpen={onRowOpen} mobileRow={r => <span>mobile {r.ref}</span>} />)
const list = container.querySelector('ul')!
expect(list.className).toContain('md:hidden')
expect(within(list).getByText('mobile IMS-0003')).toBeTruthy()
expect(container.querySelector('table')!.parentElement!.className).toContain('hidden md:block')
fireEvent.click(within(list).getByText('mobile IMS-0003'))
expect(onRowOpen).toHaveBeenCalledWith(rows[2])
})
})