| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import React from 'react'
- import { render, fireEvent, waitFor } from '@testing-library/react'
- import { HomePage } from '../../src/pages/home/HomePage'
- // Mock Taro导航
- jest.mock('@tarojs/taro', () => ({
- navigateTo: jest.fn()
- }))
- describe('HomePage', () => {
- beforeEach(() => {
- jest.clearAllMocks()
- })
- it('应该正确渲染首页', () => {
- const { getByText, getByPlaceholderText } = render(<HomePage />)
- expect(getByText('便捷出行')).toBeTruthy()
- expect(getByText('专业出行服务,安全舒适')).toBeTruthy()
- expect(getByText('大巴拼车')).toBeTruthy()
- expect(getByText('商务车')).toBeTruthy()
- expect(getByText('包车')).toBeTruthy()
- expect(getByPlaceholderText('搜索出发地点')).toBeTruthy()
- expect(getByPlaceholderText('搜索目的地点')).toBeTruthy()
- expect(getByText('查询路线')).toBeTruthy()
- })
- it('应该处理出行方式选择', () => {
- const { getByText } = render(<HomePage />)
- const businessButton = getByText('商务车')
- fireEvent.click(businessButton)
- // 验证商务车按钮有激活样式
- expect(businessButton.className).toContain('bg-blue-500')
- expect(businessButton.className).toContain('text-white')
- })
- it('应该处理交换出发地和目的地', () => {
- const { getByText } = render(<HomePage />)
- const swapButton = getByText('⇄')
- fireEvent.click(swapButton)
- // 这里可以验证状态是否正确交换
- // 由于组件内部状态,我们主要验证点击事件正常触发
- expect(swapButton).toBeTruthy()
- })
- it('应该处理日期选择', () => {
- const { getByDisplayValue } = render(<HomePage />)
- const today = new Date().toISOString().split('T')[0]
- const dateInput = getByDisplayValue(today)
- expect(dateInput).toBeTruthy()
- // 模拟日期变化
- fireEvent.change(dateInput, { target: { value: '2025-10-20' } })
- expect(dateInput.getAttribute('value')).toBe('2025-10-20')
- })
- it('应该验证查询表单', () => {
- const { getByText } = render(<HomePage />)
- const searchButton = getByText('查询路线')
- fireEvent.click(searchButton)
- // 由于没有选择完整的地点,应该不会导航
- // 这里主要验证点击事件正常触发
- expect(searchButton).toBeTruthy()
- })
- it('应该显示轮播图', () => {
- const { getByText } = render(<HomePage />)
- expect(getByText('便捷出行')).toBeTruthy()
- expect(getByText('专业出行服务,安全舒适')).toBeTruthy()
- })
- it('应该处理省市区选择', async () => {
- const { getByText } = render(<HomePage />)
- // 验证省市区组件存在
- expect(getByText('出发地区')).toBeTruthy()
- expect(getByText('目的地区')).toBeTruthy()
- })
- it('应该处理地点搜索', async () => {
- const { getByPlaceholderText } = render(<HomePage />)
- const startLocationInput = getByPlaceholderText('搜索出发地点')
- const endLocationInput = getByPlaceholderText('搜索目的地点')
- expect(startLocationInput).toBeTruthy()
- expect(endLocationInput).toBeTruthy()
- // 模拟输入
- fireEvent.input(startLocationInput, { target: { value: '北京' } })
- fireEvent.input(endLocationInput, { target: { value: '上海' } })
- expect(startLocationInput.getAttribute('value')).toBe('北京')
- expect(endLocationInput.getAttribute('value')).toBe('上海')
- })
- it('应该显示MVP限制说明', () => {
- const { getByText } = render(<HomePage />)
- expect(getByText('更多功能正在开发中...')).toBeTruthy()
- })
- it('应该正确显示默认日期', () => {
- const { getByDisplayValue } = render(<HomePage />)
- const today = new Date().toISOString().split('T')[0]
- const dateInput = getByDisplayValue(today)
- expect(dateInput).toBeTruthy()
- })
- it('应该处理完整的查询流程', async () => {
- // Mock Taro导航
- const mockNavigateTo = require('@tarojs/taro').navigateTo
- const { getByText, getByPlaceholderText } = render(<HomePage />)
- // 这里模拟一个完整的查询流程
- // 注意:由于组件内部状态管理,这个测试主要验证流程完整性
- const searchButton = getByText('查询路线')
- fireEvent.click(searchButton)
- // 验证导航没有被调用(因为缺少必要参数)
- expect(mockNavigateTo).not.toHaveBeenCalled()
- })
- })
|