SalaryRecords.test.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * SalaryRecords组件测试
  3. */
  4. import React from 'react'
  5. import { render, screen } from '@testing-library/react'
  6. import '@testing-library/jest-dom'
  7. import Taro from '@tarojs/taro'
  8. import { SalaryRecords } from '@/components/SalaryRecords'
  9. describe('SalaryRecords组件', () => {
  10. const mockRecords = [
  11. {
  12. orderId: 1,
  13. orderName: '数据标注员',
  14. companyName: '测试科技有限公司',
  15. salaryAmount: 4800,
  16. joinDate: '2023-08-15',
  17. month: '2023-11'
  18. },
  19. {
  20. orderId: 1,
  21. orderName: '数据标注员',
  22. companyName: '测试科技有限公司',
  23. salaryAmount: 4800,
  24. joinDate: '2023-08-15',
  25. month: '2023-10'
  26. },
  27. {
  28. orderId: 1,
  29. orderName: '数据标注员',
  30. companyName: '测试科技有限公司',
  31. salaryAmount: 4800,
  32. joinDate: '2023-08-15',
  33. month: '2023-09'
  34. }
  35. ]
  36. beforeEach(() => {
  37. ;(Taro.showToast as jest.Mock).mockClear()
  38. })
  39. it('应该渲染薪资记录列表', () => {
  40. render(<SalaryRecords records={mockRecords} loading={false} />)
  41. expect(screen.getByText('薪资记录')).toBeInTheDocument()
  42. expect(screen.getByText('2023年11月')).toBeInTheDocument()
  43. expect(screen.getByText('2023年10月')).toBeInTheDocument()
  44. })
  45. it('应该显示查看全部按钮', () => {
  46. render(<SalaryRecords records={mockRecords} loading={false} showViewAll={true} />)
  47. expect(screen.getByText('查看全部')).toBeInTheDocument()
  48. })
  49. it('应该只显示最近3条记录', () => {
  50. const manyRecords = [
  51. ...mockRecords,
  52. {
  53. orderId: 1,
  54. orderName: '数据标注员',
  55. companyName: '测试科技有限公司',
  56. salaryAmount: 4800,
  57. joinDate: '2023-08-15',
  58. month: '2023-08'
  59. },
  60. {
  61. orderId: 1,
  62. orderName: '数据标注员',
  63. companyName: '测试科技有限公司',
  64. salaryAmount: 4800,
  65. joinDate: '2023-08-15',
  66. month: '2023-07'
  67. }
  68. ]
  69. const { container } = render(
  70. <SalaryRecords records={manyRecords} loading={false} showViewAll={false} />
  71. )
  72. // 应该只显示3条记录
  73. const recordElements = container.querySelectorAll('.border-gray-100')
  74. expect(recordElements.length).toBe(3)
  75. })
  76. it('应该显示加载状态', () => {
  77. render(<SalaryRecords records={[]} loading={true} />)
  78. expect(screen.getByText('加载中...')).toBeInTheDocument()
  79. })
  80. it('应该显示无数据状态', () => {
  81. render(<SalaryRecords records={[]} loading={false} />)
  82. expect(screen.getByText('暂无薪资记录')).toBeInTheDocument()
  83. })
  84. it('应该处理查看全部点击', () => {
  85. const onViewAll = jest.fn()
  86. render(
  87. <SalaryRecords
  88. records={mockRecords}
  89. loading={false}
  90. onViewAll={onViewAll}
  91. showViewAll={true}
  92. />
  93. )
  94. const viewAllButton = screen.getByText('查看全部')
  95. viewAllButton.click()
  96. expect(onViewAll).toHaveBeenCalledTimes(1)
  97. })
  98. })