import { fireEvent, screen, within } from '@testing-library/react'; import { describe, expect, it } from 'vitest'; import Header from '../components/ui/Header'; import { renderWithRouter } from './utils'; describe('Header', () => { it('renders the brand with the logo', () => { renderWithRouter(
); const brand = screen.getByRole('link', { name: 'newzy' }); expect(brand).toHaveAttribute('href', '/'); expect(brand.querySelector('img')).toHaveAttribute('src', expect.stringContaining('logo')); }); it('renders the primary navigation with router links and lucide icons', () => { renderWithRouter(
); const nav = screen.getByRole('navigation', { name: 'Main navigation' }); expect(within(nav).getByRole('link', { name: /home/i })).toHaveAttribute('href', '/'); expect(within(nav).getByRole('link', { name: /articles/i })).toHaveAttribute('href', '/articles'); expect(within(nav).getByRole('link', { name: /categories/i })).toHaveAttribute('href', '/categories'); // Lucide icons render as inline SVGs (one per nav item). expect(nav.querySelectorAll('svg').length).toBeGreaterThanOrEqual(NAV_ITEM_COUNT); }); it('marks the active route in the navigation', () => { renderWithRouter(
, { route: '/categories' }); const nav = screen.getByRole('navigation', { name: 'Main navigation' }); expect(within(nav).getByRole('link', { name: /categories/i })).toHaveClass('bg-blue-50'); expect(within(nav).getByRole('link', { name: /home/i })).not.toHaveClass('bg-blue-50'); }); it('toggles the mobile menu', () => { renderWithRouter(
); // Mobile menu is closed by default. expect(screen.queryByRole('navigation', { name: 'Mobile navigation' })).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'Open menu' })); const mobileNav = screen.getByRole('navigation', { name: 'Mobile navigation' }); expect(mobileNav).toBeInTheDocument(); expect(within(mobileNav).getByRole('link', { name: /articles/i })).toHaveAttribute( 'href', '/articles', ); fireEvent.click(screen.getByRole('button', { name: 'Close menu' })); expect( screen.queryByRole('navigation', { name: 'Mobile navigation' }), ).not.toBeInTheDocument(); }); }); const NAV_ITEM_COUNT = 3;