271 lines
9.7 KiB
JavaScript
271 lines
9.7 KiB
JavaScript
/**
|
|
* 维修类型树形选择功能测试
|
|
*
|
|
* 测试目标:
|
|
* 1. 验证维修类型树形数据API正常工作
|
|
* 2. 验证树形数据结构符合前端组件要求
|
|
* 3. 验证数据包含正确的层级关系
|
|
*/
|
|
|
|
const axios = require('axios');
|
|
const colors = require('colors');
|
|
|
|
// 配置
|
|
const config = {
|
|
baseURL: 'http://localhost/dev-api',
|
|
timeout: 10000,
|
|
clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
|
|
};
|
|
|
|
/**
|
|
* 获取Token(登录)
|
|
*/
|
|
async function getToken() {
|
|
try {
|
|
const { getToken } = require('./quick-token-getter');
|
|
return await getToken('admin', 'admin123');
|
|
} catch (error) {
|
|
console.error('获取Token失败:', error.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试维修类型树形数据API
|
|
*/
|
|
async function testCategoryTreeAPI(token) {
|
|
console.log('\n🌳 测试维修类型树形数据API'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const response = await axios.get(`${config.baseURL}/pm/repairCategory/tree`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
clientid: config.clientId,
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
},
|
|
timeout: config.timeout,
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
console.log('✅ API调用成功'.green);
|
|
console.log(`📊 状态码: ${response.status}`);
|
|
console.log(`📝 响应消息: ${response.data.msg}`);
|
|
|
|
const treeData = response.data.data;
|
|
console.log(`\n📈 数据分析:`.yellow);
|
|
console.log(` - 根分类数量: ${treeData ? treeData.length : 0}`.yellow);
|
|
|
|
if (treeData && treeData.length > 0) {
|
|
console.log(` - 数据类型: ${typeof treeData}`.yellow);
|
|
console.log(` - 第一个分类: ${treeData[0].categoryName}`.yellow);
|
|
console.log(
|
|
` - ID字段: ${treeData[0].id} (类型: ${typeof treeData[0].id})`.yellow,
|
|
);
|
|
|
|
// 检查树形结构
|
|
let totalCategories = 0;
|
|
let maxDepth = 0;
|
|
|
|
function analyzeTree(nodes, depth = 1) {
|
|
if (!nodes) return;
|
|
|
|
maxDepth = Math.max(maxDepth, depth);
|
|
|
|
nodes.forEach((node) => {
|
|
totalCategories++;
|
|
if (node.children && node.children.length > 0) {
|
|
console.log(
|
|
` - ${node.categoryName} (${node.children.length}个子分类)`.cyan,
|
|
);
|
|
analyzeTree(node.children, depth + 1);
|
|
}
|
|
});
|
|
}
|
|
|
|
analyzeTree(treeData);
|
|
|
|
console.log(` - 总分类数量: ${totalCategories}`.yellow);
|
|
console.log(` - 最大层级深度: ${maxDepth}`.yellow);
|
|
|
|
// 验证数据结构是否符合el-tree-select要求
|
|
console.log(`\n🔍 数据结构验证:`.magenta);
|
|
const firstCategory = treeData[0];
|
|
const hasId = firstCategory.hasOwnProperty('id');
|
|
const hasCategoryName = firstCategory.hasOwnProperty('categoryName');
|
|
const hasChildren = firstCategory.hasOwnProperty('children');
|
|
|
|
console.log(` - 包含id字段: ${hasId ? '✅' : '❌'}`.magenta);
|
|
console.log(` - 包含categoryName字段: ${hasCategoryName ? '✅' : '❌'}`.magenta);
|
|
console.log(` - 包含children字段: ${hasChildren ? '✅' : '❌'}`.magenta);
|
|
|
|
if (hasId && hasCategoryName && hasChildren) {
|
|
console.log(` - el-tree-select兼容性: ✅ 完全兼容`.green);
|
|
} else {
|
|
console.log(` - el-tree-select兼容性: ❌ 数据结构不符合要求`.red);
|
|
}
|
|
|
|
// 显示完整的树形结构(前3个分类)
|
|
console.log(`\n📋 树形结构示例:`.cyan);
|
|
treeData.slice(0, 3).forEach((category, index) => {
|
|
displayTreeStructure(category, 0, index < 2);
|
|
});
|
|
|
|
if (treeData.length > 3) {
|
|
console.log(` ... (还有${treeData.length - 3}个分类)`.gray);
|
|
}
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
data: treeData,
|
|
analysis: {
|
|
totalCategories,
|
|
maxDepth,
|
|
rootCount: treeData ? treeData.length : 0,
|
|
},
|
|
};
|
|
} else {
|
|
console.log('❌ API调用失败'.red);
|
|
console.log(` 状态码: ${response.data.code}`.red);
|
|
console.log(` 错误信息: ${response.data.msg}`.red);
|
|
return { success: false, error: response.data };
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 请求异常'.red);
|
|
console.log(` 错误信息: ${error.message}`.red);
|
|
if (error.response) {
|
|
console.log(` HTTP状态: ${error.response.status}`.red);
|
|
console.log(` 响应数据: ${JSON.stringify(error.response.data, null, 2)}`.red);
|
|
}
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示树形结构
|
|
*/
|
|
function displayTreeStructure(node, depth = 0, showChildren = true) {
|
|
const indent = ' '.repeat(depth);
|
|
const prefix = depth === 0 ? '📁' : depth === 1 ? '├── 📄' : '└── 📝';
|
|
|
|
console.log(`${indent}${prefix} ${node.categoryName} (ID: ${node.id})`.cyan);
|
|
|
|
if (showChildren && node.children && node.children.length > 0) {
|
|
node.children.forEach((child, index) => {
|
|
const isLast = index === node.children.length - 1;
|
|
const childPrefix = isLast ? '└──' : '├──';
|
|
console.log(
|
|
`${indent} ${childPrefix} 📝 ${child.categoryName} (ID: ${child.id})`.gray,
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试普通选项API(对比)
|
|
*/
|
|
async function testCategoryOptionsAPI(token) {
|
|
console.log('\n📋 测试维修类型选项API (对比)'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const response = await axios.get(`${config.baseURL}/pm/repairCategory/options`, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
clientid: config.clientId,
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
},
|
|
timeout: config.timeout,
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
console.log('✅ 选项API调用成功'.green);
|
|
const optionsData = response.data.data;
|
|
console.log(`📊 选项数量: ${optionsData ? optionsData.length : 0}`.yellow);
|
|
|
|
if (optionsData && optionsData.length > 0) {
|
|
console.log(`📝 选项格式示例:`.yellow);
|
|
console.log(` - ${JSON.stringify(optionsData[0], null, 2)}`.gray);
|
|
}
|
|
|
|
return { success: true, data: optionsData };
|
|
} else {
|
|
console.log('❌ 选项API调用失败'.red);
|
|
return { success: false, error: response.data };
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 选项API请求异常'.red);
|
|
console.log(` 错误信息: ${error.message}`.red);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 主测试函数
|
|
*/
|
|
async function main() {
|
|
console.log('🚀 维修类型树形选择功能测试开始'.rainbow.bold);
|
|
console.log('测试时间:', new Date().toLocaleString());
|
|
|
|
try {
|
|
// 获取Token
|
|
console.log('\n🔐 获取访问Token...'.yellow);
|
|
const token = await getToken();
|
|
|
|
if (!token) {
|
|
console.log('❌ 无法获取Token,测试终止'.red);
|
|
return;
|
|
}
|
|
|
|
console.log('✅ Token获取成功'.green);
|
|
|
|
// 测试树形数据API
|
|
const treeResult = await testCategoryTreeAPI(token);
|
|
|
|
// 测试选项数据API(对比)
|
|
const optionsResult = await testCategoryOptionsAPI(token);
|
|
|
|
// 总结测试结果
|
|
console.log('\n📊 测试总结'.rainbow.bold);
|
|
console.log('='.repeat(50).rainbow);
|
|
|
|
if (treeResult.success) {
|
|
console.log('✅ 树形数据API: 正常工作'.green);
|
|
console.log(` - 根分类数量: ${treeResult.analysis?.rootCount || 0}`.green);
|
|
console.log(` - 总分类数量: ${treeResult.analysis?.totalCategories || 0}`.green);
|
|
console.log(` - 最大层级: ${treeResult.analysis?.maxDepth || 0}`.green);
|
|
} else {
|
|
console.log('❌ 树形数据API: 存在问题'.red);
|
|
}
|
|
|
|
if (optionsResult.success) {
|
|
console.log('✅ 选项数据API: 正常工作'.green);
|
|
console.log(` - 平面选项数量: ${optionsResult.data?.length || 0}`.green);
|
|
} else {
|
|
console.log('❌ 选项数据API: 存在问题'.red);
|
|
}
|
|
|
|
// 功能建议
|
|
console.log('\n💡 功能验证建议:'.cyan.bold);
|
|
console.log('1. 在前端页面中测试树形选择器的展示效果'.cyan);
|
|
console.log('2. 验证搜索过滤功能是否正常工作'.cyan);
|
|
console.log('3. 测试选择不同层级分类时的数据传递'.cyan);
|
|
console.log('4. 确认表单提交时categoryId值的正确性'.cyan);
|
|
} catch (error) {
|
|
console.log('\n❌ 测试过程中发生异常:'.red.bold);
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
if (require.main === module) {
|
|
main().catch(console.error);
|
|
}
|
|
|
|
module.exports = {
|
|
testCategoryTreeAPI,
|
|
testCategoryOptionsAPI,
|
|
};
|