import React, { useState } from 'react'; import { render, screen, fireEvent } from '@testing-library/react'; jest.mock('lucide-react', () => ({ Check: () => , Minus: () => , ArrowDown: () => , ArrowUp: () => , ChevronsUpDown: () => , Inbox: () => , ChevronLeft: () => , ChevronRight: () => , ChevronsLeft: () => , ChevronsRight: () => , })); import { DataTable } from '@/components/ds/DataTable'; const COLUMNS = [ { key: 'id', header: 'ID' }, { key: 'title', header: 'Title' }, ]; const createMockRows = (count = 60) => Array.from({ length: count }, (_, i) => ({ id: `order-${i + 1}`, title: `Order #${i + 1}`, })); function TestTableWrapper({ rowCount = 60, pageSize = 25, selectAllPages = true, initialSelected = [], }) { const [selectedIds, setSelectedIds] = useState(initialSelected); const rows = createMockRows(rowCount); return (
{selectedIds.length}
row.id} pageSize={pageSize} selectAllPages={selectAllPages} selectedIds={selectedIds} onSelectionChange={setSelectedIds} />
); } describe('DataTable selection across pagination', () => { it('should select ALL 60 rows across all pages when selectAllPages is true', () => { render(); expect(screen.getByTestId('selected-count').textContent).toBe('0'); // Header checkbox for select all const selectAllCheckbox = screen.getByRole('checkbox', { name: /select all rows/i }); expect(selectAllCheckbox).not.toBeChecked(); // Click select all fireEvent.click(selectAllCheckbox); // All 60 rows should now be selected expect(screen.getByTestId('selected-count').textContent).toBe('60'); expect(selectAllCheckbox).toBeChecked(); // Click again to unselect all fireEvent.click(selectAllCheckbox); expect(screen.getByTestId('selected-count').textContent).toBe('0'); expect(selectAllCheckbox).not.toBeChecked(); }); it('should select ONLY current page (25 rows) when selectAllPages is false', () => { render(); expect(screen.getByTestId('selected-count').textContent).toBe('0'); const selectAllCheckbox = screen.getByRole('checkbox', { name: /select all rows on this page/i }); fireEvent.click(selectAllCheckbox); // Only the 25 rows on page 1 should be selected expect(screen.getByTestId('selected-count').textContent).toBe('25'); }); it('should reflect indeterminate state when some rows are selected', () => { render( ); expect(screen.getByTestId('selected-count').textContent).toBe('3'); const selectAllCheckbox = screen.getByRole('checkbox', { name: /select all rows/i }); expect(selectAllCheckbox.getAttribute('data-state') === 'indeterminate' || selectAllCheckbox.indeterminate).toBeTruthy(); }); it('should toggle individual row selection without affecting other pages', () => { render(); const rowCheckboxes = screen.getAllByRole('checkbox'); // First checkbox is header, second is row 1 const firstRowCheckbox = rowCheckboxes[1]; fireEvent.click(firstRowCheckbox); expect(screen.getByTestId('selected-count').textContent).toBe('1'); fireEvent.click(firstRowCheckbox); expect(screen.getByTestId('selected-count').textContent).toBe('0'); }); });