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[] = [ { 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() 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( } />) 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() 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( Export} />, ) 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(Export} />) 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( , ) 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() 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(Report incident }} />) 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( mobile {r.ref}} />) 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]) }) })