import React from 'react'; import {createMockDomains, render, screen} from '../../test-utils'; import {CollectionTable} from './CollectionTable'; // Mock the TagsContext jest.mock('../../Tags', () => ({ TagsContext: { _currentValue: { SingleTag: ({name}) => {name} } }, })); // Mock the Media component to test both mobile and desktop views jest.mock('../../contexts/contexts', () => ({ ...jest.requireActual('../../contexts/contexts'), Media: ({children, at, greaterThanOrEqual}) => { // Default to desktop view for most tests if (greaterThanOrEqual === 'tablet') { return
{children}
; } if (at === 'mobile') { return
{children}
; } return children; }, })); // Mock SortableTable to simplify testing. Uses the real `Table` compound // components (rather than raw //
) because `rowFunc` returns // `Table.Row`/`Table.Cell` elements that read Mantine's table context. jest.mock('../SortableTable', () => { const {Table} = jest.requireActual('../ui'); return { SortableTable: ({data, rowFunc, tableHeaders}) => ( {tableHeaders.map(h => ( {h.text} ))} {data.map(item => rowFunc(item))}
), }; }); // Test column and routes configurations const DOMAIN_COLUMNS = [ {key: 'domain', label: 'Domain', sortable: true, width: 7}, {key: 'archive_count', label: 'Archives', sortable: true, align: 'right', width: 2}, {key: 'size', label: 'Size', sortable: true, align: 'right', format: 'bytes', width: 2, hideOnMobile: true}, {key: 'tag_name', label: 'Tag', sortable: true, width: 2}, {key: 'actions', label: 'Manage', sortable: false, type: 'actions', width: 1} ]; const DOMAIN_ROUTES = { list: '/archives/domains', edit: '/archives/domain/:id/edit', search: '/archives', searchParam: 'domain' }; describe('CollectionTable', () => { const mockCollections = createMockDomains(3); describe('Column render', () => { it('displays what a column renders, in the desktop and mobile rows', () => { const columns = [ {key: 'name', label: 'Name', render: (c) => {c.name}!}, {key: 'branch', label: 'Branch', render: (c) => c.branch || c.default_branch}, ]; render(); // Desktop: the rendered name is still the link to the collection. expect(screen.getByTestId('rendered-name')).toHaveTextContent('kiwix-tools!'); expect(screen.getByTestId('rendered-name').closest('a')).toHaveAttribute('href', '/repos/1'); expect(screen.getByText('main')).toBeInTheDocument(); // Mobile lists the other columns by label. expect(screen.getByText('Branch: main')).toBeInTheDocument(); }); }); describe('Loading and Error States', () => { it('renders loading placeholder when collections is null', () => { const {container} = render( ); expect(container.querySelector('.mantine-Skeleton-root')).toBeInTheDocument(); }); it('renders error message when collections is undefined', () => { render( ); expect(screen.getByText(/could not fetch collections/i)).toBeInTheDocument(); }); it('renders empty message when collections is empty array', () => { render( ); expect(screen.getByText(/no items yet/i)).toBeInTheDocument(); }); it('renders custom empty message', () => { render( ); expect(screen.getByText(/no domains found/i)).toBeInTheDocument(); }); it('renders warning when columns is null', () => { render( ); expect(screen.getByText(/no columns configured/i)).toBeInTheDocument(); }); it('renders warning when columns is empty', () => { render( ); expect(screen.getByText(/no columns configured/i)).toBeInTheDocument(); }); }); describe('Table Rendering', () => { it('renders table with collection data', () => { render( ); // Should render the sortable table expect(screen.getAllByTestId('sortable-table')).toHaveLength(2); // mobile + desktop }); it('renders correct number of rows', () => { render( ); // Each collection should have a row in desktop view const desktopView = screen.getByTestId('desktop-view'); const rows = desktopView.querySelectorAll('tbody tr'); expect(rows).toHaveLength(mockCollections.length); }); it('renders Edit links for each collection', () => { render( ); const editLinks = screen.getAllByText('Edit'); // Should have edit links for both mobile and desktop views expect(editLinks.length).toBeGreaterThan(0); }); }); describe('Search Filtering', () => { it('filters collections by search string', () => { render( ); const desktopView = screen.getByTestId('desktop-view'); const rows = desktopView.querySelectorAll('tbody tr'); // Should only show matching collection expect(rows).toHaveLength(1); }); it('shows all collections when search string is empty', () => { render( ); const desktopView = screen.getByTestId('desktop-view'); const rows = desktopView.querySelectorAll('tbody tr'); expect(rows).toHaveLength(mockCollections.length); }); it('handles case-insensitive search', () => { render( ); const desktopView = screen.getByTestId('desktop-view'); const rows = desktopView.querySelectorAll('tbody tr'); expect(rows).toHaveLength(1); }); }); describe('Column Widths', () => { it('uses explicit widths from column config', () => { render( ); const desktopView = screen.getByTestId('desktop-view'); const headers = desktopView.querySelectorAll('th'); // DOMAIN_COLUMNS has explicit widths: domain=7, archive_count=2, size=2, tag_name=2, actions=1 expect(headers[0]).toHaveAttribute('data-width', '7'); expect(headers[1]).toHaveAttribute('data-width', '2'); expect(headers[2]).toHaveAttribute('data-width', '2'); expect(headers[3]).toHaveAttribute('data-width', '2'); expect(headers[4]).toHaveAttribute('data-width', '1'); }); it('uses widths from 4-column config', () => { const fourColumns = [ {key: 'name', label: 'Name', sortable: true, width: 9}, {key: 'count', label: 'Count', sortable: true, align: 'right', width: 2}, {key: 'size', label: 'Size', sortable: true, format: 'bytes', width: 2}, {key: 'actions', label: 'Manage', type: 'actions', width: 1}, ]; render( ); const desktopView = screen.getByTestId('desktop-view'); const headers = desktopView.querySelectorAll('th'); expect(headers[0]).toHaveAttribute('data-width', '9'); expect(headers[1]).toHaveAttribute('data-width', '2'); expect(headers[2]).toHaveAttribute('data-width', '2'); expect(headers[3]).toHaveAttribute('data-width', '1'); }); }); describe('Row Click Handler', () => { it('calls onRowClick when row is clicked', () => { const mockOnRowClick = jest.fn(); render( ); const desktopView = screen.getByTestId('desktop-view'); const firstRow = desktopView.querySelector('tbody tr'); firstRow.click(); expect(mockOnRowClick).toHaveBeenCalledWith(mockCollections[0]); }); it('adds clickable class when onRowClick is provided', () => { const mockOnRowClick = jest.fn(); render( ); const desktopView = screen.getByTestId('desktop-view'); const firstRow = desktopView.querySelector('tbody tr'); expect(firstRow).toHaveClass('clickable'); }); it('does not add clickable class when onRowClick is not provided', () => { render( ); const desktopView = screen.getByTestId('desktop-view'); const firstRow = desktopView.querySelector('tbody tr'); expect(firstRow).not.toHaveClass('clickable'); }); }); describe('Value Formatting', () => { it('formats bytes values using humanFileSize', () => { const collectionsWithSize = [{ id: 1, domain: 'test.com', size: 1048576, // 1 MB archive_count: 5, }]; render( ); // humanFileSize(1048576) should return "1.0 MB" // Multiple elements found (mobile + desktop), use getAllByText const sizeElements = screen.getAllByText(/1\.0 MB/i); expect(sizeElements.length).toBeGreaterThan(0); }); it('renders SingleTag for tag_name column', () => { const collectionsWithTag = [{ id: 1, domain: 'test.com', tag_name: 'News', size: 1000, archive_count: 5, }]; render( ); expect(screen.getAllByTestId('single-tag')[0]).toHaveTextContent('News'); }); }); describe('Mobile View', () => { it('hides columns with hideOnMobile in mobile view', () => { const collectionsWithData = [{ id: 1, domain: 'test.com', archive_count: 5, size: 1048576, tag_name: 'News', }]; render( ); const mobileView = screen.getByTestId('mobile-view'); const mobileHeaders = mobileView.querySelectorAll('th'); // Mobile uses simplified 2-column layout: primary column + Manage expect(mobileHeaders).toHaveLength(2); expect(mobileHeaders[0]).toHaveTextContent('Domain'); expect(mobileHeaders[1]).toHaveTextContent('Manage'); // Archives should be visible in row content (no hideOnMobile) expect(mobileView).toHaveTextContent('Archives:'); // Size should be hidden in row content (hideOnMobile: true) expect(mobileView).not.toHaveTextContent('Size:'); }); }); describe('Links', () => { it('renders search link on primary column when searchParam is configured', () => { render( ); // Domain column should link to search with domain parameter const desktopView = screen.getByTestId('desktop-view'); const domainLink = desktopView.querySelector('a[href*="domain="]'); expect(domainLink).toBeInTheDocument(); }); it('renders edit link in actions column', () => { render( ); const editLinks = screen.getAllByRole('link', {name: /edit/i}); expect(editLinks.length).toBeGreaterThan(0); expect(editLinks[0]).toHaveAttribute('href', expect.stringContaining('/edit')); }); }); });