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

import DateTimeField from '@/components/common/DateTimeField'

describe ('DateTimeField', () => {
  it ('renders an ISO value as a datetime-local value', () => {
    render (<DateTimeField aria-label="日時" value="2026-01-02T03:04:05.000Z"/>)

    const input = screen.getByLabelText ('日時')

    expect (input).toHaveValue ('2026-01-02T12:04')
  })

  it ('reports local changes as ISO strings and empty values as null', () => {
    const handleChange = vi.fn ()
    render (<DateTimeField aria-label="日時" onChange={handleChange}/>)

    const input = screen.getByLabelText ('日時')
    fireEvent.change (input, { target: { value: '2026-01-02T03:04' } })
    fireEvent.change (input, { target: { value: '' } })

    const first = handleChange.mock.calls[0]?.[0]
    expect (new Date (first).getFullYear ()).toBe (2026)
    expect (handleChange).toHaveBeenLastCalledWith (null)
  })
})
