|
@@ -3,26 +3,22 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
|
import { mockShowModal, mockShowToast, mockGetNetworkType } from '~/__mocks__/taroMock'
|
|
import { mockShowModal, mockShowToast, mockGetNetworkType } from '~/__mocks__/taroMock'
|
|
|
import OrderButtonBar from '@/components/order/OrderButtonBar'
|
|
import OrderButtonBar from '@/components/order/OrderButtonBar'
|
|
|
|
|
|
|
|
-// Mock specific React Query hooks
|
|
|
|
|
-jest.mock('@tanstack/react-query', () => {
|
|
|
|
|
- const originalModule = jest.requireActual('@tanstack/react-query')
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- ...originalModule,
|
|
|
|
|
- useMutation: jest.fn(() => ({
|
|
|
|
|
- mutate: jest.fn(),
|
|
|
|
|
- mutateAsync: jest.fn(),
|
|
|
|
|
- isLoading: false,
|
|
|
|
|
- isError: false,
|
|
|
|
|
- isSuccess: false,
|
|
|
|
|
- error: null,
|
|
|
|
|
- data: null
|
|
|
|
|
- })),
|
|
|
|
|
- useQueryClient: jest.fn(() => ({
|
|
|
|
|
- invalidateQueries: jest.fn()
|
|
|
|
|
- }))
|
|
|
|
|
- }
|
|
|
|
|
-})
|
|
|
|
|
|
|
+// Mock React Query
|
|
|
|
|
+// jest.mock('@tanstack/react-query', () => ({
|
|
|
|
|
+// useMutation: jest.fn(() => ({
|
|
|
|
|
+// mutate: jest.fn(),
|
|
|
|
|
+// mutateAsync: jest.fn(),
|
|
|
|
|
+// isLoading: false,
|
|
|
|
|
+// isPending: false,
|
|
|
|
|
+// isError: false,
|
|
|
|
|
+// isSuccess: false,
|
|
|
|
|
+// error: null,
|
|
|
|
|
+// data: null
|
|
|
|
|
+// })),
|
|
|
|
|
+// useQueryClient: jest.fn(() => ({
|
|
|
|
|
+// invalidateQueries: jest.fn()
|
|
|
|
|
+// }))
|
|
|
|
|
+// }))
|
|
|
|
|
|
|
|
// Mock API client
|
|
// Mock API client
|
|
|
jest.mock('@/api', () => ({
|
|
jest.mock('@/api', () => ({
|
|
@@ -67,7 +63,13 @@ const TestWrapper = ({ children }: { children: React.ReactNode }) => (
|
|
|
describe('OrderButtonBar', () => {
|
|
describe('OrderButtonBar', () => {
|
|
|
beforeEach(() => {
|
|
beforeEach(() => {
|
|
|
jest.clearAllMocks()
|
|
jest.clearAllMocks()
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
|
|
+ // 模拟网络检查成功回调
|
|
|
|
|
+ mockGetNetworkType.mockImplementation((options) => {
|
|
|
|
|
+ if (options?.success) {
|
|
|
|
|
+ options.success({ networkType: 'wifi' })
|
|
|
|
|
+ }
|
|
|
|
|
+ return Promise.resolve()
|
|
|
|
|
+ })
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should render cancel button for unpaid order', () => {
|
|
it('should render cancel button for unpaid order', () => {
|
|
@@ -83,7 +85,6 @@ describe('OrderButtonBar', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should show cancel reason dialog when cancel button is clicked', async () => {
|
|
it('should show cancel reason dialog when cancel button is clicked', async () => {
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
mockShowModal.mockResolvedValue({ confirm: true, content: '测试取消原因' })
|
|
mockShowModal.mockResolvedValue({ confirm: true, content: '测试取消原因' })
|
|
|
|
|
|
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
@@ -107,7 +108,6 @@ describe('OrderButtonBar', () => {
|
|
|
it('should call API when cancel order is confirmed', async () => {
|
|
it('should call API when cancel order is confirmed', async () => {
|
|
|
const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
|
|
const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
|
|
|
|
|
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
mockShowModal
|
|
mockShowModal
|
|
|
.mockResolvedValueOnce({ confirm: true, content: '测试取消原因' }) // 原因输入
|
|
.mockResolvedValueOnce({ confirm: true, content: '测试取消原因' }) // 原因输入
|
|
|
.mockResolvedValueOnce({ confirm: true }) // 确认取消
|
|
.mockResolvedValueOnce({ confirm: true }) // 确认取消
|
|
@@ -133,7 +133,6 @@ describe('OrderButtonBar', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should show error when cancel reason is empty', async () => {
|
|
it('should show error when cancel reason is empty', async () => {
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
mockShowModal.mockResolvedValue({ confirm: true, content: '' })
|
|
mockShowModal.mockResolvedValue({ confirm: true, content: '' })
|
|
|
|
|
|
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
@@ -156,7 +155,6 @@ describe('OrderButtonBar', () => {
|
|
|
it('should handle network error gracefully', async () => {
|
|
it('should handle network error gracefully', async () => {
|
|
|
const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
|
|
const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
|
|
|
|
|
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
mockShowModal
|
|
mockShowModal
|
|
|
.mockResolvedValueOnce({ confirm: true, content: '测试取消原因' })
|
|
.mockResolvedValueOnce({ confirm: true, content: '测试取消原因' })
|
|
|
.mockResolvedValueOnce({ confirm: true })
|
|
.mockResolvedValueOnce({ confirm: true })
|
|
@@ -181,17 +179,17 @@ describe('OrderButtonBar', () => {
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should disable cancel button during mutation', async () => {
|
|
it('should disable cancel button during mutation', async () => {
|
|
|
- const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
|
|
|
|
|
-
|
|
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
- mockShowModal
|
|
|
|
|
- .mockResolvedValueOnce({ confirm: true, content: '测试取消原因' })
|
|
|
|
|
- .mockResolvedValueOnce({ confirm: true })
|
|
|
|
|
-
|
|
|
|
|
- // 模拟API调用延迟
|
|
|
|
|
- mockApiCall.mockImplementation(() => new Promise(resolve => {
|
|
|
|
|
- setTimeout(() => resolve({ status: 200, json: () => Promise.resolve({ success: true }) }), 100)
|
|
|
|
|
- }))
|
|
|
|
|
|
|
+ // 模拟mutation正在进行中
|
|
|
|
|
+ // mockUseMutation.mockReturnValueOnce({
|
|
|
|
|
+ // mutate: jest.fn(),
|
|
|
|
|
+ // mutateAsync: jest.fn(),
|
|
|
|
|
+ // isLoading: true,
|
|
|
|
|
+ // isPending: true,
|
|
|
|
|
+ // isError: false,
|
|
|
|
|
+ // isSuccess: false,
|
|
|
|
|
+ // error: null,
|
|
|
|
|
+ // data: null
|
|
|
|
|
+ // })
|
|
|
|
|
|
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
|
<TestWrapper>
|
|
<TestWrapper>
|
|
@@ -199,15 +197,15 @@ describe('OrderButtonBar', () => {
|
|
|
</TestWrapper>
|
|
</TestWrapper>
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- fireEvent.click(getByText('取消订单'))
|
|
|
|
|
-
|
|
|
|
|
- await waitFor(() => {
|
|
|
|
|
- expect(getByText('取消中...')).toBeTruthy()
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ expect(getByText('取消中...')).toBeTruthy()
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should not show cancel button for shipped order', () => {
|
|
it('should not show cancel button for shipped order', () => {
|
|
|
- const shippedOrder = { ...mockOrder, state: 1 } // 已发货
|
|
|
|
|
|
|
+ const shippedOrder = {
|
|
|
|
|
+ ...mockOrder,
|
|
|
|
|
+ payState: 2, // 已支付
|
|
|
|
|
+ state: 1 // 已发货
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
const { queryByText } = render(
|
|
const { queryByText } = render(
|
|
|
<TestWrapper>
|
|
<TestWrapper>
|
|
@@ -221,7 +219,6 @@ describe('OrderButtonBar', () => {
|
|
|
|
|
|
|
|
it('should use external cancel handler when provided', async () => {
|
|
it('should use external cancel handler when provided', async () => {
|
|
|
const mockOnCancelOrder = jest.fn()
|
|
const mockOnCancelOrder = jest.fn()
|
|
|
- mockGetNetworkType.mockResolvedValue({ networkType: 'wifi' })
|
|
|
|
|
|
|
|
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
|
<TestWrapper>
|
|
<TestWrapper>
|