DocumentPhotoItem.test.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * DocumentPhotoItem 组件测试
  3. */
  4. import React from 'react'
  5. import { render, screen } from '@testing-library/react'
  6. import '@testing-library/jest-dom'
  7. import DocumentPhotoItem, { PhotoInfo } from '../../../src/components/DocumentPhotoItem'
  8. import Taro from '@tarojs/taro'
  9. describe('DocumentPhotoItem', () => {
  10. const mockPhoto: PhotoInfo = {
  11. id: 1,
  12. photoType: '身份证',
  13. fileUrl: 'https://example.com/id-card.jpg',
  14. fileName: '身份证.jpg',
  15. uploadTime: '2024-01-01T10:30:00Z',
  16. canDownload: 1
  17. }
  18. beforeEach(() => {
  19. ;(Taro.previewImage as jest.Mock).mockClear()
  20. })
  21. it('应该正确渲染照片信息', () => {
  22. const { container } = render(<DocumentPhotoItem photo={mockPhoto} />)
  23. expect(screen.getByText('身份证')).toBeInTheDocument()
  24. // 检查边框样式
  25. const borderedElement = container.querySelector('.border-gray-200')
  26. expect(borderedElement).toBeInTheDocument()
  27. })
  28. it('应该在fileUrl为空时显示占位符', () => {
  29. const photoWithoutUrl = { ...mockPhoto, fileUrl: null }
  30. const { container } = render(<DocumentPhotoItem photo={photoWithoutUrl} />)
  31. // 应该显示占位图标 (使用 document 图标)
  32. const placeholderIcon = container.querySelector('.i-heroicons-document-20-solid')
  33. expect(placeholderIcon).toBeInTheDocument()
  34. })
  35. it('应该在点击照片时调用预览', () => {
  36. const { fireEvent } = require('@testing-library/react')
  37. render(<DocumentPhotoItem photo={mockPhoto} />)
  38. const photoContainer = screen.getByText('身份证').closest('.cursor-pointer')
  39. if (photoContainer) {
  40. fireEvent.click(photoContainer)
  41. expect(Taro.previewImage).toHaveBeenCalledWith({
  42. current: 'https://example.com/id-card.jpg',
  43. urls: ['https://example.com/id-card.jpg']
  44. })
  45. }
  46. })
  47. it('应该在fileUrl为空时不调用预览', () => {
  48. const { fireEvent } = require('@testing-library/react')
  49. const photoWithoutUrl = { ...mockPhoto, fileUrl: null }
  50. render(<DocumentPhotoItem photo={photoWithoutUrl} />)
  51. const photoContainer = screen.getByText('身份证').closest('.cursor-pointer')
  52. if (photoContainer) {
  53. fireEvent.click(photoContainer)
  54. expect(Taro.previewImage).not.toHaveBeenCalled()
  55. }
  56. })
  57. })