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();
|
});
|
});
|