Files
loan-pricing/ruoyi-ui/tests/workflow-index-refresh.test.js
2026-05-25 16:04:23 +08:00

81 lines
2.6 KiB
JavaScript

const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
function loadComponentOptions(filePath) {
const source = fs.readFileSync(filePath, 'utf8')
const scriptMatch = source.match(/<script>([\s\S]*?)<\/script>/)
if (!scriptMatch) {
throw new Error('未找到组件脚本内容')
}
const importNames = []
const importPattern = /^import\s+([A-Za-z0-9_]+)\s+from\s+.*$/gm
let importMatch = importPattern.exec(scriptMatch[1])
while (importMatch) {
importNames.push(importMatch[1])
importMatch = importPattern.exec(scriptMatch[1])
}
const stubImports = importNames.map(name => `const ${name} = {};`).join('\n')
const namedImportStubs = `
const listWorkflow = function() {};
const getWorkflowEdit = function() {};
const formatRate = function(value) { return value; };
const mapGetters = function(names) {
const result = {};
names.forEach((name) => {
result[name] = function() { return ''; };
});
return result;
};`
const transformed = `${stubImports}\n${namedImportStubs}\n${scriptMatch[1]}`
.replace(/^import .*$/gm, '')
.replace(/export default/, 'module.exports =')
const sandbox = {
module: { exports: {} },
exports: {},
require,
console
}
vm.runInNewContext(transformed, sandbox, { filename: filePath })
return sandbox.module.exports
}
const filePath = path.resolve(__dirname, '../src/views/loanPricing/workflow/index.vue')
const component = loadComponentOptions(filePath)
const source = fs.readFileSync(filePath, 'utf8')
assert.strictEqual(typeof component.activated, 'function', '流程列表页应在激活时刷新数据')
assert(source.includes('icon="el-icon-edit"') && source.includes('>编辑</el-button>'), '流程列表操作列应包含编辑按钮')
assert(source.includes('v-if="canEdit(scope.row)"'), '流程列表编辑按钮应只对可编辑行显示')
assert(source.includes('row.createBy === this.currentCreateBy'), '流程列表应按创建者判断编辑按钮权限')
assert(source.includes('getWorkflowEdit(row.serialNum)'), '编辑操作应先查询编辑数据')
let refreshCount = 0
component.activated.call({
getList() {
refreshCount += 1
}
})
assert.strictEqual(refreshCount, 1, '流程列表页激活时应调用一次 getList')
assert.strictEqual(component.methods.canEdit.call({
currentCreateBy: '张三-8920001'
}, {
createBy: '张三-8920001'
}), true, '创建者应允许编辑')
assert.strictEqual(component.methods.canEdit.call({
currentCreateBy: '张三-8920001'
}, {
createBy: '李四-8920002'
}), false, '非创建者不应允许编辑')
console.log('workflow-index-refresh test passed')