import React from 'react'; import {createMockDomains, render, screen} from '../test-utils'; import {DomainsPage} from './Archive'; // Mock the custom hooks jest.mock('../hooks/customHooks', () => ({ ...jest.requireActual('../hooks/customHooks'), useDomains: jest.fn(), useOneQuery: jest.fn(), })); // Mock CollectionTable component jest.mock('./collections/CollectionTable', () => ({ CollectionTable: ({collections, columns, routes, searchStr}) => (
{collections?.length || 0}
{searchStr}
{columns?.length || 0}
{collections?.map((domain) => (
{domain.domain}
))}
), })); // Mock SearchInput component and useTitle jest.mock('./Common', () => ({ ...jest.requireActual('./Common'), SearchInput: ({placeholder, searchStr, onChange, disabled}) => ( onChange(e.target.value)} disabled={disabled} /> ), ErrorMessage: ({children}) =>
{children}
, useTitle: jest.fn(), })); describe('DomainsPage', () => { const {useDomains, useOneQuery} = require('../hooks/customHooks'); const {useTitle} = require('./Common'); beforeEach(() => { // Reset mocks before each test jest.clearAllMocks(); // Default mock implementations useTitle.mockImplementation(() => { }); useOneQuery.mockReturnValue(['', jest.fn()]); }); describe('Page Rendering', () => { it('displays domains page without errors', () => { const mockDomains = createMockDomains(3); useDomains.mockReturnValue([mockDomains, 3]); render(); // Page should render without crashing expect(screen.getByTestId('search-input')).toBeInTheDocument(); expect(screen.getByTestId('collection-table')).toBeInTheDocument(); }); it('renders CollectionTable component with columns config', () => { const mockDomains = createMockDomains(2); useDomains.mockReturnValue([mockDomains, 2]); render(); expect(screen.getByTestId('collection-table')).toBeInTheDocument(); // Should have 6 columns defined in DOMAIN_COLUMNS expect(screen.getByTestId('columns-count')).toHaveTextContent('6'); }); it('shows search input', () => { const mockDomains = createMockDomains(1); useDomains.mockReturnValue([mockDomains, 1]); render(); const searchInput = screen.getByTestId('search-input'); expect(searchInput).toBeInTheDocument(); expect(searchInput).toHaveAttribute('placeholder', 'Domain filter...'); }); }); describe('Domain Display', () => { it('shows all domains from API', () => { const mockDomains = createMockDomains(5); useDomains.mockReturnValue([mockDomains, 5]); render(); // Should render all 5 domains expect(screen.getByTestId('collection-count')).toHaveTextContent('5'); mockDomains.forEach((domain) => { expect(screen.getByTestId(`domain-${domain.id}`)).toBeInTheDocument(); }); }); it('displays domain names', () => { const mockDomains = [ {id: 1, domain: 'example1.com', archive_count: 10, size: 1000}, {id: 2, domain: 'example2.com', archive_count: 20, size: 2000}, {id: 3, domain: 'example3.com', archive_count: 30, size: 3000}, ]; useDomains.mockReturnValue([mockDomains, 3]); render(); expect(screen.getByTestId('domain-name-1')).toHaveTextContent('example1.com'); expect(screen.getByTestId('domain-name-2')).toHaveTextContent('example2.com'); expect(screen.getByTestId('domain-name-3')).toHaveTextContent('example3.com'); }); it('displays Edit buttons in Manage column', () => { const mockDomains = createMockDomains(3); useDomains.mockReturnValue([mockDomains, 3]); render(); // Each domain should have an Edit button mockDomains.forEach((domain) => { expect(screen.getByTestId(`edit-button-${domain.id}`)).toBeInTheDocument(); }); }); it('Edit button has correct styling', () => { const mockDomains = createMockDomains(1); useDomains.mockReturnValue([mockDomains, 1]); render(); const editButton = screen.getByTestId('edit-button-1'); expect(editButton).toHaveClass('ui'); expect(editButton).toHaveClass('mini'); expect(editButton).toHaveClass('primary'); expect(editButton).toHaveClass('button'); }); }); describe('Empty and Error States', () => { it('shows "No items yet" message when no domains', () => { // Empty array indicates no domains useDomains.mockReturnValue([[], 0]); render(); // Should show empty state message expect(screen.getByText(/no domains yet/i)).toBeInTheDocument(); expect(screen.getByText(/archive some webpages/i)).toBeInTheDocument(); // Should not show table expect(screen.queryByTestId('collection-table')).not.toBeInTheDocument(); }); it('shows error message when fetch fails', () => { // undefined indicates error state useDomains.mockReturnValue([undefined, 0]); render(); // Should show error message expect(screen.getByTestId('error-message')).toBeInTheDocument(); expect(screen.getByText(/could not fetch domains/i)).toBeInTheDocument(); // Should not show table expect(screen.queryByTestId('collection-table')).not.toBeInTheDocument(); }); it('does not show "New Domain" button', () => { const mockDomains = createMockDomains(2); useDomains.mockReturnValue([mockDomains, 2]); render(); // Domains are auto-created, so there should be no "New" button expect(screen.queryByRole('button', {name: /new/i})).not.toBeInTheDocument(); expect(screen.queryByRole('button', {name: /create/i})).not.toBeInTheDocument(); expect(screen.queryByRole('button', {name: /add/i})).not.toBeInTheDocument(); }); }); describe('Search Integration', () => { it('disables search when no domains', () => { useDomains.mockReturnValue([[], 0]); render(); const searchInput = screen.getByTestId('search-input'); expect(searchInput).toBeDisabled(); }); it('enables search when domains exist', () => { const mockDomains = createMockDomains(3); useDomains.mockReturnValue([mockDomains, 3]); render(); const searchInput = screen.getByTestId('search-input'); expect(searchInput).not.toBeDisabled(); }); it('passes search string to CollectionTable', () => { const mockDomains = createMockDomains(2); useDomains.mockReturnValue([mockDomains, 2]); const mockSetSearchStr = jest.fn(); useOneQuery.mockReturnValue(['example', mockSetSearchStr]); render(); // Search string should be passed to table expect(screen.getByTestId('search-filter')).toHaveTextContent('example'); }); }); describe('Page Title', () => { it('sets page title correctly', () => { const mockDomains = createMockDomains(1); useDomains.mockReturnValue([mockDomains, 1]); render(); expect(useTitle).toHaveBeenCalledWith('Archive Domains'); }); }); });