MenuSection.test.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React from 'react'
  2. import { render, screen, fireEvent } from '@testing-library/react'
  3. import '@testing-library/jest-dom'
  4. import { MenuSection } from '../../src/components/MenuSection'
  5. describe('MenuSection', () => {
  6. const mockSection = {
  7. title: undefined,
  8. items: [
  9. {
  10. id: 'item1',
  11. title: '修改个人信息',
  12. icon: 'i-heroicons-user-20-solid',
  13. bgColor: 'bg-blue-100',
  14. iconColor: 'text-blue-500',
  15. onPress: jest.fn()
  16. },
  17. {
  18. id: 'item2',
  19. title: '账号与安全',
  20. icon: 'i-heroicons-shield-check-20-solid',
  21. bgColor: 'bg-green-100',
  22. iconColor: 'text-green-500',
  23. onPress: jest.fn()
  24. }
  25. ]
  26. }
  27. beforeEach(() => {
  28. jest.clearAllMocks()
  29. })
  30. it('应该渲染所有菜单项', () => {
  31. render(<MenuSection section={mockSection} />)
  32. expect(screen.getByText('修改个人信息')).toBeInTheDocument()
  33. expect(screen.getByText('账号与安全')).toBeInTheDocument()
  34. })
  35. it('应该渲染分组标题(如果存在)', () => {
  36. const sectionWithTitle = {
  37. ...mockSection,
  38. title: '功能设置'
  39. }
  40. render(<MenuSection section={sectionWithTitle} />)
  41. expect(screen.getByText('功能设置')).toBeInTheDocument()
  42. })
  43. it('点击菜单项应该调用对应的onPress', () => {
  44. render(<MenuSection section={mockSection} />)
  45. const item1 = screen.getByText('修改个人信息').closest('.flex')
  46. if (item1) {
  47. fireEvent.click(item1)
  48. }
  49. expect(mockSection.items[0].onPress).toHaveBeenCalledTimes(1)
  50. })
  51. it('应该渲染所有菜单项的图标', () => {
  52. const { container } = render(<MenuSection section={mockSection} />)
  53. const icons = container.querySelectorAll('.i-heroicons-user-20-solid, .i-heroicons-shield-check-20-solid')
  54. expect(icons.length).toBe(2)
  55. })
  56. it('应该渲染所有菜单项的右箭头', () => {
  57. const { container } = render(<MenuSection section={mockSection} />)
  58. const arrows = container.querySelectorAll('.i-heroicons-chevron-right-20-solid')
  59. expect(arrows.length).toBe(2)
  60. })
  61. })