MonthSelector.test.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * MonthSelector组件测试
  3. */
  4. import React from 'react'
  5. import { render, screen } from '@testing-library/react'
  6. import '@testing-library/jest-dom'
  7. import { MonthSelector } from '@/components/MonthSelector'
  8. describe('MonthSelector组件', () => {
  9. const mockOnPrevious = jest.fn()
  10. const mockOnNext = jest.fn()
  11. beforeEach(() => {
  12. jest.clearAllMocks()
  13. })
  14. test('应该渲染当前月份显示', () => {
  15. render(
  16. <MonthSelector
  17. currentMonth="2023年11月"
  18. onPreviousMonth={mockOnPrevious}
  19. onNextMonth={mockOnNext}
  20. />
  21. )
  22. expect(screen.getByText('考勤记录')).toBeInTheDocument()
  23. expect(screen.getByText('2023年11月')).toBeInTheDocument()
  24. })
  25. test('应该渲染左右箭头按钮', () => {
  26. const { container } = render(
  27. <MonthSelector
  28. currentMonth="2023年11月"
  29. onPreviousMonth={mockOnPrevious}
  30. onNextMonth={mockOnNext}
  31. />
  32. )
  33. // 检查左右箭头图标是否存在
  34. const leftArrow = container.querySelector('.i-heroicons-chevron-left-20-solid')
  35. const rightArrow = container.querySelector('.i-heroicons-chevron-right-20-solid')
  36. expect(leftArrow).toBeInTheDocument()
  37. expect(rightArrow).toBeInTheDocument()
  38. })
  39. test('应该调用上个月处理函数', () => {
  40. const { container } = render(
  41. <MonthSelector
  42. currentMonth="2023年11月"
  43. onPreviousMonth={mockOnPrevious}
  44. onNextMonth={mockOnNext}
  45. />
  46. )
  47. const leftArrow = container.querySelector('.i-heroicons-chevron-left-20-solid')
  48. if (leftArrow) {
  49. leftArrow.dispatchEvent(new MouseEvent('click', { bubbles: true }))
  50. }
  51. expect(mockOnPrevious).toHaveBeenCalledTimes(1)
  52. })
  53. test('应该调用下个月处理函数', () => {
  54. const { container } = render(
  55. <MonthSelector
  56. currentMonth="2023年11月"
  57. onPreviousMonth={mockOnPrevious}
  58. onNextMonth={mockOnNext}
  59. />
  60. )
  61. const rightArrow = container.querySelector('.i-heroicons-chevron-right-20-solid')
  62. if (rightArrow) {
  63. rightArrow.dispatchEvent(new MouseEvent('click', { bubbles: true }))
  64. }
  65. expect(mockOnNext).toHaveBeenCalledTimes(1)
  66. })
  67. })