import { render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import PreviewContainer from '../container'
// Tests for PreviewContainer - a layout wrapper with header and scrollable main area
describe('PreviewContainer', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('Rendering', () => {
it('should render header content in a header element', () => {
render(Header Title}>Body)
expect(screen.getByText('Header Title')).toBeInTheDocument()
const headerEl = screen.getByText('Header Title').closest('header')
expect(headerEl).toBeInTheDocument()
})
it('should render children in a main element', () => {
render(Main content)
const mainEl = screen.getByRole('main')
expect(mainEl).toHaveTextContent('Main content')
})
it('should render both header and children simultaneously', () => {
render(
My Header}>
Body paragraph
,
)
expect(screen.getByText('My Header')).toBeInTheDocument()
expect(screen.getByText('Body paragraph')).toBeInTheDocument()
})
it('should render without children', () => {
render()
expect(screen.getByRole('main')).toBeInTheDocument()
expect(screen.getByRole('main').childElementCount).toBe(0)
})
})
describe('Props', () => {
it('should apply className to the outer wrapper div', () => {
const { container } = render(
Content,
)
expect(container.firstElementChild).toHaveClass('outer-class')
})
it('should apply mainClassName to the main element', () => {
render(
Content,
)
const mainEl = screen.getByRole('main')
expect(mainEl).toHaveClass('custom-main')
// Default classes should still be present
expect(mainEl).toHaveClass('w-full', 'grow', 'overflow-y-auto', 'px-6', 'py-5')
})
it('should forward ref to the inner container div', () => {
const ref = vi.fn()
render(
Content,
)
expect(ref).toHaveBeenCalled()
const refArg = ref.mock.calls[0][0]
expect(refArg).toBeInstanceOf(HTMLDivElement)
})
it('should pass rest props to the inner container div', () => {
render(
Content
,
)
const inner = screen.getByTestId('inner-container')
expect(inner).toHaveAttribute('id', 'container-1')
})
it('should render ReactNode as header', () => {
render(
Complex}>
Content
,
)
expect(screen.getByTestId('complex-header')).toBeInTheDocument()
expect(screen.getByText('Complex')).toBeInTheDocument()
})
})
// Layout structure tests
describe('Layout Structure', () => {
it('should have header with border-b styling', () => {
render(Content)
const headerEl = screen.getByText('Header').closest('header')
expect(headerEl).toHaveClass('border-b', 'border-divider-subtle')
})
it('should have inner div with flex column layout', () => {
render(
Content,
)
const inner = screen.getByTestId('inner')
expect(inner).toHaveClass('flex', 'h-full', 'w-full', 'flex-col')
})
it('should have main with overflow-y-auto for scrolling', () => {
render(Content)
expect(screen.getByRole('main')).toHaveClass('overflow-y-auto')
})
})
// DisplayName test
describe('DisplayName', () => {
it('should have correct displayName', () => {
expect(PreviewContainer.displayName).toBe('PreviewContainer')
})
})
describe('Edge Cases', () => {
it('should render with empty string header', () => {
render(Content)
const headerEl = screen.getByRole('banner')
expect(headerEl).toBeInTheDocument()
})
it('should render with null children', () => {
render({null})
expect(screen.getByRole('main')).toBeInTheDocument()
})
it('should render with multiple children', () => {
render(
Child 1
Child 2
Child 3
,
)
expect(screen.getByText('Child 1')).toBeInTheDocument()
expect(screen.getByText('Child 2')).toBeInTheDocument()
expect(screen.getByText('Child 3')).toBeInTheDocument()
})
it('should not crash on re-render with different props', () => {
const { rerender } = render(
Content A,
)
rerender(
Content B,
)
expect(screen.getByText('Second')).toBeInTheDocument()
expect(screen.getByText('Content B')).toBeInTheDocument()
})
})
})