tony.cheng
2026-03-17 2461efdde98a2509c7ac5a9fd56629aaecb8b281
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { message } from 'antd';
import TabContainer from '../components/dashboard/TabContainer';
import ProcessAPIService from '../services/ProcessAPIService';
import { getMergedParams } from '../utils/urlParams';
 
// Mock依赖
jest.mock('../services/ProcessAPIService');
jest.mock('../utils/urlParams');
jest.mock('antd', () => ({
  message: {
    success: jest.fn(),
    error: jest.fn()
  },
  Spin: ({ children }) => <div data-testid="spin">{children}</div>,
  Modal: ({ children, visible, onOk, onCancel, title, confirmLoading }) => (
    visible ? (
      <div data-testid="modal">
        <h3>{title}</h3>
        {children}
        <button onClick={onOk} disabled={confirmLoading}>确定</button>
        <button onClick={onCancel}>取消</button>
      </div>
    ) : null
  )
}));
 
jest.mock('../contexts/CaseDataContext', () => ({
  useCaseData: () => ({
    caseData: {
      mediation: {
        state: 0, // 测试终止状态
        id: 'test-mediation-id'
      }
    },
    refreshData: jest.fn()
  })
}));
 
describe('AI调解状态控制功能测试', () => {
  beforeEach(() => {
    jest.clearAllMocks();
    getMergedParams.mockReturnValue({ caseId: 'test-case-id' });
  });
 
  test('应该显示终止按钮当caseState为0', () => {
    render(<TabContainer activeTab="mediation-board" />);
    
    // 等待组件渲染完成
    expect(screen.getByText('终止')).toBeInTheDocument();
  });
 
  test('应该显示恢复按钮当caseState为5', () => {
    // 修改mock数据
    jest.mock('../contexts/CaseDataContext', () => ({
      useCaseData: () => ({
        caseData: {
          mediation: {
            state: 5, // 恢复状态
            id: 'test-mediation-id'
          }
        },
        refreshData: jest.fn()
      })
    }));
 
    render(<TabContainer activeTab="mediation-board" />);
    
    expect(screen.getByText('恢复')).toBeInTheDocument();
  });
 
  test('点击终止按钮应该显示确认对话框', () => {
    render(<TabContainer activeTab="mediation-board" />);
    
    const terminateButton = screen.getByText('终止');
    fireEvent.click(terminateButton);
    
    expect(screen.getByTestId('modal')).toBeInTheDocument();
    expect(screen.getByText('确认终止调解')).toBeInTheDocument();
  });
 
  test('确认终止应该调用API并显示成功消息', async () => {
    ProcessAPIService.updateMediationState.mockResolvedValue({ code: 200 });
    
    render(<TabContainer activeTab="mediation-board" />);
    
    // 点击终止按钮
    const terminateButton = screen.getByText('终止');
    fireEvent.click(terminateButton);
    
    // 点击确认按钮
    const confirmButton = screen.getByText('确定');
    fireEvent.click(confirmButton);
    
    // 验证API调用
    await waitFor(() => {
      expect(ProcessAPIService.updateMediationState).toHaveBeenCalledWith(
        'test-case-id',
        {
          action: 0,
          userName: '调解员'
        }
      );
    });
    
    // 验证成功消息
    expect(message.success).toHaveBeenCalledWith('案件状态更新成功');
  });
 
  test('API调用失败应该显示错误消息', async () => {
    ProcessAPIService.updateMediationState.mockRejectedValue(new Error('网络错误'));
    
    render(<TabContainer activeTab="mediation-board" />);
    
    // 点击终止按钮
    const terminateButton = screen.getByText('终止');
    fireEvent.click(terminateButton);
    
    // 点击确认按钮
    const confirmButton = screen.getByText('确定');
    fireEvent.click(confirmButton);
    
    // 验证错误消息
    await waitFor(() => {
      expect(message.error).toHaveBeenCalledWith('网络错误');
    });
  });
 
  test('不应该显示按钮当caseState为其他值', () => {
    // 修改mock数据为不支持的状态
    jest.mock('../contexts/CaseDataContext', () => ({
      useCaseData: () => ({
        caseData: {
          mediation: {
            state: 2, // 不支持的状态
            id: 'test-mediation-id'
          }
        },
        refreshData: jest.fn()
      })
    }));
 
    render(<TabContainer activeTab="mediation-board" />);
    
    expect(screen.queryByText('终止')).not.toBeInTheDocument();
    expect(screen.queryByText('恢复')).not.toBeInTheDocument();
  });
});