/** @vitest-environment jsdom */ import type { ComponentProps } from 'react'; import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { getCoreRowModel, useReactTable, type ColumnDef } from '@tanstack/react-table'; import { afterEach, describe, expect, it, vi } from 'vitest'; import DropdownMenu from '@/components/elements/dropdown/DropdownMenu'; import DataTable from './DataTable'; import { actionsColumn, DeleteAction, EditAction, EditLinkAction, RowActions, RowActionsMenu } from './RowActions'; vi.mock('@tanstack/react-router', () => ({ Link: ({ to, params, ...props }: ComponentProps<'a'> & { to: string; params?: Record }) => ( path.replace(`$${key}`, String(value)), to )} {...props} /> ), })); interface TestRow { id: number; name: string; } const rows: TestRow[] = [{ id: 7, name: 'Alpha' }]; afterEach(cleanup); describe('RowActions', () => { it('renders edit and delete as labelled buttons with tooltips', async () => { const user = userEvent.setup(); const onEdit = vi.fn(); const onDelete = vi.fn(); render( ); await user.click(screen.getByRole('button', { name: 'Edit Alpha' })); await user.click(screen.getByRole('button', { name: 'Delete Alpha' })); expect(onEdit).toHaveBeenCalledOnce(); expect(onDelete).toHaveBeenCalledOnce(); await user.hover(screen.getByRole('button', { name: 'Edit Alpha' })); await waitFor(() => expect(screen.getAllByRole('tooltip').map((tooltip) => tooltip.textContent)).toContain('Edit') ); }); it('explains why a disabled action is unavailable', async () => { const user = userEvent.setup(); const onDelete = vi.fn(); render( ); const button = screen.getByRole('button', { name: 'Delete Alpha' }); expect(button).toBeDisabled(); await user.hover(button.parentElement!); expect(await screen.findByRole('tooltip')).toHaveTextContent('Users who own servers cannot be deleted.'); }); it('links to the page where the resource is edited', () => { render(); expect(screen.getByRole('link', { name: 'Edit Alpha' })).toHaveAttribute('href', '/panel/nodes/7/settings'); }); it('keeps other actions in the overflow menu', async () => { const onOpen = vi.fn(); render( Open in client area ); fireEvent.click(screen.getByRole('button', { name: 'More actions for Alpha' })); fireEvent.click(await screen.findByRole('menuitem', { name: 'Open in client area' })); expect(onOpen).toHaveBeenCalledOnce(); }); }); describe('actionsColumn', () => { it('sizes the column for its icon buttons unless a width is given', () => { expect(actionsColumn(1, () => null).meta?.cellClassName).toBe('w-14 text-right'); expect(actionsColumn(2, () => null).meta?.headerClassName).toBe('w-24 text-right'); expect(actionsColumn(3, () => null).meta?.cellClassName).toBe('w-32 text-right'); expect(actionsColumn(2, () => null, 'w-52').meta?.cellClassName).toBe('w-52 text-right'); }); it('renders an unsortable column with a screen-reader header and the row actions', () => { const columns = [ { accessorKey: 'name', header: 'Name' }, actionsColumn(1, (row) => ), ] satisfies ColumnDef[]; const Table = () => { const table = useReactTable({ data: rows, columns, getCoreRowModel: getCoreRowModel() }); return ; }; render(); expect(columns[1]!.enableSorting).toBe(false); expect(screen.getByRole('columnheader', { name: 'Actions' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Edit Alpha' })).toBeInTheDocument(); }); });