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

import TagSearchBox from '@/components/TagSearchBox'
import { buildTag } from '@/test/factories'
import { renderWithProviders } from '@/test/render'

describe ('TagSearchBox', () => {
  it ('renders suggestions and selects tags on mouse down', () => {
    const handleSelect = vi.fn ()
    const tag = buildTag ({ id: 9, name: '候補', postCount: 2 })

    renderWithProviders (
        <TagSearchBox suggestions={[tag]} activeIndex={0} onSelect={handleSelect}/>,
    )

    fireEvent.mouseDown (screen.getByText ('候補'))

    expect (handleSelect).toHaveBeenCalledWith (tag)
    expect (screen.getByText ('2')).toBeInTheDocument ()
  })

  it ('renders nothing when suggestions are empty', () => {
    const { container } = renderWithProviders (
        <TagSearchBox suggestions={[]} activeIndex={-1} onSelect={vi.fn ()}/>,
    )

    expect (container).toBeEmptyDOMElement ()
  })
})
