53 lines
1.5 KiB
JavaScript
53 lines
1.5 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 transformed = `${stubImports}\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)
|
|
|
|
assert.strictEqual(typeof component.activated, 'function', '流程列表页应在激活时刷新数据')
|
|
|
|
let refreshCount = 0
|
|
component.activated.call({
|
|
getList() {
|
|
refreshCount += 1
|
|
}
|
|
})
|
|
|
|
assert.strictEqual(refreshCount, 1, '流程列表页激活时应调用一次 getList')
|
|
|
|
console.log('workflow-index-refresh test passed')
|