/**
 * MonthSelector组件测试
 */
import React from 'react'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import { MonthSelector } from '@/components/MonthSelector'

describe('MonthSelector组件', () => {
  const mockOnPrevious = jest.fn()
  const mockOnNext = jest.fn()

  beforeEach(() => {
    jest.clearAllMocks()
  })

  test('应该渲染当前月份显示', () => {
    render(
      <MonthSelector
        currentMonth="2023年11月"
        onPreviousMonth={mockOnPrevious}
        onNextMonth={mockOnNext}
      />
    )

    expect(screen.getByText('考勤记录')).toBeInTheDocument()
    expect(screen.getByText('2023年11月')).toBeInTheDocument()
  })

  test('应该渲染左右箭头按钮', () => {
    const { container } = render(
      <MonthSelector
        currentMonth="2023年11月"
        onPreviousMonth={mockOnPrevious}
        onNextMonth={mockOnNext}
      />
    )

    // 检查左右箭头图标是否存在
    const leftArrow = container.querySelector('.i-heroicons-chevron-left-20-solid')
    const rightArrow = container.querySelector('.i-heroicons-chevron-right-20-solid')

    expect(leftArrow).toBeInTheDocument()
    expect(rightArrow).toBeInTheDocument()
  })

  test('应该调用上个月处理函数', () => {
    const { container } = render(
      <MonthSelector
        currentMonth="2023年11月"
        onPreviousMonth={mockOnPrevious}
        onNextMonth={mockOnNext}
      />
    )

    const leftArrow = container.querySelector('.i-heroicons-chevron-left-20-solid')
    if (leftArrow) {
      leftArrow.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    }

    expect(mockOnPrevious).toHaveBeenCalledTimes(1)
  })

  test('应该调用下个月处理函数', () => {
    const { container } = render(
      <MonthSelector
        currentMonth="2023年11月"
        onPreviousMonth={mockOnPrevious}
        onNextMonth={mockOnNext}
      />
    )

    const rightArrow = container.querySelector('.i-heroicons-chevron-right-20-solid')
    if (rightArrow) {
      rightArrow.dispatchEvent(new MouseEvent('click', { bubbles: true }))
    }

    expect(mockOnNext).toHaveBeenCalledTimes(1)
  })
})
