import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { BrowserRouter, MemoryRouter } from 'react-router-dom'
import { Header } from './Header'
const renderWithRouter = (element: React.ReactElement) => {
return render(
{element}
,
)
}
const renderAtPath = (element: React.ReactElement, path: string) => {
return render(
{element}
,
)
}
describe('Header', () => {
it('renders header with default title', () => {
renderWithRouter()
expect(screen.getByTestId('header-title')).toHaveTextContent('Föräldraledigplannerare')
})
it('renders parent name when provided', () => {
renderWithRouter()
expect(screen.getByTestId('parent-name')).toHaveTextContent('Elin')
})
it('renders child select when children provided', () => {
const childrenList = [
{ id: '1', name: 'Emma' },
{ id: '2', name: 'Oscar' },
]
renderWithRouter()
expect(screen.getByTestId('child-select')).toBeInTheDocument()
expect(screen.getByDisplayValue('Emma')).toBeInTheDocument()
})
it('calls onChildSelect when child selection changes', async () => {
const onChildSelect = vi.fn()
const childrenList = [
{ id: '1', name: 'Emma' },
{ id: '2', name: 'Oscar' },
]
renderWithRouter(
,
)
const select = screen.getByTestId('child-select') as HTMLSelectElement
await userEvent.selectOptions(select, '2')
expect(onChildSelect).toHaveBeenCalledWith('2')
})
it('renders navigation links', () => {
renderWithRouter()
expect(screen.getByText('Översikt')).toBeInTheDocument()
expect(screen.getByText('Kalender')).toBeInTheDocument()
expect(screen.getByText('Inställningar')).toBeInTheDocument()
})
it('does not render the retired Plan/Tips nav entries', () => {
renderWithRouter()
expect(screen.queryByText('Planering')).not.toBeInTheDocument()
expect(screen.queryByText('Tips')).not.toBeInTheDocument()
})
it('sets an aria-label on the child select for accessibility', () => {
const childrenList = [
{ id: '1', name: 'Emma' },
{ id: '2', name: 'Oscar' },
]
renderWithRouter()
expect(screen.getByTestId('child-select')).toHaveAttribute('aria-label', 'Välj barn')
})
it('sets an aria-label on the logout button for accessibility', () => {
renderWithRouter()
expect(screen.getByTestId('logout-button')).toHaveAttribute('aria-label', 'Logga ut')
})
it('renders logout button when onLogout provided', () => {
const onLogout = vi.fn()
renderWithRouter()
const button = screen.getByTestId('logout-button')
expect(button).toBeInTheDocument()
})
it('calls onLogout when logout button clicked', async () => {
const onLogout = vi.fn()
renderWithRouter()
const button = screen.getByTestId('logout-button')
await userEvent.click(button)
expect(onLogout).toHaveBeenCalled()
})
it('does not render logout button when not provided', () => {
renderWithRouter()
expect(screen.queryByTestId('logout-button')).not.toBeInTheDocument()
})
it('does not crash and renders the select when selectedChildId is not provided (falls back to "")', () => {
const childrenList = [
{ id: '1', name: 'Emma' },
{ id: '2', name: 'Oscar' },
]
renderWithRouter()
const select = screen.getByTestId('child-select') as HTMLSelectElement
// selectedChildId ?? '' binds the select to a value ('') that matches no
//