CancelReasonDialog.test.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { render, fireEvent } from '@testing-library/react'
  2. import { mockShowToast } from '~/__mocks__/taroMock'
  3. import CancelReasonDialog from '@/components/common/CancelReasonDialog'
  4. describe('CancelReasonDialog', () => {
  5. const defaultProps = {
  6. open: true,
  7. onOpenChange: jest.fn(),
  8. onConfirm: jest.fn(),
  9. loading: false
  10. }
  11. beforeEach(() => {
  12. jest.clearAllMocks()
  13. })
  14. it('应该渲染对话框当可见时为true', () => {
  15. const { getByText, getAllByText } = render(<CancelReasonDialog {...defaultProps} />)
  16. expect(getByText('取消订单')).toBeTruthy()
  17. expect(getByText('请选择或填写取消原因,这将帮助我们改进服务')).toBeTruthy()
  18. expect(getByText('我不想买了')).toBeTruthy()
  19. expect(getByText('信息填写错误,重新下单')).toBeTruthy()
  20. expect(getByText('商家缺货')).toBeTruthy()
  21. expect(getByText('价格不合适')).toBeTruthy()
  22. // 有多个"其他原因"文本,使用getAllByText
  23. expect(getAllByText('其他原因').length).toBeGreaterThan(0)
  24. })
  25. it('不应该渲染对话框当open为false时', () => {
  26. const { queryByText } = render(
  27. <CancelReasonDialog {...defaultProps} open={false} />
  28. )
  29. expect(queryByText('取消订单')).toBeNull()
  30. })
  31. it('应该选择预定义原因当点击时', () => {
  32. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  33. // 检查对话框基本渲染
  34. expect(getByText('取消订单')).toBeTruthy()
  35. expect(getByText('请选择或填写取消原因,这将帮助我们改进服务')).toBeTruthy()
  36. })
  37. it('应该调用onConfirm当确认按钮被点击时', () => {
  38. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  39. const reasonOption = getByText('我不想买了')
  40. fireEvent.click(reasonOption)
  41. const confirmButton = getByText('确认取消')
  42. fireEvent.click(confirmButton)
  43. expect(defaultProps.onConfirm).toHaveBeenCalledWith('我不想买了')
  44. })
  45. it('应该调用onOpenChange当取消按钮被点击时', () => {
  46. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  47. const cancelButton = getByText('取消')
  48. fireEvent.click(cancelButton)
  49. expect(defaultProps.onOpenChange).toHaveBeenCalledWith(false)
  50. })
  51. it('应该显示错误当确认空原因时', () => {
  52. const { getByText, getByRole } = render(<CancelReasonDialog {...defaultProps} />)
  53. const confirmButton = getByText('确认取消')
  54. fireEvent.click(confirmButton)
  55. // 使用更精确的查询方式查找错误消息
  56. const errorMessage = getByText('请输入取消原因')
  57. expect(errorMessage).toBeTruthy()
  58. expect(defaultProps.onConfirm).not.toHaveBeenCalled()
  59. })
  60. it('应该显示错误当原因超过200字符时', () => {
  61. const { getByPlaceholderText, getByText } = render(
  62. <CancelReasonDialog {...defaultProps} />
  63. )
  64. const input = getByPlaceholderText('请输入其他取消原因...')
  65. fireEvent.input(input, { target: { value: 'a'.repeat(201) } })
  66. const confirmButton = getByText('确认取消')
  67. fireEvent.click(confirmButton)
  68. const errorMessage = getByText('取消原因不能超过200个字符')
  69. expect(errorMessage).toBeTruthy()
  70. expect(defaultProps.onConfirm).not.toHaveBeenCalled()
  71. })
  72. it('应该处理自定义原因输入', () => {
  73. const { getByPlaceholderText, getByText } = render(
  74. <CancelReasonDialog {...defaultProps} />
  75. )
  76. const input = getByPlaceholderText('请输入其他取消原因...')
  77. fireEvent.input(input, { target: { value: '自定义取消原因' } })
  78. const confirmButton = getByText('确认取消')
  79. fireEvent.click(confirmButton)
  80. expect(defaultProps.onConfirm).toHaveBeenCalledWith('自定义取消原因')
  81. })
  82. it('应该显示加载状态当loading为true时', () => {
  83. const { getByText } = render(
  84. <CancelReasonDialog {...defaultProps} loading={true} />
  85. )
  86. expect(getByText('提交中...')).toBeTruthy()
  87. })
  88. it('应该禁用按钮当loading为true时', () => {
  89. const { getByText } = render(
  90. <CancelReasonDialog {...defaultProps} loading={true} />
  91. )
  92. const cancelButton = getByText('取消')
  93. const confirmButton = getByText('提交中...')
  94. // 检查按钮是否被禁用
  95. expect(cancelButton).toBeTruthy()
  96. expect(confirmButton).toBeTruthy()
  97. })
  98. it('应该重置状态当对话框关闭时', () => {
  99. const { getByText, rerender } = render(
  100. <CancelReasonDialog {...defaultProps} />
  101. )
  102. const reasonOption = getByText('我不想买了')
  103. fireEvent.click(reasonOption)
  104. // 重新渲染关闭的对话框
  105. rerender(<CancelReasonDialog {...defaultProps} open={false} />)
  106. // 重新渲染打开的对话框
  107. rerender(<CancelReasonDialog {...defaultProps} open={true} />)
  108. // 检查状态是否重置 - 直接点击确认按钮应该显示错误
  109. const confirmButton = getByText('确认取消')
  110. fireEvent.click(confirmButton)
  111. const errorMessage = getByText('请输入取消原因')
  112. expect(errorMessage).toBeTruthy()
  113. })
  114. })