import { render, screen, fireEvent } from '@testing-library/react'
import { ErrorState } from '../states/ErrorState'
import { EmptyState } from '../states/EmptyState'
describe('ErrorState', () => {
it('renders title and message', () => {
render()
expect(screen.getByText('Something went wrong')).toBeInTheDocument()
expect(screen.getByText('Something broke')).toBeInTheDocument()
})
it('renders custom title', () => {
render()
expect(screen.getByText('Custom Error')).toBeInTheDocument()
})
it('renders with role="alert" and aria-live', () => {
const { container } = render()
const alertEl = container.querySelector('[role="alert"]')
expect(alertEl).toBeInTheDocument()
expect(alertEl).toHaveAttribute('aria-live', 'assertive')
})
it('renders retry button when onRetry is provided', () => {
const onRetry = jest.fn()
render()
const retryButton = screen.getByLabelText('Retry loading content')
expect(retryButton).toBeInTheDocument()
fireEvent.click(retryButton)
expect(onRetry).toHaveBeenCalledTimes(1)
})
it('does not render retry button without onRetry', () => {
render()
expect(screen.queryByText('Try Again')).not.toBeInTheDocument()
})
it('applies custom className', () => {
const { container } = render(
)
expect(container.firstChild).toHaveClass('custom-class')
})
})
describe('EmptyState', () => {
it('renders title', () => {
render()
expect(screen.getByText('No items')).toBeInTheDocument()
})
it('renders description when provided', () => {
render(
)
expect(screen.getByText('Add some items to get started')).toBeInTheDocument()
})
it('does not render description when not provided', () => {
const { container } = render()
const paragraphs = container.querySelectorAll('p')
expect(paragraphs).toHaveLength(0)
})
it('renders action button when action is provided', () => {
const onClick = jest.fn()
render(
)
const button = screen.getByLabelText('Add Item')
expect(button).toBeInTheDocument()
fireEvent.click(button)
expect(onClick).toHaveBeenCalledTimes(1)
})
it('does not render action button without action', () => {
render()
expect(screen.queryByRole('button')).not.toBeInTheDocument()
})
it('has role="status" and aria-live="polite"', () => {
const { container } = render()
const statusEl = container.querySelector('[role="status"]')
expect(statusEl).toBeInTheDocument()
expect(statusEl).toHaveAttribute('aria-live', 'polite')
})
})