import { render, screen } from '@testing-library/react'; import { computeColumnWidths } from '~/shared/components/TableV2/column-widths'; import { useColumnState } from '~/shared/components/TableV2/hooks/useColumnState'; import { useInfiniteScroll } from '~/shared/components/TableV2/hooks/useInfiniteScroll'; import { useResponsiveColumns } from '~/shared/components/TableV2/hooks/useResponsiveColumns'; import { useTable } from '~/shared/components/TableV2/hooks/useTable'; import { useVirtualization } from '~/shared/components/TableV2/hooks/useVirtualization'; import { Table } from '~/shared/components/TableV2/Table'; import { type ColumnDefinition } from '~/shared/components/TableV2/types'; jest.mock('~/shared/hooks', () => ({ getParentScrollableElement: jest.fn().mockReturnValue(null), useLayoutResizeObserver: jest.fn(), })); jest.mock('~/shared/components/TableV2/hooks/useTable'); jest.mock('~/shared/components/TableV2/hooks/useColumnState'); jest.mock('~/shared/components/TableV2/hooks/useResponsiveColumns'); jest.mock('~/shared/components/TableV2/hooks/useVirtualization'); jest.mock('~/shared/components/TableV2/hooks/useInfiniteScroll'); jest.mock('~/shared/components/TableV2/TableHeader', () => ({ TableHeader: () => , })); jest.mock('~/shared/components/TableV2/TableBody', () => ({ TableBody: () => , })); jest.mock('~/shared/components/TableV2/column-widths', () => ({ computeColumnWidths: jest.fn().mockReturnValue([]), })); type TestData = { id: string; name: string }; const columns: ColumnDefinition[] = [ { id: 'name', header: 'Name', accessorFn: (row) => row.name }, { id: 'id', header: 'ID', accessorFn: (row) => row.id }, ]; const defaultProps = { data: [{ id: '1', name: 'App 1' }] as TestData[], columns, getRowId: (row: TestData) => row.id, 'aria-label': 'Test table', }; beforeEach(() => { jest.mocked(useTable).mockReturnValue({ table: { getVisibleLeafColumns: () => [1, 2, 3] } as never, rows: [], }); jest.mocked(useColumnState).mockReturnValue({ columnState: { visibleColumns: ['name', 'id'], columnOrder: ['name', 'id'] }, setColumnState: jest.fn(), }); jest.mocked(useResponsiveColumns).mockReturnValue({ columnVisibility: {}, }); jest.mocked(useVirtualization).mockReturnValue({ virtualizer: { getTotalSize: () => 500 } as never, virtualRows: [], }); }); describe('Table', () => { it('renders TableHeader and TableBody', () => { render(); expect(screen.getByTestId('table-header')).toBeInTheDocument(); expect(screen.getByTestId('table-body')).toBeInTheDocument(); }); it('has data-test="table-v2" on the outer element', () => { render(
); expect(screen.getByTestId('table-v2')).toBeInTheDocument(); }); it('renders a PF Table with the provided aria-label', () => { render(
); expect(screen.getByRole('grid', { name: 'Test table' })).toBeInTheDocument(); }); it('calls useColumnState with columnStateKey and columns', () => { render(
); expect(useColumnState).toHaveBeenCalledWith('my-table', columns); }); it('calls useResponsiveColumns with columns', () => { render(
); expect(useResponsiveColumns).toHaveBeenCalledWith(columns); }); it('calls useTable with correct options', () => { const setColumnState = jest.fn(); jest.mocked(useColumnState).mockReturnValue({ columnState: { visibleColumns: ['name', 'id'], columnOrder: ['name', 'id'] }, setColumnState, }); jest.mocked(useResponsiveColumns).mockReturnValue({ columnVisibility: { name: true }, }); render(
); expect(useTable).toHaveBeenCalledWith( expect.objectContaining({ data: defaultProps.data, columns, getRowId: defaultProps.getRowId, columnState: { visibleColumns: ['name', 'id'], columnOrder: ['name', 'id'] }, setColumnState, responsiveColumnVisibility: { name: true }, enableSorting: true, enableExpansion: true, meta: { key: 'val' }, }), ); }); it('calls useVirtualization with the row count', () => { jest.mocked(useTable).mockReturnValue({ table: { getVisibleLeafColumns: () => [1, 2] } as never, rows: [{}, {}, {}] as never[], }); render(
); expect(useVirtualization).toHaveBeenCalledWith( expect.objectContaining({ count: 3, }), ); }); it('calls useInfiniteScroll when hasNextPage is provided', () => { const fetchNextPage = jest.fn(); render(
, ); expect(useInfiniteScroll).toHaveBeenCalledWith( expect.objectContaining({ virtualRows: [], totalCount: 0, hasNextPage: true, isFetchingNextPage: false, fetchNextPage, scrollElement: null, }), ); }); it('calls useInfiniteScroll with hasNextPage=false when not provided', () => { render(
); expect(useInfiniteScroll).toHaveBeenCalledWith( expect.objectContaining({ virtualRows: [], totalCount: 0, hasNextPage: false, isFetchingNextPage: false, scrollElement: null, }), ); }); it('renders the outer div without inline scroll styles', () => { render(
); const container = screen.getByTestId('table-v2'); expect(container.tagName).toBe('DIV'); expect(container.style.overflow).toBe(''); }); it('passes columnWidths from computeColumnWidths to TableHeader', () => { const mockWidths = [{ id: 'name', type: 'flex' as const, widthPercent: 50 }]; jest.mocked(computeColumnWidths).mockReturnValue(mockWidths); render(
); expect(computeColumnWidths).toHaveBeenCalledWith(columns, ['name', 'id']); }); describe('row selection', () => { it('passes enableRowSelection and onRowSelectionChange to useTable', () => { const onRowSelectionChange = jest.fn(); render(
, ); expect(useTable).toHaveBeenCalledWith( expect.objectContaining({ enableRowSelection: true, onRowSelectionChange, }), ); }); it('does not pass enableRowSelection to useTable when omitted', () => { render(
); expect(useTable).toHaveBeenCalledWith( expect.not.objectContaining({ enableRowSelection: true, }), ); }); }); });