| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import { render, fireEvent } from '@testing-library/react'
- import { mockShowToast } from '~/__mocks__/taroMock'
- import CancelReasonDialog from '@/components/common/CancelReasonDialog'
- describe('CancelReasonDialog', () => {
- const defaultProps = {
- open: true,
- onOpenChange: jest.fn(),
- onConfirm: jest.fn(),
- loading: false
- }
- beforeEach(() => {
- jest.clearAllMocks()
- })
- it('应该渲染对话框当可见时为true', () => {
- const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
- expect(getByText('取消订单')).toBeTruthy()
- expect(getByText('请选择或填写取消原因,这将帮助我们改进服务')).toBeTruthy()
- expect(getByText('我不想买了')).toBeTruthy()
- expect(getByText('信息填写错误,重新下单')).toBeTruthy()
- expect(getByText('商家缺货')).toBeTruthy()
- expect(getByText('价格不合适')).toBeTruthy()
- expect(getByText('其他原因')).toBeTruthy()
- })
- it('不应该渲染对话框当open为false时', () => {
- const { queryByText } = render(
- <CancelReasonDialog {...defaultProps} open={false} />
- )
- expect(queryByText('取消订单')).toBeNull()
- })
- it('应该选择预定义原因当点击时', () => {
- const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
- // 检查对话框基本渲染
- expect(getByText('取消订单')).toBeTruthy()
- expect(getByText('请选择或填写取消原因,这将帮助我们改进服务')).toBeTruthy()
- })
- it('应该调用onConfirm当确认按钮被点击时', () => {
- const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
- const reasonOption = getByText('我不想买了')
- fireEvent.click(reasonOption)
- const confirmButton = getByText('确认取消')
- fireEvent.click(confirmButton)
- expect(defaultProps.onConfirm).toHaveBeenCalledWith('我不想买了')
- })
- it('应该调用onOpenChange当取消按钮被点击时', () => {
- const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
- const cancelButton = getByText('取消')
- fireEvent.click(cancelButton)
- expect(defaultProps.onOpenChange).toHaveBeenCalledWith(false)
- })
- it('应该显示错误当确认空原因时', () => {
- const { getByText } = render(<CancelReasonDialog {...defaultProps} />)
- const confirmButton = getByText('确认取消')
- fireEvent.click(confirmButton)
- expect(getByText('请输入取消原因')).toBeTruthy()
- expect(defaultProps.onConfirm).not.toHaveBeenCalled()
- })
- it('应该显示错误当原因超过200字符时', () => {
- const { getByPlaceholderText, getByText } = render(
- <CancelReasonDialog {...defaultProps} />
- )
- const input = getByPlaceholderText('请输入其他取消原因...')
- fireEvent.input(input, { target: { value: 'a'.repeat(201) } })
- const confirmButton = getByText('确认取消')
- fireEvent.click(confirmButton)
- expect(getByText('取消原因不能超过200个字符')).toBeTruthy()
- expect(defaultProps.onConfirm).not.toHaveBeenCalled()
- })
- it('应该处理自定义原因输入', () => {
- const { getByPlaceholderText, getByText } = render(
- <CancelReasonDialog {...defaultProps} />
- )
- const input = getByPlaceholderText('请输入其他取消原因...')
- fireEvent.input(input, { target: { value: '自定义取消原因' } })
- const confirmButton = getByText('确认取消')
- fireEvent.click(confirmButton)
- expect(defaultProps.onConfirm).toHaveBeenCalledWith('自定义取消原因')
- })
- it('应该显示加载状态当loading为true时', () => {
- const { getByText } = render(
- <CancelReasonDialog {...defaultProps} loading={true} />
- )
- expect(getByText('提交中...')).toBeTruthy()
- })
- it('应该禁用按钮当loading为true时', () => {
- const { getByText } = render(
- <CancelReasonDialog {...defaultProps} loading={true} />
- )
- const cancelButton = getByText('取消')
- const confirmButton = getByText('提交中...')
- // 检查按钮是否被禁用
- expect(cancelButton).toBeTruthy()
- expect(confirmButton).toBeTruthy()
- })
- it('应该重置状态当对话框关闭时', () => {
- const { getByText, rerender } = render(
- <CancelReasonDialog {...defaultProps} />
- )
- const reasonOption = getByText('我不想买了')
- fireEvent.click(reasonOption)
- // 重新渲染关闭的对话框
- rerender(<CancelReasonDialog {...defaultProps} open={false} />)
- rerender(<CancelReasonDialog {...defaultProps} open={true} />)
- // 检查状态是否重置
- const confirmButton = getByText('确认取消')
- fireEvent.click(confirmButton)
- expect(getByText('请输入取消原因')).toBeTruthy()
- })
- })
|