import { render, screen } from '../../test-utils'; import userEvent from '@testing-library/user-event'; import { ListItem, type ListItemProps } from '../ListItem'; describe('ListItem.Navigation', () => { const renderWith = (overrides: Partial = {}) => render(); describe('as button', () => { it('renders as button if onClick is set but no href', () => { renderWith({ control: {}} /> }); expect(screen.getByRole('button')).toBeInTheDocument(); }); it('handles onClick events', async () => { const handleClick = jest.fn(); renderWith({ control: }); await userEvent.click(screen.getByRole('button')); expect(handleClick).toHaveBeenCalledTimes(1); }); it('respects disabled state', async () => { renderWith({ disabled: true, control: , }); expect(screen.queryByRole('button')).toBeDisabled(); expect(screen.queryByRole('link')).not.toBeInTheDocument(); }); it('passes data-* attributes to the button', () => { renderWith({ control: {}} />, }); expect(screen.getByRole('button')).toHaveAttribute('data-tracking-id', 'nav-item'); }); }); describe('hideIndicator', () => { it('renders the chevron by default for a button', () => { renderWith({ control: {}} /> }); expect(screen.getByTestId('NavigationIndicator')).toBeInTheDocument(); }); it('renders the chevron by default for a link', () => { renderWith({ control: }); expect(screen.getByTestId('NavigationIndicator')).toBeInTheDocument(); }); it('hides the chevron when hideIndicator is true on a button', () => { renderWith({ control: {}} /> }); expect(screen.queryByTestId('NavigationIndicator')).not.toBeInTheDocument(); }); it('hides the chevron when hideIndicator is true on a link', () => { renderWith({ control: }); expect(screen.getByRole('link')).toBeInTheDocument(); expect(screen.queryByTestId('NavigationIndicator')).not.toBeInTheDocument(); }); it('does not apply hasControl grid class when hideIndicator is true on a link', () => { const { container } = renderWith({ control: , }); expect(container.querySelector('.wds-list-item-hasMedia-hasControl')).not.toBeInTheDocument(); expect(container.querySelector('.wds-list-item-noMedia-hasControl')).not.toBeInTheDocument(); }); it('still renders the button control when hideIndicator is true without href', () => { renderWith({ control: {}} /> }); expect(screen.getByRole('button')).toBeInTheDocument(); }); }); describe('as link', () => { it('renders a link if href is set', () => { renderWith({ control: }); expect(screen.queryByRole('button')).not.toBeInTheDocument(); expect(screen.getByRole('link')).toBeInTheDocument(); }); it('respects target prop', () => { renderWith({ control: , }); const link = screen.getByRole('link'); expect(link).toHaveAttribute('target', '_blank'); expect(link).toHaveAttribute('rel', 'noopener noreferrer'); }); it('handles onClick events', async () => { const handleClick = jest.fn(); renderWith({ control: }); await userEvent.click(screen.getByRole('link')); expect(handleClick).toHaveBeenCalledTimes(1); }); }); });