OrderButtonBar.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { render, fireEvent, waitFor } from '@testing-library/react'
  2. import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
  3. import { mockShowModal, mockShowToast, mockGetNetworkType } from '~/__mocks__/taroMock'
  4. import OrderButtonBar from '@/components/order/OrderButtonBar'
  5. // Mock API client
  6. jest.mock('@/api', () => ({
  7. orderClient: {
  8. cancelOrder: {
  9. $post: jest.fn()
  10. }
  11. }
  12. }))
  13. const mockOrder = {
  14. id: 1,
  15. orderNo: 'ORDER001',
  16. payState: 0, // 未支付
  17. state: 0, // 未发货
  18. amount: 100,
  19. payAmount: 100,
  20. freightAmount: 0,
  21. discountAmount: 0,
  22. goodsDetail: JSON.stringify([
  23. { id: 1, name: '商品1', price: 50, num: 2, image: '', spec: '默认规格' }
  24. ]),
  25. recevierName: '张三',
  26. receiverMobile: '13800138000',
  27. address: '北京市朝阳区',
  28. createdAt: '2025-01-01T00:00:00Z'
  29. }
  30. const createTestQueryClient = () => new QueryClient({
  31. defaultOptions: {
  32. queries: { retry: false },
  33. mutations: { retry: false }
  34. }
  35. })
  36. const TestWrapper = ({ children }: { children: React.ReactNode }) => (
  37. <QueryClientProvider client={createTestQueryClient()}>
  38. {children}
  39. </QueryClientProvider>
  40. )
  41. describe('OrderButtonBar', () => {
  42. beforeEach(() => {
  43. jest.clearAllMocks()
  44. // 模拟网络检查成功回调
  45. mockGetNetworkType.mockImplementation((options) => {
  46. if (options?.success) {
  47. options.success({ networkType: 'wifi' })
  48. }
  49. return Promise.resolve()
  50. })
  51. })
  52. it('should render cancel button for unpaid order', () => {
  53. const { getByText } = render(
  54. <TestWrapper>
  55. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  56. </TestWrapper>
  57. )
  58. expect(getByText('取消订单')).toBeTruthy()
  59. expect(getByText('去支付')).toBeTruthy()
  60. expect(getByText('查看详情')).toBeTruthy()
  61. })
  62. it('should show cancel reason dialog when cancel button is clicked', async () => {
  63. mockShowModal.mockResolvedValue({ confirm: true, content: '测试取消原因' })
  64. const { getByText } = render(
  65. <TestWrapper>
  66. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  67. </TestWrapper>
  68. )
  69. fireEvent.click(getByText('取消订单'))
  70. await waitFor(() => {
  71. expect(mockShowModal).toHaveBeenCalledWith({
  72. title: '取消订单',
  73. content: '请填写取消原因:',
  74. editable: true,
  75. placeholderText: '请输入取消原因(必填)'
  76. })
  77. })
  78. })
  79. it('should call API when cancel order is confirmed', async () => {
  80. const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
  81. mockShowModal
  82. .mockResolvedValueOnce({ confirm: true, content: '测试取消原因' }) // 原因输入
  83. .mockResolvedValueOnce({ confirm: true }) // 确认取消
  84. mockApiCall.mockResolvedValue({ status: 200, json: () => Promise.resolve({ success: true, message: '取消成功' }) })
  85. const { getByText } = render(
  86. <TestWrapper>
  87. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  88. </TestWrapper>
  89. )
  90. fireEvent.click(getByText('取消订单'))
  91. await waitFor(() => {
  92. expect(mockApiCall).toHaveBeenCalledWith({
  93. json: {
  94. orderId: 1,
  95. reason: '测试取消原因'
  96. }
  97. })
  98. })
  99. })
  100. it('should show error when cancel reason is empty', async () => {
  101. mockShowModal.mockResolvedValue({ confirm: true, content: '' })
  102. const { getByText } = render(
  103. <TestWrapper>
  104. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  105. </TestWrapper>
  106. )
  107. fireEvent.click(getByText('取消订单'))
  108. await waitFor(() => {
  109. expect(mockShowToast).toHaveBeenCalledWith({
  110. title: '请填写取消原因',
  111. icon: 'error',
  112. duration: 2000
  113. })
  114. })
  115. })
  116. it('should handle network error gracefully', async () => {
  117. const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
  118. mockShowModal
  119. .mockResolvedValueOnce({ confirm: true, content: '测试取消原因' })
  120. .mockResolvedValueOnce({ confirm: true })
  121. mockApiCall.mockRejectedValue(new Error('网络连接失败'))
  122. const { getByText } = render(
  123. <TestWrapper>
  124. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  125. </TestWrapper>
  126. )
  127. fireEvent.click(getByText('取消订单'))
  128. await waitFor(() => {
  129. expect(mockShowToast).toHaveBeenCalledWith({
  130. title: '网络连接失败,请检查网络后重试',
  131. icon: 'error',
  132. duration: 3000
  133. })
  134. })
  135. })
  136. it('should disable cancel button during mutation', async () => {
  137. // 模拟mutation正在进行中
  138. // mockUseMutation.mockReturnValueOnce({
  139. // mutate: jest.fn(),
  140. // mutateAsync: jest.fn(),
  141. // isLoading: true,
  142. // isPending: true,
  143. // isError: false,
  144. // isSuccess: false,
  145. // error: null,
  146. // data: null
  147. // })
  148. const { getByText } = render(
  149. <TestWrapper>
  150. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  151. </TestWrapper>
  152. )
  153. expect(getByText('取消中...')).toBeTruthy()
  154. })
  155. it('should not show cancel button for shipped order', () => {
  156. const shippedOrder = {
  157. ...mockOrder,
  158. payState: 2, // 已支付
  159. state: 1 // 已发货
  160. }
  161. const { queryByText } = render(
  162. <TestWrapper>
  163. <OrderButtonBar order={shippedOrder} onViewDetail={jest.fn()} />
  164. </TestWrapper>
  165. )
  166. expect(queryByText('取消订单')).toBeNull()
  167. expect(queryByText('确认收货')).toBeTruthy()
  168. })
  169. it('should use external cancel handler when provided', async () => {
  170. const mockOnCancelOrder = jest.fn()
  171. const { getByText } = render(
  172. <TestWrapper>
  173. <OrderButtonBar
  174. order={mockOrder}
  175. onViewDetail={jest.fn()}
  176. onCancelOrder={mockOnCancelOrder}
  177. />
  178. </TestWrapper>
  179. )
  180. fireEvent.click(getByText('取消订单'))
  181. await waitFor(() => {
  182. expect(mockOnCancelOrder).toHaveBeenCalled()
  183. })
  184. })
  185. })