OrderButtonBar.test.tsx 6.3 KB

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