|
@@ -3,11 +3,6 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
|
import HomePage from '../../src/pages/home/index'
|
|
import HomePage from '../../src/pages/home/index'
|
|
|
|
|
|
|
|
-// Mock Taro 导航
|
|
|
|
|
-jest.mock('@tarojs/taro', () => ({
|
|
|
|
|
- navigateTo: jest.fn()
|
|
|
|
|
-}))
|
|
|
|
|
-
|
|
|
|
|
// Mock AreaPicker 组件
|
|
// Mock AreaPicker 组件
|
|
|
jest.mock('../../src/components/AreaPicker', () => ({
|
|
jest.mock('../../src/components/AreaPicker', () => ({
|
|
|
AreaPicker: jest.fn(({ visible, onClose, onConfirm, value, title }) => {
|
|
AreaPicker: jest.fn(({ visible, onClose, onConfirm, value, title }) => {
|
|
@@ -45,6 +40,9 @@ const createTestQueryClient = () => new QueryClient({
|
|
|
},
|
|
},
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+// 保存原始环境变量
|
|
|
|
|
+const originalEnv = process.env
|
|
|
|
|
+
|
|
|
// 包装组件
|
|
// 包装组件
|
|
|
const Wrapper = ({ children }: { children: React.ReactNode }) => {
|
|
const Wrapper = ({ children }: { children: React.ReactNode }) => {
|
|
|
const queryClient = createTestQueryClient()
|
|
const queryClient = createTestQueryClient()
|
|
@@ -58,6 +56,13 @@ const Wrapper = ({ children }: { children: React.ReactNode }) => {
|
|
|
describe('首页集成测试', () => {
|
|
describe('首页集成测试', () => {
|
|
|
beforeEach(() => {
|
|
beforeEach(() => {
|
|
|
jest.clearAllMocks()
|
|
jest.clearAllMocks()
|
|
|
|
|
+ // 恢复原始环境变量
|
|
|
|
|
+ process.env = { ...originalEnv }
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ afterEach(() => {
|
|
|
|
|
+ // 恢复原始环境变量
|
|
|
|
|
+ process.env = originalEnv
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('应该正确渲染首页', () => {
|
|
test('应该正确渲染首页', () => {
|
|
@@ -366,4 +371,72 @@ describe('首页集成测试', () => {
|
|
|
const busOption = screen.getByText('大巴拼车')
|
|
const busOption = screen.getByText('大巴拼车')
|
|
|
expect(busOption.parentElement).toHaveClass('bg-blue-500')
|
|
expect(busOption.parentElement).toHaveClass('bg-blue-500')
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+ test('应该使用环境变量配置的默认目的地', () => {
|
|
|
|
|
+ // 设置环境变量
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_PROVINCE_ID = '4'
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_CITY_ID = '5'
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_DISTRICT_ID = '6'
|
|
|
|
|
+
|
|
|
|
|
+ render(
|
|
|
|
|
+ <Wrapper>
|
|
|
|
|
+ <HomePage />
|
|
|
|
|
+ </Wrapper>
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // 检查目的地显示默认文本
|
|
|
|
|
+ const destinationText = screen.getByText('默认目的地')
|
|
|
|
|
+ expect(destinationText).toBeInTheDocument()
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ test('应该处理环境变量配置缺失的情况', () => {
|
|
|
|
|
+ // 清除环境变量
|
|
|
|
|
+ delete process.env.TARO_APP_DEFAULT_END_PROVINCE_ID
|
|
|
|
|
+ delete process.env.TARO_APP_DEFAULT_END_CITY_ID
|
|
|
|
|
+ delete process.env.TARO_APP_DEFAULT_END_DISTRICT_ID
|
|
|
|
|
+
|
|
|
|
|
+ render(
|
|
|
|
|
+ <Wrapper>
|
|
|
|
|
+ <HomePage />
|
|
|
|
|
+ </Wrapper>
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // 检查目的地显示请选择地区
|
|
|
|
|
+ const destinationText = screen.getByText('请选择地区')
|
|
|
|
|
+ expect(destinationText).toBeInTheDocument()
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ test('应该处理环境变量配置不完整的情况', () => {
|
|
|
|
|
+ // 只设置部分环境变量
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_PROVINCE_ID = '4'
|
|
|
|
|
+ delete process.env.TARO_APP_DEFAULT_END_CITY_ID
|
|
|
|
|
+ delete process.env.TARO_APP_DEFAULT_END_DISTRICT_ID
|
|
|
|
|
+
|
|
|
|
|
+ render(
|
|
|
|
|
+ <Wrapper>
|
|
|
|
|
+ <HomePage />
|
|
|
|
|
+ </Wrapper>
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // 检查目的地显示请选择地区(因为配置不完整)
|
|
|
|
|
+ const destinationText = screen.getByText('请选择地区')
|
|
|
|
|
+ expect(destinationText).toBeInTheDocument()
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ test('应该处理环境变量格式错误的情况', () => {
|
|
|
|
|
+ // 设置格式错误的环境变量
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_PROVINCE_ID = 'invalid'
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_CITY_ID = '5'
|
|
|
|
|
+ process.env.TARO_APP_DEFAULT_END_DISTRICT_ID = '6'
|
|
|
|
|
+
|
|
|
|
|
+ render(
|
|
|
|
|
+ <Wrapper>
|
|
|
|
|
+ <HomePage />
|
|
|
|
|
+ </Wrapper>
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // 检查目的地显示请选择地区(因为配置格式错误)
|
|
|
|
|
+ const destinationText = screen.getByText('请选择地区')
|
|
|
|
|
+ expect(destinationText).toBeInTheDocument()
|
|
|
|
|
+ })
|
|
|
})
|
|
})
|