import { fireEvent, screen, within } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import type { Mock } from 'vitest'; import { me } from '../api/client'; import Header from '../components/ui/Header'; import { renderWithAuth } from './utils'; vi.mock('../api/client', () => ({ TOKEN_KEY: 'AUTH_TOKEN', login: vi.fn(), register: vi.fn(), me: vi.fn(), logout: vi.fn(), })); describe('Header', () => { afterEach(() => { localStorage.clear(); vi.clearAllMocks(); }); it('renders the brand with the logo', () => { renderWithAuth(
); 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', () => { renderWithAuth(
); 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', () => { renderWithAuth(
, { 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', () => { renderWithAuth(
); // 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(); }); it('shows sign-in and register links when signed out', async () => { renderWithAuth(
); const accountNav = screen.getByRole('navigation', { name: 'Account' }); expect(await within(accountNav).findByRole('link', { name: 'Sign in' })).toHaveAttribute( 'href', '/login', ); expect(within(accountNav).getByRole('link', { name: /register/i })).toHaveAttribute( 'href', '/register', ); expect(within(accountNav).queryByRole('link', { name: /sign out/i })).not.toBeInTheDocument(); }); it('shows the username and sign-out link when signed in', async () => { (me as Mock).mockResolvedValue({ username: 'alice' }); localStorage.setItem('AUTH_TOKEN', 'stored-token'); renderWithAuth(
); const accountNav = screen.getByRole('navigation', { name: 'Account' }); expect(await within(accountNav).findByText('alice')).toBeInTheDocument(); expect(within(accountNav).getByRole('link', { name: 'Sign out' })).toHaveAttribute( 'href', '/sign-out', ); }); }); const NAV_ITEM_COUNT = 3;