AttendanceCalendar.test.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * AttendanceCalendar组件测试
  3. */
  4. import React from 'react'
  5. import { render, screen } from '@testing-library/react'
  6. import '@testing-library/jest-dom'
  7. import { AttendanceCalendar } from '@/components/AttendanceCalendar'
  8. import { AttendanceRecord, AttendanceStatus } from '@/types/attendance'
  9. describe('AttendanceCalendar组件', () => {
  10. const mockRecords: AttendanceRecord[] = [
  11. {
  12. date: '2023-11-01',
  13. weekday: '星期三',
  14. checkInTime: '08:30',
  15. checkOutTime: '17:30',
  16. status: AttendanceStatus.NORMAL
  17. },
  18. {
  19. date: '2023-11-02',
  20. weekday: '星期四',
  21. checkInTime: '08:30',
  22. checkOutTime: '17:30',
  23. status: AttendanceStatus.NORMAL
  24. },
  25. {
  26. date: '2023-11-15',
  27. weekday: '星期三',
  28. checkInTime: '09:15',
  29. checkOutTime: '17:30',
  30. status: AttendanceStatus.LATE
  31. }
  32. ]
  33. test('应该渲染考勤日历标题', () => {
  34. render(
  35. <AttendanceCalendar
  36. year={2023}
  37. month={11}
  38. attendanceRecords={mockRecords}
  39. />
  40. )
  41. expect(screen.getByText('考勤日历')).toBeInTheDocument()
  42. })
  43. test('应该渲染星期标题', () => {
  44. render(
  45. <AttendanceCalendar
  46. year={2023}
  47. month={11}
  48. attendanceRecords={mockRecords}
  49. />
  50. )
  51. expect(screen.getByText('日')).toBeInTheDocument()
  52. expect(screen.getByText('一')).toBeInTheDocument()
  53. expect(screen.getByText('二')).toBeInTheDocument()
  54. expect(screen.getByText('三')).toBeInTheDocument()
  55. expect(screen.getByText('四')).toBeInTheDocument()
  56. expect(screen.getByText('五')).toBeInTheDocument()
  57. expect(screen.getByText('六')).toBeInTheDocument()
  58. })
  59. test('应该渲染日期网格', () => {
  60. const { container } = render(
  61. <AttendanceCalendar
  62. year={2023}
  63. month={11}
  64. attendanceRecords={mockRecords}
  65. />
  66. )
  67. // 检查是否有日期数字
  68. expect(screen.getByText('1')).toBeInTheDocument()
  69. expect(screen.getByText('15')).toBeInTheDocument()
  70. expect(screen.getByText('30')).toBeInTheDocument()
  71. })
  72. test('应该正确标记已打卡日期', () => {
  73. render(
  74. <AttendanceCalendar
  75. year={2023}
  76. month={11}
  77. attendanceRecords={mockRecords}
  78. />
  79. )
  80. // 11月1日有打卡记录,应该有绿色背景
  81. expect(screen.getByText('1')).toBeInTheDocument()
  82. })
  83. test('应该正确处理跨月情况', () => {
  84. render(
  85. <AttendanceCalendar
  86. year={2023}
  87. month={12}
  88. attendanceRecords={[]}
  89. />
  90. )
  91. // 12月应该有31天
  92. expect(screen.getByText('31')).toBeInTheDocument()
  93. })
  94. test('应该正确处理闰年2月', () => {
  95. render(
  96. <AttendanceCalendar
  97. year={2024}
  98. month={2}
  99. attendanceRecords={[]}
  100. />
  101. )
  102. // 2024年是闰年,2月应该有29天
  103. expect(screen.getByText('29')).toBeInTheDocument()
  104. })
  105. test('应该正确处理非闰年2月', () => {
  106. render(
  107. <AttendanceCalendar
  108. year={2023}
  109. month={2}
  110. attendanceRecords={[]}
  111. />
  112. )
  113. // 2023年不是闰年,2月应该有28天
  114. expect(screen.getByText('28')).toBeInTheDocument()
  115. expect(screen.queryByText('29')).not.toBeInTheDocument()
  116. })
  117. })