UserInfoHeader.test.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * UserInfoHeader 组件测试
  3. */
  4. import React from 'react'
  5. import { render, screen } from '@testing-library/react'
  6. import '@testing-library/jest-dom'
  7. import UserInfoHeader from '../../../src/components/UserInfoHeader'
  8. import { PersonalInfoResponse } from '../../../src/components/PersonalBasicInfo'
  9. describe('UserInfoHeader', () => {
  10. const mockPersonalInfo: PersonalInfoResponse = {
  11. name: '张三',
  12. gender: '男',
  13. idCard: '110101199001011234',
  14. disabilityId: 'D12345678901',
  15. disabilityType: '肢体残疾',
  16. disabilityLevel: '三级',
  17. phone: '13800138000',
  18. province: '北京市',
  19. city: '北京市',
  20. district: '朝阳区',
  21. detailedAddress: '某某街道123号',
  22. birthDate: '1990-01-01',
  23. idAddress: '北京市朝阳区某某街道123号',
  24. idValidDate: '2030-01-01',
  25. disabilityValidDate: '2025-12-31',
  26. canDirectContact: 1,
  27. isMarried: 0,
  28. nation: '汉族',
  29. jobStatus: 1,
  30. specificDisability: '左腿小腿截肢'
  31. }
  32. it('应该正确渲染用户信息区域', () => {
  33. const { container } = render(
  34. <UserInfoHeader personalInfo={mockPersonalInfo} loading={false} />
  35. )
  36. // 检查用户名
  37. expect(screen.getByText('张三')).toBeInTheDocument()
  38. // 检查用户状态
  39. expect(screen.getByText('肢体残疾 · 三级 · 在职')).toBeInTheDocument()
  40. // 检查渐变背景
  41. const gradientElement = container.querySelector('.bg-gradient-to-b')
  42. expect(gradientElement).toBeInTheDocument()
  43. // 检查头像 (姓名首字)
  44. expect(screen.getByText('张')).toBeInTheDocument()
  45. })
  46. it('应该显示loading状态', () => {
  47. const { container } = render(
  48. <UserInfoHeader personalInfo={null} loading={true} />
  49. )
  50. // 检查loading动画类
  51. const pulseElement = container.querySelector('.animate-pulse')
  52. expect(pulseElement).toBeInTheDocument()
  53. })
  54. it('应该在personalInfo为null时显示默认值', () => {
  55. const { container } = render(
  56. <UserInfoHeader personalInfo={null} loading={false} />
  57. )
  58. // 应该显示默认值而不是null
  59. expect(screen.getByText('未知用户')).toBeInTheDocument()
  60. expect(screen.getByText('状态未知')).toBeInTheDocument()
  61. expect(screen.getByText('?')).toBeInTheDocument()
  62. // 检查渐变背景
  63. const gradientElement = container.querySelector('.bg-gradient-to-b')
  64. expect(gradientElement).toBeInTheDocument()
  65. })
  66. it('应该正确提取姓名首字作为头像', () => {
  67. render(<UserInfoHeader personalInfo={mockPersonalInfo} loading={false} />)
  68. // 姓名首字 "张"
  69. expect(screen.getByText('张')).toBeInTheDocument()
  70. })
  71. it('应该正确处理待业状态', () => {
  72. const unemployedInfo = { ...mockPersonalInfo, jobStatus: 0 }
  73. render(<UserInfoHeader personalInfo={unemployedInfo} loading={false} />)
  74. expect(screen.getByText('肢体残疾 · 三级 · 待业')).toBeInTheDocument()
  75. })
  76. it('应该在缺少某些字段时正确显示', () => {
  77. const incompleteInfo = {
  78. ...mockPersonalInfo,
  79. disabilityType: '',
  80. disabilityLevel: ''
  81. }
  82. render(<UserInfoHeader personalInfo={incompleteInfo} loading={false} />)
  83. // 只显示就业状态
  84. expect(screen.getByText('在职')).toBeInTheDocument()
  85. })
  86. it('应该显示相机图标按钮', () => {
  87. const { container } = render(
  88. <UserInfoHeader personalInfo={mockPersonalInfo} loading={false} />
  89. )
  90. // 检查相机图标
  91. const cameraIcon = container.querySelector('.i-heroicons-camera-20-solid')
  92. expect(cameraIcon).toBeInTheDocument()
  93. })
  94. })