import { render, screen, fireEvent } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest'
import SectionHeader from './SectionHeader'

describe('SectionHeader', () => {
  it('renders title', () => {
    render(<SectionHeader title="标题" />)
    expect(screen.getByText('标题')).toBeTruthy()
  })

  it('renders back button when onBack provided', () => {
    const onBack = vi.fn()
    render(<SectionHeader title="标题" onBack={onBack} />)
    const btn = screen.getByText('返回')
    expect(btn).toBeTruthy()
    fireEvent.click(btn)
    expect(onBack).toHaveBeenCalled()
  })

  it('uses custom backLabel', () => {
    render(<SectionHeader title="标题" onBack={vi.fn()} backLabel="上一步" />)
    expect(screen.getByText('上一步')).toBeTruthy()
  })

  it('does not render back button when onBack not provided', () => {
    const { container } = render(<SectionHeader title="标题" />)
    const buttons = container.querySelectorAll('button')
    expect(buttons).toHaveLength(0)
  })

  it('renders actions', () => {
    render(<SectionHeader title="标题" actions={<button>刷新</button>} />)
    expect(screen.getByText('刷新')).toBeTruthy()
  })
})
