import { render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PreviewHeader } from '../header'
// Tests for PreviewHeader - displays a title and optional children
describe('PreviewHeader', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('Rendering', () => {
it('should render the title text', () => {
render()
expect(screen.getByText('Preview Title')).toBeInTheDocument()
})
it('should render children below the title', () => {
render(
Child content
,
)
expect(screen.getByText('Title')).toBeInTheDocument()
expect(screen.getByText('Child content')).toBeInTheDocument()
})
it('should render without children', () => {
const { container } = render()
expect(container.firstElementChild).toBeInTheDocument()
expect(screen.getByText('Solo Title')).toBeInTheDocument()
})
it('should render title in an inner div with uppercase styling', () => {
render()
const titleEl = screen.getByText('Styled Title')
expect(titleEl).toHaveClass('uppercase', 'mb-1', 'px-1', 'text-text-accent')
})
})
describe('Props', () => {
it('should apply custom className to outer div', () => {
render()
expect(screen.getByTestId('header')).toHaveClass('custom-header')
})
it('should pass rest props to the outer div', () => {
render()
const el = screen.getByTestId('header')
expect(el).toHaveAttribute('id', 'header-1')
expect(el).toHaveAttribute('aria-label', 'preview header')
})
it('should render with empty string title', () => {
render()
const header = screen.getByTestId('header')
// Title div exists but is empty
const titleDiv = header.querySelector('.uppercase')
expect(titleDiv).toBeInTheDocument()
expect(titleDiv?.textContent).toBe('')
})
})
describe('Structure', () => {
it('should render as a div element', () => {
render()
expect(screen.getByTestId('header').tagName).toBe('DIV')
})
it('should have title div as the first child', () => {
render()
const header = screen.getByTestId('header')
const firstChild = header.firstElementChild
expect(firstChild).toHaveTextContent('Title')
})
it('should place children after the title div', () => {
render(
,
)
const header = screen.getByTestId('header')
const children = Array.from(header.children)
expect(children).toHaveLength(2)
expect(children[0]).toHaveTextContent('Title')
expect(children[1]).toHaveTextContent('Action')
})
})
describe('Edge Cases', () => {
it('should handle special characters in title', () => {
render()
expect(screen.getByText('Test & \'Characters\'')).toBeInTheDocument()
})
it('should handle long titles', () => {
const longTitle = 'A'.repeat(500)
render()
expect(screen.getByText(longTitle)).toBeInTheDocument()
})
it('should render multiple children', () => {
render(
First
Second
,
)
expect(screen.getByText('First')).toBeInTheDocument()
expect(screen.getByText('Second')).toBeInTheDocument()
})
it('should render with null children', () => {
render({null})
expect(screen.getByText('Title')).toBeInTheDocument()
})
it('should not crash on re-render with different title', () => {
const { rerender } = render()
rerender()
expect(screen.queryByText('First Title')).not.toBeInTheDocument()
expect(screen.getByText('Second Title')).toBeInTheDocument()
})
})
})