import { Table } from '@patternfly/react-table';
import { render, screen, within } from '@testing-library/react';
import { TableBody } from '~/shared/components/TableV2/TableBody';
import { createMockRow, createMockVirtualRows } from '~/unit-test-utils';
jest.mock('~/shared/components/TableV2/TableRow', () => ({
TableRow: ({ rowId }: { rowId: string }) => (
| Row {rowId} |
),
}));
const mockMeasureElement = jest.fn();
const renderTableBody = (props: Partial> = {}) => {
const rows = props.rows ?? [createMockRow('r-0'), createMockRow('r-1'), createMockRow('r-2')];
const virtualRows = props.virtualRows ?? createMockVirtualRows(rows.length);
return render(
row.id}
visibleColumnCount={3}
{...props}
/>
,
);
};
describe('TableBody', () => {
it('renders only the virtualized rows, not all rows', () => {
const allRows = Array.from({ length: 10 }, (_, i) => createMockRow(`r-${i}`));
const virtualRows = createMockVirtualRows(3); // only 3 virtual items
renderTableBody({ rows: allRows as never[], virtualRows });
const renderedRows = screen.getAllByTestId('table-row');
expect(renderedRows).toHaveLength(3);
});
it('has data-test="table-body" on Tbody', () => {
renderTableBody();
expect(screen.getByTestId('table-body')).toBeInTheDocument();
});
it('passes correct rowId to each TableRow', () => {
const rows = [createMockRow('app-1'), createMockRow('app-2')];
const virtualRows = createMockVirtualRows(2);
renderTableBody({ rows: rows as never[], virtualRows });
const renderedRows = screen.getAllByTestId('table-row');
expect(renderedRows[0]).toHaveAttribute('data-id', 'app-1');
expect(renderedRows[1]).toHaveAttribute('data-id', 'app-2');
});
it('renders expanded content when row is expanded', () => {
const rows = [createMockRow('r-0', { expanded: true })];
const virtualRows = createMockVirtualRows(1);
renderTableBody({
rows: rows as never[],
virtualRows,
enableExpansion: true,
expandedContent: (row: { id: string }) => (
Details for {row.id}
),
});
expect(screen.getByTestId('expanded')).toBeInTheDocument();
expect(screen.getByText('Details for r-0')).toBeInTheDocument();
});
it('marks the row group and expanded row with PatternFly expansion state', () => {
const rows = [createMockRow('r-0', { expanded: true })];
const virtualRows = createMockVirtualRows(1);
renderTableBody({
rows: rows as never[],
virtualRows,
enableExpansion: true,
expandedContent: () => details
,
});
const expandedRow = screen.getByText('details').closest('tr');
expect(expandedRow).toHaveClass('pf-v6-c-table__expandable-row', 'pf-m-expanded');
const expandedBody = screen
.getAllByRole('rowgroup')
.find((rowGroup) => rowGroup.classList.contains('pf-m-expanded'));
expect(expandedBody).toBeDefined();
});
it('renders an explicit divider after expandable row groups', () => {
const rows = [createMockRow('r-0')];
const virtualRows = createMockVirtualRows(1);
renderTableBody({ rows: rows as never[], virtualRows, enableExpansion: true });
expect(document.querySelector('tr.pf-m-border-row')).toBeInTheDocument();
});
it('does not render expanded content when row is not expanded', () => {
const rows = [createMockRow('r-0')];
const virtualRows = createMockVirtualRows(1);
renderTableBody({
rows: rows as never[],
virtualRows,
enableExpansion: true,
expandedContent: (row: { id: string }) => (
Details for {row.id}
),
});
expect(screen.queryByTestId('expanded')).not.toBeInTheDocument();
});
it('sets correct colspan on expanded content', () => {
const rows = [createMockRow('r-0', { expanded: true })];
const virtualRows = createMockVirtualRows(1);
renderTableBody({
rows: rows as never[],
virtualRows,
enableExpansion: true,
visibleColumnCount: 5,
expandedContent: () => details
,
});
// The expansion cell is rendered separately so content aligns with the data columns.
const expandedRow = screen.getByText('details').closest('tr') as HTMLElement;
const cells = within(expandedRow).getAllByRole('cell');
expect(cells).toHaveLength(2);
expect(cells[0]).not.toHaveAttribute('colspan');
expect(cells[1]).toHaveAttribute('colspan', '4');
});
it('renders 3 skeleton rows when isFetchingNextPage is true', () => {
renderTableBody({ isFetchingNextPage: true, visibleColumnCount: 4 });
const skeletonRows = screen.getAllByTestId('table-loading-more');
expect(skeletonRows).toHaveLength(3);
// Each skeleton row should have one skeleton per visible column
skeletonRows.forEach((row) => {
const skeletons = row.querySelectorAll('.pf-v6-c-skeleton');
expect(skeletons).toHaveLength(4);
});
});
it('does not render loading indicator when isFetchingNextPage is false', () => {
renderTableBody({ isFetchingNextPage: false });
expect(screen.queryByTestId('table-loading-more')).not.toBeInTheDocument();
});
it('attaches measureElement ref and data-index to per-row tbody wrappers', () => {
mockMeasureElement.mockClear();
const rows = [createMockRow('r-0'), createMockRow('r-1')];
const virtualRows = createMockVirtualRows(2);
renderTableBody({ rows: rows as never[], virtualRows });
// measureElement should be called once per virtual row
expect(mockMeasureElement).toHaveBeenCalledTimes(2);
// Each call should receive a element with the correct data-index
const calls = mockMeasureElement.mock.calls;
for (let i = 0; i < calls.length; i++) {
const el = calls[i][0] as HTMLElement;
expect(el.tagName).toBe('TBODY');
expect(el).toHaveAttribute('data-index', String(i));
}
});
});