2
0

OrderButtonBar.test.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { render, fireEvent, waitFor } from '@testing-library/react'
  2. import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
  3. import { mockShowModal, mockShowToast, mockGetNetworkType, mockGetEnv } 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. mockGetEnv.mockReturnValue('WEB')
  53. })
  54. it('should render cancel button for unpaid order', () => {
  55. const { getByText } = render(
  56. <TestWrapper>
  57. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  58. </TestWrapper>
  59. )
  60. expect(getByText('取消订单')).toBeTruthy()
  61. expect(getByText('去支付')).toBeTruthy()
  62. expect(getByText('查看详情')).toBeTruthy()
  63. })
  64. it('should show cancel reason dialog when cancel button is clicked', async () => {
  65. const { getByText, getByTestId } = render(
  66. <TestWrapper>
  67. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  68. </TestWrapper>
  69. )
  70. fireEvent.click(getByText('取消订单'))
  71. await waitFor(() => {
  72. expect(getByTestId('cancel-reason-dialog')).toBeTruthy()
  73. // 检查对话框内容
  74. expect(getByText('请选择或填写取消原因,这将帮助我们改进服务')).toBeTruthy()
  75. })
  76. })
  77. it('should call API when cancel order is confirmed', async () => {
  78. const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
  79. mockShowModal.mockResolvedValueOnce({ confirm: true }) // 确认取消
  80. mockApiCall.mockResolvedValue({ status: 200, json: () => Promise.resolve({ success: true, message: '取消成功' }) })
  81. const { getByText, getByPlaceholderText, getByTestId } = render(
  82. <TestWrapper>
  83. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  84. </TestWrapper>
  85. )
  86. // 打开取消对话框
  87. fireEvent.click(getByText('取消订单'))
  88. await waitFor(() => {
  89. expect(getByTestId('cancel-reason-dialog')).toBeTruthy()
  90. })
  91. // 输入取消原因
  92. const reasonInput = getByPlaceholderText('请输入其他取消原因...')
  93. fireEvent.change(reasonInput, { target: { value: '测试取消原因' } })
  94. // 点击确认取消按钮
  95. fireEvent.click(getByText('确认取消'))
  96. await waitFor(() => {
  97. expect(mockShowModal).toHaveBeenCalledWith({
  98. title: '确认取消',
  99. content: '确定要取消订单吗?\n取消原因:测试取消原因',
  100. success: expect.any(Function)
  101. })
  102. })
  103. // 模拟确认对话框确认
  104. const modalCall = mockShowModal.mock.calls[0][0]
  105. if (modalCall.success) {
  106. modalCall.success({ confirm: true })
  107. }
  108. await waitFor(() => {
  109. expect(mockApiCall).toHaveBeenCalledWith({
  110. json: {
  111. orderId: 1,
  112. reason: '测试取消原因'
  113. }
  114. })
  115. })
  116. })
  117. it('should show error when cancel reason is empty', async () => {
  118. const { getByText, getByTestId } = render(
  119. <TestWrapper>
  120. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  121. </TestWrapper>
  122. )
  123. // 打开取消对话框
  124. fireEvent.click(getByText('取消订单'))
  125. await waitFor(() => {
  126. expect(getByTestId('cancel-reason-dialog')).toBeTruthy()
  127. })
  128. // 直接点击确认取消按钮(不输入原因)
  129. fireEvent.click(getByText('确认取消'))
  130. await waitFor(() => {
  131. expect(getByTestId('error-message')).toBeTruthy()
  132. expect(getByText('请输入取消原因')).toBeTruthy()
  133. })
  134. })
  135. it('should handle network error gracefully', async () => {
  136. const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
  137. mockShowModal.mockResolvedValueOnce({ confirm: true })
  138. mockApiCall.mockRejectedValue(new Error('网络连接失败'))
  139. const { getByText, getByPlaceholderText, getByTestId } = render(
  140. <TestWrapper>
  141. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  142. </TestWrapper>
  143. )
  144. // 打开取消对话框
  145. fireEvent.click(getByText('取消订单'))
  146. await waitFor(() => {
  147. expect(getByTestId('cancel-reason-dialog')).toBeTruthy()
  148. })
  149. // 输入取消原因
  150. const reasonInput = getByPlaceholderText('请输入其他取消原因...')
  151. fireEvent.change(reasonInput, { target: { value: '测试取消原因' } })
  152. // 点击确认取消按钮
  153. fireEvent.click(getByText('确认取消'))
  154. // 模拟确认对话框确认
  155. await waitFor(() => {
  156. expect(mockShowModal).toHaveBeenCalledWith({
  157. title: '确认取消',
  158. content: '确定要取消订单吗?\n取消原因:测试取消原因',
  159. success: expect.any(Function)
  160. })
  161. })
  162. const modalCall = mockShowModal.mock.calls[0][0]
  163. if (modalCall.success) {
  164. modalCall.success({ confirm: true })
  165. }
  166. await waitFor(() => {
  167. expect(mockShowToast).toHaveBeenCalledWith({
  168. title: '网络连接失败,请检查网络后重试',
  169. icon: 'error',
  170. duration: 3000
  171. })
  172. })
  173. })
  174. it('should disable cancel button during mutation', async () => {
  175. // 模拟mutation正在进行中
  176. const mockApiCall = require('@/api').orderClient.cancelOrder.$post as jest.Mock
  177. mockApiCall.mockImplementation(() => new Promise(() => {})) // 永不resolve的promise
  178. const { getByText, getByPlaceholderText, getByTestId } = render(
  179. <TestWrapper>
  180. <OrderButtonBar order={mockOrder} onViewDetail={jest.fn()} />
  181. </TestWrapper>
  182. )
  183. // 打开取消对话框
  184. fireEvent.click(getByText('取消订单'))
  185. await waitFor(() => {
  186. expect(getByTestId('cancel-reason-dialog')).toBeTruthy()
  187. })
  188. // 输入取消原因
  189. const reasonInput = getByPlaceholderText('请输入其他取消原因...')
  190. fireEvent.change(reasonInput, { target: { value: '测试取消原因' } })
  191. // 点击确认取消按钮
  192. fireEvent.click(getByText('确认取消'))
  193. // 模拟确认对话框确认
  194. await waitFor(() => {
  195. expect(mockShowModal).toHaveBeenCalledWith({
  196. title: '确认取消',
  197. content: '确定要取消订单吗?\n取消原因:测试取消原因',
  198. success: expect.any(Function)
  199. })
  200. })
  201. const modalCall = mockShowModal.mock.calls[0][0]
  202. if (modalCall.success) {
  203. modalCall.success({ confirm: true })
  204. }
  205. // 检查按钮状态
  206. await waitFor(() => {
  207. expect(getByText('取消中...')).toBeTruthy()
  208. })
  209. })
  210. it('should not show cancel button for shipped order', () => {
  211. const shippedOrder = {
  212. ...mockOrder,
  213. payState: 2, // 已支付
  214. state: 1 // 已发货
  215. }
  216. const { queryByText } = render(
  217. <TestWrapper>
  218. <OrderButtonBar order={shippedOrder} onViewDetail={jest.fn()} />
  219. </TestWrapper>
  220. )
  221. expect(queryByText('取消订单')).toBeNull()
  222. expect(queryByText('确认收货')).toBeTruthy()
  223. })
  224. it('should use external cancel handler when provided', async () => {
  225. const mockOnCancelOrder = jest.fn()
  226. const { getByText } = render(
  227. <TestWrapper>
  228. <OrderButtonBar
  229. order={mockOrder}
  230. onViewDetail={jest.fn()}
  231. onCancelOrder={mockOnCancelOrder}
  232. />
  233. </TestWrapper>
  234. )
  235. fireEvent.click(getByText('取消订单'))
  236. await waitFor(() => {
  237. expect(mockOnCancelOrder).toHaveBeenCalled()
  238. })
  239. })
  240. })