|
@@ -8,35 +8,8 @@ import '@testing-library/jest-dom'
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
|
import ProfilePage from '../../src/pages/profile/index'
|
|
import ProfilePage from '../../src/pages/profile/index'
|
|
|
|
|
|
|
|
-// Mock Taro相关API
|
|
|
|
|
-const mockShowToast = jest.fn()
|
|
|
|
|
-const mockShowLoading = jest.fn()
|
|
|
|
|
-const mockHideLoading = jest.fn()
|
|
|
|
|
-const mockShowModal = jest.fn()
|
|
|
|
|
-const mockNavigateTo = jest.fn()
|
|
|
|
|
-const mockReLaunch = jest.fn()
|
|
|
|
|
-const mockOpenCustomerServiceChat = jest.fn()
|
|
|
|
|
-
|
|
|
|
|
-jest.mock('@tarojs/taro', () => ({
|
|
|
|
|
- showToast: mockShowToast,
|
|
|
|
|
- showLoading: mockShowLoading,
|
|
|
|
|
- hideLoading: mockHideLoading,
|
|
|
|
|
- showModal: mockShowModal,
|
|
|
|
|
- navigateTo: mockNavigateTo,
|
|
|
|
|
- reLaunch: mockReLaunch,
|
|
|
|
|
- openCustomerServiceChat: mockOpenCustomerServiceChat,
|
|
|
|
|
- getSystemInfoSync: () => ({
|
|
|
|
|
- statusBarHeight: 20
|
|
|
|
|
- }),
|
|
|
|
|
- getMenuButtonBoundingClientRect: () => ({
|
|
|
|
|
- width: 87,
|
|
|
|
|
- height: 32,
|
|
|
|
|
- top: 48,
|
|
|
|
|
- right: 314,
|
|
|
|
|
- bottom: 80,
|
|
|
|
|
- left: 227
|
|
|
|
|
- })
|
|
|
|
|
-}))
|
|
|
|
|
|
|
+// 导入 Taro mock 函数
|
|
|
|
|
+import taroMock from '../../tests/__mocks__/taroMock'
|
|
|
|
|
|
|
|
// Mock TabBarLayout 组件
|
|
// Mock TabBarLayout 组件
|
|
|
jest.mock('@/layouts/tab-bar-layout', () => ({
|
|
jest.mock('@/layouts/tab-bar-layout', () => ({
|
|
@@ -189,6 +162,15 @@ describe('个人中心页面测试', () => {
|
|
|
mutateAsync: options.mutationFn,
|
|
mutateAsync: options.mutationFn,
|
|
|
isPending: false
|
|
isPending: false
|
|
|
}))
|
|
}))
|
|
|
|
|
+
|
|
|
|
|
+ // 重置所有 mock 调用记录
|
|
|
|
|
+ taroMock.showToast.mockClear()
|
|
|
|
|
+ taroMock.openCustomerServiceChat.mockClear()
|
|
|
|
|
+ taroMock.navigateTo.mockClear()
|
|
|
|
|
+ taroMock.showLoading.mockClear()
|
|
|
|
|
+ taroMock.hideLoading.mockClear()
|
|
|
|
|
+ taroMock.showModal.mockClear()
|
|
|
|
|
+ taroMock.reLaunch.mockClear()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('应该正确渲染个人中心页面', () => {
|
|
test('应该正确渲染个人中心页面', () => {
|
|
@@ -225,7 +207,7 @@ describe('个人中心页面测试', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('应该处理联系客服功能 - 成功场景', async () => {
|
|
test('应该处理联系客服功能 - 成功场景', async () => {
|
|
|
- mockOpenCustomerServiceChat.mockImplementation((options) => {
|
|
|
|
|
|
|
+ taroMock.openCustomerServiceChat.mockImplementation((options) => {
|
|
|
options.success()
|
|
options.success()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -236,12 +218,12 @@ describe('个人中心页面测试', () => {
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// 点击联系客服按钮
|
|
// 点击联系客服按钮
|
|
|
- const customerServiceButton = screen.getByText('联系客服')
|
|
|
|
|
|
|
+ const customerServiceButton = screen.getByTestId('customer-service-button')
|
|
|
fireEvent.click(customerServiceButton)
|
|
fireEvent.click(customerServiceButton)
|
|
|
|
|
|
|
|
// 检查微信客服API被正确调用
|
|
// 检查微信客服API被正确调用
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockOpenCustomerServiceChat).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.openCustomerServiceChat).toHaveBeenCalledWith({
|
|
|
extInfo: {
|
|
extInfo: {
|
|
|
url: 'https://work.weixin.qq.com/kfid/kfc5f4d729bc3c893d7'
|
|
url: 'https://work.weixin.qq.com/kfid/kfc5f4d729bc3c893d7'
|
|
|
},
|
|
},
|
|
@@ -253,7 +235,7 @@ describe('个人中心页面测试', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('应该处理联系客服功能 - 失败场景', async () => {
|
|
test('应该处理联系客服功能 - 失败场景', async () => {
|
|
|
- mockOpenCustomerServiceChat.mockImplementation((options) => {
|
|
|
|
|
|
|
+ taroMock.openCustomerServiceChat.mockImplementation((options) => {
|
|
|
options.fail({ errMsg: '客服功能不可用' })
|
|
options.fail({ errMsg: '客服功能不可用' })
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -264,12 +246,12 @@ describe('个人中心页面测试', () => {
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// 点击联系客服按钮
|
|
// 点击联系客服按钮
|
|
|
- const customerServiceButton = screen.getByText('联系客服')
|
|
|
|
|
|
|
+ const customerServiceButton = screen.getByTestId('customer-service-button')
|
|
|
fireEvent.click(customerServiceButton)
|
|
fireEvent.click(customerServiceButton)
|
|
|
|
|
|
|
|
// 检查错误提示显示
|
|
// 检查错误提示显示
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '客服功能暂不可用,请稍后重试',
|
|
title: '客服功能暂不可用,请稍后重试',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
@@ -277,7 +259,7 @@ describe('个人中心页面测试', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('应该处理联系客服功能 - 异常场景', async () => {
|
|
test('应该处理联系客服功能 - 异常场景', async () => {
|
|
|
- mockOpenCustomerServiceChat.mockImplementation(() => {
|
|
|
|
|
|
|
+ taroMock.openCustomerServiceChat.mockImplementation(() => {
|
|
|
throw new Error('API调用异常')
|
|
throw new Error('API调用异常')
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -288,12 +270,12 @@ describe('个人中心页面测试', () => {
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// 点击联系客服按钮
|
|
// 点击联系客服按钮
|
|
|
- const customerServiceButton = screen.getByText('联系客服')
|
|
|
|
|
|
|
+ const customerServiceButton = screen.getByTestId('customer-service-button')
|
|
|
fireEvent.click(customerServiceButton)
|
|
fireEvent.click(customerServiceButton)
|
|
|
|
|
|
|
|
// 检查异常处理
|
|
// 检查异常处理
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '客服功能异常,请稍后重试',
|
|
title: '客服功能异常,请稍后重试',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
@@ -308,40 +290,40 @@ describe('个人中心页面测试', () => {
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// 点击编辑资料按钮
|
|
// 点击编辑资料按钮
|
|
|
- const editProfileButton = screen.getByText('编辑资料')
|
|
|
|
|
|
|
+ const editProfileButton = screen.getByTestId('edit-profile-button')
|
|
|
fireEvent.click(editProfileButton)
|
|
fireEvent.click(editProfileButton)
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '功能开发中...',
|
|
title: '功能开发中...',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// 点击乘车人管理按钮
|
|
// 点击乘车人管理按钮
|
|
|
- const passengersButton = screen.getByText('乘车人管理')
|
|
|
|
|
|
|
+ const passengersButton = screen.getByTestId('passengers-button')
|
|
|
fireEvent.click(passengersButton)
|
|
fireEvent.click(passengersButton)
|
|
|
- expect(mockNavigateTo).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.navigateTo).toHaveBeenCalledWith({
|
|
|
url: '/pages/passengers/passengers'
|
|
url: '/pages/passengers/passengers'
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// 点击设置按钮
|
|
// 点击设置按钮
|
|
|
- const settingsButton = screen.getByText('设置')
|
|
|
|
|
|
|
+ const settingsButton = screen.getByTestId('settings-button')
|
|
|
fireEvent.click(settingsButton)
|
|
fireEvent.click(settingsButton)
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '功能开发中...',
|
|
title: '功能开发中...',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// 点击常见问题按钮
|
|
// 点击常见问题按钮
|
|
|
- const faqButton = screen.getByText('常见问题')
|
|
|
|
|
|
|
+ const faqButton = screen.getByTestId('faq-button')
|
|
|
fireEvent.click(faqButton)
|
|
fireEvent.click(faqButton)
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '常见问题功能开发中...',
|
|
title: '常见问题功能开发中...',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// 点击意见反馈按钮
|
|
// 点击意见反馈按钮
|
|
|
- const feedbackButton = screen.getByText('意见反馈')
|
|
|
|
|
|
|
+ const feedbackButton = screen.getByTestId('feedback-button')
|
|
|
fireEvent.click(feedbackButton)
|
|
fireEvent.click(feedbackButton)
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '意见反馈功能开发中...',
|
|
title: '意见反馈功能开发中...',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
@@ -360,9 +342,9 @@ describe('个人中心页面测试', () => {
|
|
|
|
|
|
|
|
// 检查上传成功处理
|
|
// 检查上传成功处理
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockShowLoading).toHaveBeenCalledWith({ title: '更新头像...' })
|
|
|
|
|
- expect(mockHideLoading).toHaveBeenCalled()
|
|
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showLoading).toHaveBeenCalledWith({ title: '更新头像...' })
|
|
|
|
|
+ expect(taroMock.hideLoading).toHaveBeenCalled()
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '头像更新成功',
|
|
title: '头像更新成功',
|
|
|
icon: 'success'
|
|
icon: 'success'
|
|
|
})
|
|
})
|
|
@@ -386,15 +368,16 @@ describe('个人中心页面测试', () => {
|
|
|
|
|
|
|
|
// 检查上传失败处理
|
|
// 检查上传失败处理
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '上传失败,请重试',
|
|
title: '上传失败,请重试',
|
|
|
icon: 'none'
|
|
icon: 'none'
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- test('应该处理退出登录', async () => {
|
|
|
|
|
- mockShowModal.mockImplementation((options) => {
|
|
|
|
|
|
|
+ // 退出登录功能暂时被注释掉,跳过相关测试
|
|
|
|
|
+ test.skip('应该处理退出登录', async () => {
|
|
|
|
|
+ taroMock.showModal.mockImplementation((options) => {
|
|
|
options.success({ confirm: true })
|
|
options.success({ confirm: true })
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -410,15 +393,15 @@ describe('个人中心页面测试', () => {
|
|
|
|
|
|
|
|
// 检查退出登录流程
|
|
// 检查退出登录流程
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockShowModal).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.showModal).toHaveBeenCalledWith({
|
|
|
title: '退出登录',
|
|
title: '退出登录',
|
|
|
content: '确定要退出登录吗?',
|
|
content: '确定要退出登录吗?',
|
|
|
success: expect.any(Function)
|
|
success: expect.any(Function)
|
|
|
})
|
|
})
|
|
|
- expect(mockShowLoading).toHaveBeenCalledWith({ title: '退出中...' })
|
|
|
|
|
|
|
+ expect(taroMock.showLoading).toHaveBeenCalledWith({ title: '退出中...' })
|
|
|
expect(mockLogout).toHaveBeenCalled()
|
|
expect(mockLogout).toHaveBeenCalled()
|
|
|
- expect(mockHideLoading).toHaveBeenCalled()
|
|
|
|
|
- expect(mockShowToast).toHaveBeenCalledWith({
|
|
|
|
|
|
|
+ expect(taroMock.hideLoading).toHaveBeenCalled()
|
|
|
|
|
+ expect(taroMock.showToast).toHaveBeenCalledWith({
|
|
|
title: '已退出登录',
|
|
title: '已退出登录',
|
|
|
icon: 'success',
|
|
icon: 'success',
|
|
|
duration: 1500
|
|
duration: 1500
|
|
@@ -426,8 +409,8 @@ describe('个人中心页面测试', () => {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- test('应该处理退出登录取消', async () => {
|
|
|
|
|
- mockShowModal.mockImplementation((options) => {
|
|
|
|
|
|
|
+ test.skip('应该处理退出登录取消', async () => {
|
|
|
|
|
+ taroMock.showModal.mockImplementation((options) => {
|
|
|
options.success({ confirm: false })
|
|
options.success({ confirm: false })
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -443,7 +426,7 @@ describe('个人中心页面测试', () => {
|
|
|
|
|
|
|
|
// 检查取消退出登录
|
|
// 检查取消退出登录
|
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
|
- expect(mockShowModal).toHaveBeenCalled()
|
|
|
|
|
|
|
+ expect(taroMock.showModal).toHaveBeenCalled()
|
|
|
expect(mockLogout).not.toHaveBeenCalled()
|
|
expect(mockLogout).not.toHaveBeenCalled()
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|