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

import PostThumbnailPreview from '@/components/posts/PostThumbnailPreview'

describe ('PostThumbnailPreview', () => {
  it ('keeps an existing blob preview URL unchanged for normal post forms', () => {
    render (<PostThumbnailPreview url="blob:preview" className="h-10 w-10"/>)

    expect (screen.getByRole ('img')).toHaveAttribute ('src', 'blob:preview')
  })

  it ('renders an empty thumbnail frame without text when the URL is empty', () => {
    const { container } = render (
        <PostThumbnailPreview url="" className="h-10 w-10"/>)

    expect (screen.queryByRole ('img')).toBeNull ()
    expect (screen.queryByText ('サムネールを表示できません')).toBeNull ()
    expect (screen.queryByText ('なし')).toBeNull ()
    expect (container.querySelector ('div.rounded.border.bg-muted')).not.toBeNull ()
    expect (container.textContent).toBe ('')
  })

  it ('renders an empty thumbnail frame without text when image loading fails', () => {
    const { container } = render (
        <PostThumbnailPreview url="blob:preview" className="h-10 w-10"/>)

    fireEvent.error (screen.getByRole ('img'))

    expect (screen.queryByRole ('img')).toBeNull ()
    expect (screen.queryByText ('サムネールを表示できません')).toBeNull ()
    expect (screen.queryByText ('なし')).toBeNull ()
    expect (container.querySelector ('div.rounded.border.bg-muted')).not.toBeNull ()
    expect (container.textContent).toBe ('')
  })
})
