HomePage.test.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react'
  2. import { render, fireEvent, waitFor } from '@testing-library/react'
  3. import { HomePage } from '../../src/pages/home/HomePage'
  4. // Mock Taro导航
  5. jest.mock('@tarojs/taro', () => ({
  6. navigateTo: jest.fn()
  7. }))
  8. describe('HomePage', () => {
  9. beforeEach(() => {
  10. jest.clearAllMocks()
  11. })
  12. it('应该正确渲染首页', () => {
  13. const { getByText, getByPlaceholderText } = render(<HomePage />)
  14. expect(getByText('便捷出行')).toBeTruthy()
  15. expect(getByText('专业出行服务,安全舒适')).toBeTruthy()
  16. expect(getByText('大巴拼车')).toBeTruthy()
  17. expect(getByText('商务车')).toBeTruthy()
  18. expect(getByText('包车')).toBeTruthy()
  19. expect(getByPlaceholderText('搜索出发地点')).toBeTruthy()
  20. expect(getByPlaceholderText('搜索目的地点')).toBeTruthy()
  21. expect(getByText('查询路线')).toBeTruthy()
  22. })
  23. it('应该处理出行方式选择', () => {
  24. const { getByText } = render(<HomePage />)
  25. const businessButton = getByText('商务车')
  26. fireEvent.click(businessButton)
  27. // 验证商务车按钮有激活样式
  28. expect(businessButton.className).toContain('bg-blue-500')
  29. expect(businessButton.className).toContain('text-white')
  30. })
  31. it('应该处理交换出发地和目的地', () => {
  32. const { getByText } = render(<HomePage />)
  33. const swapButton = getByText('⇄')
  34. fireEvent.click(swapButton)
  35. // 这里可以验证状态是否正确交换
  36. // 由于组件内部状态,我们主要验证点击事件正常触发
  37. expect(swapButton).toBeTruthy()
  38. })
  39. it('应该处理日期选择', () => {
  40. const { getByDisplayValue } = render(<HomePage />)
  41. const today = new Date().toISOString().split('T')[0]
  42. const dateInput = getByDisplayValue(today)
  43. expect(dateInput).toBeTruthy()
  44. // 模拟日期变化
  45. fireEvent.change(dateInput, { target: { value: '2025-10-20' } })
  46. expect(dateInput.getAttribute('value')).toBe('2025-10-20')
  47. })
  48. it('应该验证查询表单', () => {
  49. const { getByText } = render(<HomePage />)
  50. const searchButton = getByText('查询路线')
  51. fireEvent.click(searchButton)
  52. // 由于没有选择完整的地点,应该不会导航
  53. // 这里主要验证点击事件正常触发
  54. expect(searchButton).toBeTruthy()
  55. })
  56. it('应该显示轮播图', () => {
  57. const { getByText } = render(<HomePage />)
  58. expect(getByText('便捷出行')).toBeTruthy()
  59. expect(getByText('专业出行服务,安全舒适')).toBeTruthy()
  60. })
  61. it('应该处理省市区选择', async () => {
  62. const { getByText } = render(<HomePage />)
  63. // 验证省市区组件存在
  64. expect(getByText('出发地区')).toBeTruthy()
  65. expect(getByText('目的地区')).toBeTruthy()
  66. })
  67. it('应该处理地点搜索', async () => {
  68. const { getByPlaceholderText } = render(<HomePage />)
  69. const startLocationInput = getByPlaceholderText('搜索出发地点')
  70. const endLocationInput = getByPlaceholderText('搜索目的地点')
  71. expect(startLocationInput).toBeTruthy()
  72. expect(endLocationInput).toBeTruthy()
  73. // 模拟输入
  74. fireEvent.input(startLocationInput, { target: { value: '北京' } })
  75. fireEvent.input(endLocationInput, { target: { value: '上海' } })
  76. expect(startLocationInput.getAttribute('value')).toBe('北京')
  77. expect(endLocationInput.getAttribute('value')).toBe('上海')
  78. })
  79. it('应该显示MVP限制说明', () => {
  80. const { getByText } = render(<HomePage />)
  81. expect(getByText('更多功能正在开发中...')).toBeTruthy()
  82. })
  83. it('应该正确显示默认日期', () => {
  84. const { getByDisplayValue } = render(<HomePage />)
  85. const today = new Date().toISOString().split('T')[0]
  86. const dateInput = getByDisplayValue(today)
  87. expect(dateInput).toBeTruthy()
  88. })
  89. it('应该处理完整的查询流程', async () => {
  90. // Mock Taro导航
  91. const mockNavigateTo = require('@tarojs/taro').navigateTo
  92. const { getByText, getByPlaceholderText } = render(<HomePage />)
  93. // 这里模拟一个完整的查询流程
  94. // 注意:由于组件内部状态管理,这个测试主要验证流程完整性
  95. const searchButton = getByText('查询路线')
  96. fireEvent.click(searchButton)
  97. // 验证导航没有被调用(因为缺少必要参数)
  98. expect(mockNavigateTo).not.toHaveBeenCalled()
  99. })
  100. })