2
0

CancelReasonDialog.test.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 } = 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. expect(getByText('其他原因')).toBeTruthy()
  23. })
  24. it('不应该渲染对话框当open为false时', () => {
  25. const { queryByText } = render(
  26. <CancelReasonDialog {...defaultProps} open={false} />
  27. )
  28. expect(queryByText('取消订单')).toBeNull()
  29. })
  30. it('应该选择预定义原因当点击时', () => {
  31. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  32. // 检查对话框基本渲染
  33. expect(getByText('取消订单')).toBeTruthy()
  34. expect(getByText('请选择或填写取消原因,这将帮助我们改进服务')).toBeTruthy()
  35. })
  36. it('应该调用onConfirm当确认按钮被点击时', () => {
  37. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  38. const reasonOption = getByText('我不想买了')
  39. fireEvent.click(reasonOption)
  40. const confirmButton = getByText('确认取消')
  41. fireEvent.click(confirmButton)
  42. expect(defaultProps.onConfirm).toHaveBeenCalledWith('我不想买了')
  43. })
  44. it('应该调用onOpenChange当取消按钮被点击时', () => {
  45. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  46. const cancelButton = getByText('取消')
  47. fireEvent.click(cancelButton)
  48. expect(defaultProps.onOpenChange).toHaveBeenCalledWith(false)
  49. })
  50. it('应该显示错误当确认空原因时', () => {
  51. const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
  52. const confirmButton = getByText('确认取消')
  53. fireEvent.click(confirmButton)
  54. expect(getByText('请输入取消原因')).toBeTruthy()
  55. expect(defaultProps.onConfirm).not.toHaveBeenCalled()
  56. })
  57. it('应该显示错误当原因超过200字符时', () => {
  58. const { getByPlaceholderText, getByText } = render(
  59. <CancelReasonDialog {...defaultProps} />
  60. )
  61. const input = getByPlaceholderText('请输入其他取消原因...')
  62. fireEvent.input(input, { target: { value: 'a'.repeat(201) } })
  63. const confirmButton = getByText('确认取消')
  64. fireEvent.click(confirmButton)
  65. expect(getByText('取消原因不能超过200个字符')).toBeTruthy()
  66. expect(defaultProps.onConfirm).not.toHaveBeenCalled()
  67. })
  68. it('应该处理自定义原因输入', () => {
  69. const { getByPlaceholderText, getByText } = render(
  70. <CancelReasonDialog {...defaultProps} />
  71. )
  72. const input = getByPlaceholderText('请输入其他取消原因...')
  73. fireEvent.input(input, { target: { value: '自定义取消原因' } })
  74. const confirmButton = getByText('确认取消')
  75. fireEvent.click(confirmButton)
  76. expect(defaultProps.onConfirm).toHaveBeenCalledWith('自定义取消原因')
  77. })
  78. it('应该显示加载状态当loading为true时', () => {
  79. const { getByText } = render(
  80. <CancelReasonDialog {...defaultProps} loading={true} />
  81. )
  82. expect(getByText('提交中...')).toBeTruthy()
  83. })
  84. it('应该禁用按钮当loading为true时', () => {
  85. const { getByText } = render(
  86. <CancelReasonDialog {...defaultProps} loading={true} />
  87. )
  88. const cancelButton = getByText('取消')
  89. const confirmButton = getByText('提交中...')
  90. // 检查按钮是否被禁用
  91. expect(cancelButton).toBeTruthy()
  92. expect(confirmButton).toBeTruthy()
  93. })
  94. it('应该重置状态当对话框关闭时', () => {
  95. const { getByText, rerender } = render(
  96. <CancelReasonDialog {...defaultProps} />
  97. )
  98. const reasonOption = getByText('我不想买了')
  99. fireEvent.click(reasonOption)
  100. // 重新渲染关闭的对话框
  101. rerender(<CancelReasonDialog {...defaultProps} open={false} />)
  102. rerender(<CancelReasonDialog {...defaultProps} open={true} />)
  103. // 检查状态是否重置
  104. const confirmButton = getByText('确认取消')
  105. fireEvent.click(confirmButton)
  106. expect(getByText('请输入取消原因')).toBeTruthy()
  107. })
  108. })